-6ren">
gpt4 book ai didi

javascript - jQuery 图像交叉淡入淡出不起作用

转载 作者:行者123 更新时间:2023-12-03 06:07:50 25 4
gpt4 key购买 nike

您好,我们的 jQuery 图像交叉淡入淡出设置有什么问题!? http://tips.techmatemn.com/

HTML

<div class="hero-cycler">
<img class="active" alt="Tips qualified Client 1" src="<?php bloginfo('stylesheet_directory'); ?>/assets/img/hero-bk-1.jpg">
<img alt="Tips qualified Client 2" src="<?php bloginfo('stylesheet_directory'); ?>/assets/img/hero-bk-2.jpg">
</div>

CSS

.hero-cycler{position:relative;}
.hero-cycler img{position:absolute;z-index:1}
.hero-cycler img.active{z-index:3}

脚本

<script> // homepage client images
function cycleImages(){
var $active = $('.hero-cycler .active');
var $next = ($active.next().length > 0) ? $active.next() : $('.hero-cycler 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>

谢谢!

最佳答案

更改调用函数的方式。您的 setInterval 使用字符串参数,该参数已过时且通常不推荐。使用如下所示的标准方法来正确运行该函数。

setInterval('cycleImages()', 7000); 更改为 setInterval(cycleImages, 7000);

带有通用填充图像的完整工作副本:

function cycleImages() {
var $active = $('.hero-cycler .active');
var $next = ($active.next().length > 0) ? $active.next() : $('.hero-cycler 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);
})
.hero-cycler {
position: relative;
}
.hero-cycler img {
position: absolute;
z-index: 1
}
.hero-cycler img.active {
z-index: 3
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="hero-cycler">
<img class="active" alt="Tips qualified Client 1" src="https://unsplash.it/300/200/?image=0">
<img alt="Tips qualified Client 2" src="https://unsplash.it/300/200/?image=1">
</div>

(快速检查浏览器控制台会向您显示错误:Uncaught ReferenceError:cycleImages未定义,这对于您的问题非常有帮助)

关于javascript - jQuery 图像交叉淡入淡出不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39457024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com