gpt4 book ai didi

javascript - 为什么不是所有图像都显示在此 slider 中?

转载 作者:行者123 更新时间:2023-11-28 09:21:40 25 4
gpt4 key购买 nike

这是我的代码

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
var startSlider = function() {
var startSlide = $(".slider li:first");
var nextSlide = $(".active").next("li");
$(".active").removeClass();

if(nextSlide.html() == null) {
nextSlide = startSlide;
}

nextSlide.addClass("active");

setTimeout(startSlider, 1000);
};

setTimeout(startSlider, 1000);
});
</script>
<style>
.active{
display:none;
}
</style>
</head>
<body>
<ul class="slider" style="list-style:none;">
<li><div class="active" style="background:#F00;height:100px;width:100px;margin:6px; position: fixed; top: 100px;"></div></li>
<li><div style="background:#0F0;height:100px;width:100px;margin:6px; position: fixed; top:100px;"></div></li>
<li><div style="background:#00F;height:100px;width:100px;margin:6px; position: fixed; top:100px;"></li>
<li><div style="background:#000;height:100px;width:100px;margin:6px; position: fixed; top: 100px;"></div></li>
</ul>
</body>
</html>

当我运行这段代码时,只有蓝色和黑色的 div 一次又一次地出现。这可能是什么原因?我试图将 active 类放在不同的位置,但没有帮助。我删除了答案中建议的错字,它不在原始代码中,只出现在这里。

最佳答案

您的脚本将从第一个 div 开始一次将 class="active" 设置为四个 div 之一。因为这是 active CSS 类的定义

<style>
.active{
display:none;
}
</style>

当前具有 class="active" 的 div 将消失,但其他三个不会消失,它们被放置在完全相同的位置,因此只能看到较暗的颜色,在这种情况下是蓝色和黑色。

我建议使用如下两个 CSS 类

<style>
.active{
display:block;
}
.inactive{
display:none;
}
</style>

并更改您的脚本,使一个 div 具有 class="active",其余的一次具有 class="inactive",这意味着 active div 将出现,inactive div 将消失。

<script>
$(document).ready(function() {
var startSlider = function() {
var startSlide = $(".slider li:first");
var nextSlide = $(".active").next("li");

// remove css class from all divs
$(".active").removeClass();
$(".inactive").removeClass();

if(nextSlide.html() == null)
{nextSlide = startSlide;}

// set nextSlide css class to active
nextSlide.addClass("active");

// set the rest of the divs css class to inactive
nextSlide.siblings().addClass("inactive");

setTimeout(startSlider, 1000);
};
setTimeout(startSlider, 1000);
});

</script>

这是进行上述更改后的完整代码。

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
var startSlider = function() {
var startSlide = $(".slider li:first");
var nextSlide = $(".active").next("li");

// remove css class from all divs
$(".active").removeClass();
$(".inactive").removeClass();

if(nextSlide.html() == null)
{nextSlide = startSlide;}

// set nextSlide css class to active
nextSlide.addClass("active");

// set the rest of the divs css class to inactive
nextSlide.siblings().addClass("inactive");

setTimeout(startSlider, 1000);
};
setTimeout(startSlider, 1000);
});

</script>
<style>
.active{
display:block;
}
.inactive{
display:none;
}
</style>
</head>
<body>

<ul class="slider" style="list-style:none;">
<li><div class="active" style="background:#F00;height:100px;width:100px;margin:6px; position: fixed; top: 100px;"></div></li>
<li><div class="inactive" style="background:#0F0;height:100px;width:100px;margin:6px; position: fixed; top:100px;"></div></li>
<li><div class="inactive" style="background:#00F;height:100px;width:100px;margin:6px; position: fixed; top:100px;"></div></li>
<li><div class="inactive" style="background:#000;height:100px;width:100px;margin:6px; position: fixed; top: 100px;"></div></li>
</ul>

</body>
</html>

这是工作的 jsfiddle:http://jsfiddle.net/k8qod5g0/

关于javascript - 为什么不是所有图像都显示在此 slider 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26075248/

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