【问题标题】:how to center align an image inside the JavaScript Image Slider如何在 JavaScript 图像滑块中居中对齐图像
【发布时间】:2015-06-11 23:50:00
【问题描述】:

我用 JavaScript 创建了一个幻灯片放映,框架的分辨率设置为 960x540,但是当我将图像插入滑块时,它会自动向左对齐,如何将滑块内的图像对齐到中心,并且是即使图像不完全是滑块的分辨率,有任何方法可以自动对齐图像。这是我使用的基本代码。

 <style type="text/css">

#sliderFrame {position:relative;width:960px;margin: 0 auto 40px;}

#slider {
    width:960px;height:540px;/* Make it the same size as your images */
 background:#fff url(http://4.bp.blogspot.com/-aVGCcclfKQo/UR-T3vxFAMI/AAAAAAAABm8/ncwIfUdoLIw/s1600/loading.gif) no-repeat 50% 50%;
 position:relative;
 margin:0 auto;/*make the image slider center-aligned */
    box-shadow: 0px 1px 5px #999999;
}
#slider img {
 position:absolute;
 border:none;
 display:none;
}

/* the link style (if an image is wrapped in a link) */
#slider a.imgLink {
 z-index:2;
 display:none;position:absolute;
 top:0px;left:0px;border:0;padding:0;margin:0;
 width:100%;height:100%;
}

/* Caption styles */
div.mc-caption-bg, div.mc-caption-bg2 {
 position:absolute;
 width:100%;
 height:auto;
 padding:0;
 left:0px; 
 bottom:15px;
 z-index:3;
 overflow:hidden;
 font-size: 0;
}
div.mc-caption-bg {
 background-color:black;
}
div.mc-caption {
 font: bold 14px/20px Arial;
 color:#EEE;
 z-index:4;
 padding:10px 0;
 text-align:center;
}
div.mc-caption a {
 color:#FB0;
}
div.mc-caption a:hover {
 color:#DA0;
}


/* ------ built-in navigation bullets wrapper ------*/
div.navBulletsWrapper  {
 top:500px; left:280px; /* Its position is relative to the #slider */
 width:150px;
 background:none;
 padding-left:20px;
 position:relative;
 z-index:5;
 cursor:pointer;
}

/* each bullet */
div.navBulletsWrapper div 
{
    width:11px; height:11px;
    background:transparent url(http://3.bp.blogspot.com/-ZUaX5-lcCi4/UR-TzMXcpuI/AAAAAAAABm0/kokxtfFdNcU/s1600/bullet.png) no-repeat 0 0;
    float:left;overflow:hidden;vertical-align:middle;cursor:pointer;
    margin-right:11px;/* distance between each bullet*/
    _position:relative;/*IE6 hack*/
}
div.navBulletsWrapper div.active {background-position:0 -11px;}
.intro {
    bottom: 0;
    color: rgba(0, 0, 0, 0.2);
    font-size: 16px;
    position: absolute;
    right: 0;
    text-decoration: none;
    z-index: 99999;
}
/* --------- Others ------- */
#slider 
{
 transform: translate3d(0,0,0);
    -ms-transform:translate3d(0,0,0);
    -moz-transform:translate3d(0,0,0);
    -o-transform:translate3d(0,0,0);
}
</style>
<script src="http://project.dimpost.com/image-slider/js-image-slider-1.js" type="text/javascript"></script>
<script src="http://project.dimpost.com/image-slider/js-image-slider-2.js" type="text/javascript"></script>

【问题讨论】:

    标签: javascript image slideshow image-scaling


    【解决方案1】:

    据我了解,您可以添加

    left: 50%;
    

    使用“#slider img”选择器添加到您的 CSS 规则中,并添加一些 JavaScript 以将负边距设置为等于图像宽度一半的图像,例如(使用 jQuery):

    $("#slider img").each(function () {
        $(this).css("margin-left", -this.width / 2);
    })
    

    【讨论】:

    • 嗨,谢谢你的回复,我正在使用博客,所以我会为它做这个,我在编程方面不太好,我主要依靠试错法,所以可以指导我粘贴这些代码,这样我就不必弄乱我的网站了,你还可以建议一个代码来根据大小自动调整图像大小。目前我正在使用 Photoshop 调整每个图像的大小以制作 960x540,然后在滑块中使用,这非常耗时,有什么办法可以避免这种情况..
    • 我不确定你是否使用像 Wordpress 这样的博客工具,但如果你是,也许你应该搜索一些插件,我相信应该有用于图像滑块和图像大小调整的插件,据我了解,它们不需要您成为程序员来安装和使用它们。
    • 我正在使用博客,博客可用的插件看起来不太好,我一直在使用 photonack 的幻灯片放映,这是一个提供幻灯片放映的外部网站,我们可以将其嵌入页面..但最近我知道他们对每天的最大观众数有限制,所以我不能再坚持下去了,这就是我一直在寻找替代品的原因,我已经设法让这个幻灯片放映工作了,剩下的唯一问题是调整大小,如果没有其他解决方案,我可以用 Photoshop 管理
    • 不幸的是,我没有与博主合作过,所以我恐怕无法在这里为您提供帮助。也许您应该澄清您的问题或询问一个新的问题,指定您使用博客等。
    猜你喜欢
    • 2019-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-02
    • 2016-03-17
    • 2012-01-30
    • 2017-03-28
    相关资源
    最近更新 更多