【发布时间】:2015-12-21 21:04:08
【问题描述】:
您好,我正在尝试为主页上的横幅图像实现交叉淡入淡出效果。我正在使用 jQuery 进行此操作,并且褪色效果运行良好。 这是我的代码:
<script>
function bannerImages(){
var $active = $('.banner-test .banner_one');
var $next = ($active.next().length > 0) ? $active.next() :
$('.banner-test img:first');
$next.css('z-index',2);//move the next image up the pile
$active.fadeOut(1500,function(){//fade out the top image
$active.css('z-index',1).show().removeClass('active');//reset the z-index and unhide the image
$next.css('z-index',3).addClass('active');//make the next image the top one
});
}
$(document).ready(function(){
// run every 7s
setInterval('cycleImages()', 7000);
})
</script>
正如我所说,这工作正常,但是我遇到了一个问题。为了使它起作用,我需要将position:absolute 应用于.banner-test img 类。现在我还在.banner-test 类中添加了另一个div,用于在横幅图像顶部显示一些文本。
代码如下所示:
<div class="banner-test">
<img class="banner_one" src="../image.jpg" alt="" />
<img src="../image2.jpg" alt=""/>
<div id="text">
<p class="text1">Sample Text</p>
</div>
</div>
还有 #text 的 css :
#text {
position:absolute;
bottom:35px ;
left:10px;
width:70% ;
background-color:#104E8B;
font-size:1em;
color:white;
opacity:0.95;
filter:alpha(opacity=95); /* IE transparency */
}
.text1 {
padding:13px;
margin:0px;
}
.banner-test {
display: block;
position: relative;
}
因此,如果我对图像应用绝对定位,则会将布局与文本混淆(所有内容都被推到页面顶部)。 有人能想出一个解决方法吗?
编辑
https://jsfiddle.net/ztrez888/1/embedded/result/ 这是小提琴 - 如果绝对位置应用于.banner-test img,则文本消失
【问题讨论】:
-
为了能够提供帮助,我们确实需要查看一个有效的示例。你能设置一个jsfiddle.net 来显示问题吗?
-
你必须为
.banner-test类父div设置高度。 -
两张图片中只有一张需要绝对位置,所以你可以把它们堆起来;)
-
@RoryMcCrossan 查看编辑
标签: javascript jquery html css