gpt4 book ai didi

javascript - 为什么 jquery addClass 和 removeClass 只能与警报功能一起正常工作?

转载 作者:行者123 更新时间:2023-11-28 17:28:34 27 4
gpt4 key购买 nike

我正在尝试使用 jquery 制作我自己的 slider 。以下是工作示例:

$(document).ready(function(){


$(".carousel-indicators li").click(function(){
$(".carousel-indicators li").css("background-color", "transparent");
$(this).css("background-color", "yellow");
});


var active1, prev1, next1;

function rotateimages(){
active1 = $(".item.active");

if(active1.prev().length == 0) {
prev1 = $(".item").last();
prev1.removeClass("mytrans");
prev1.addClass("prev");
}
else {
prev1 = active1.prev();
prev1.removeClass("mytrans");
prev1.addClass("prev");
}

if(active1.next().length == 0) {
next1 = $(".item").first();
next1.removeClass("mytrans");
next1.addClass("next");
//alert();
}
else {
next1 = active1.next();
next1.removeClass("mytrans");
next1.addClass("next");
//alert();
}


active1.removeClass("active").addClass("prev mytrans");
next1.removeClass("next").addClass("mytrans active");
prev1.removeClass("prev mytrans");

}

setInterval(rotateimages, 3000);

});
.carousel-inner > .item > img,
.carousel-inner > .item > a > img {
width: 70%;
margin: auto;
}
html, body {
width: 100%;
height: 100%;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
list-style-type: none;
}
.container {
width: 768px;
margin: auto;
padding: 0 15px;
height: 100%;
}
#myCarousel {
width: 100%;
background-color: lightgray;
position: relative;
height: 100%;
}
.carousel-indicators {
position: absolute;
bottom: 20px;
left: 45%;
}
.carousel-indicators li {
list-style-type: circle;
display: inline-block;
width: 10px;
height: 10px;
border: 1px solid red;
border-radius: 10px;
cursor: pointer;
}

.mytrans {
transition: all 1s;
}
.carousel-inner .item {
position: absolute;
display: none;
}
.carousel-inner .item.prev {
left: -100%;
top: 0;
display: block;
}
.carousel-inner .item.next {
left: 100%;
top: 0;
display: block;
}
.carousel-inner .item.active {
left: 0%;
top: 0;
display: block;
}
.carousel-inner .left {
position: absolute;
left: 10%;
top: 50%;
}
.carousel-inner .right {
position: absolute;
right: 10%;
top: 50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">

<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
<li data-target="#myCarousel" data-slide-to="3"></li>
</ol>

<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">

<div class="item active">
<img src="http://madaboutwords.com/wp-content/uploads/2010/05/one.png" alt="Chania" width="460" height="345">
<div class="carousel-caption">
<h3>Chania</h3>
<p>The atmosphere in Chania has a touch of Florence and Venice.</p>
</div>
</div>



<div class="item">
<img src="http://digimind.com/blog/wp-content/uploads/2012/02/number2c.png" alt="Flower" width="460" height="345">
<div class="carousel-caption">
<h3>Flowers</h3>
<p>Beatiful flowers in Kolymbari, Crete.</p>
</div>
</div>

<div class="item">
<img src="http://www.hotel-r.net/im/hotel/gb/number-three-22.png" alt="Flower" width="460" height="345">
<div class="carousel-caption">
<h3>Flowers</h3>
<p>Beatiful flowers in Kolymbari, Crete.</p>
</div>
</div>

<div class="item">
<img src="http://www.hotel-r.net/im/hotel/gb/number-four-10.gif" alt="Flower" width="460" height="345">
<div class="carousel-caption">
<h3>Flowers</h3>
<p>Beatiful flowers in Kolymbari, Crete.</p>
</div>
</div>

</div>

<!-- Left and right controls -->
<a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>

我设计的代码是将下一个图像放在容器的右侧,然后平滑移动到中心。但是 下一个 图像永远不会达到 left: 100%。但是,如果我在 jquery addClass 方法之后添加 alert(),则 next 图像会到达容器的右侧。以下是代码:

$(document).ready(function() {


$(".carousel-indicators li").click(function() {
$(".carousel-indicators li").css("background-color", "transparent");
$(this).css("background-color", "yellow");
});


var active1, prev1, next1;

function rotateimages() {
active1 = $(".item.active");

if (active1.prev().length == 0) {
prev1 = $(".item").last();
prev1.removeClass("mytrans");
prev1.addClass("prev");
} else {
prev1 = active1.prev();
prev1.removeClass("mytrans");
prev1.addClass("prev");
}

if (active1.next().length == 0) {
next1 = $(".item").first();
next1.removeClass("mytrans");
next1.addClass("next");
alert();
} else {
next1 = active1.next();
next1.removeClass("mytrans");
next1.addClass("next");
alert();
}


active1.removeClass("active").addClass("prev mytrans");


next1.removeClass("next").addClass("mytransactive");


prev1.removeClass("prev mytrans");

}

setInterval(rotateimages, 3000);
});
.carousel-inner > .item > img,
.carousel-inner > .item > a > img {
width: 70%;
margin: auto;
}
html,
body {
width: 100%;
height: 100%;
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
list-style-type: none;
}
.container {
width: 768px;
margin: auto;
padding: 0 15px;
height: 100%;
}
#myCarousel {
width: 100%;
background-color: lightgray;
position: relative;
height: 100%;
}
.carousel-indicators {
position: absolute;
bottom: 20px;
left: 45%;
}
.carousel-indicators li {
list-style-type: circle;
display: inline-block;
width: 10px;
height: 10px;
border: 1px solid red;
border-radius: 10px;
cursor: pointer;
}
.mytrans {
transition: all 1s;
}
.carousel-inner .item {
position: absolute;
display: none;
}
.carousel-inner .item.prev {
left: -100%;
top: 0;
display: block;
}
.carousel-inner .item.next {
left: 100%;
top: 0;
display: block;
}
.carousel-inner .item.active {
left: 0%;
top: 0;
display: block;
}
.carousel-inner .left {
position: absolute;
left: 10%;
top: 50%;
}
.carousel-inner .right {
position: absolute;
right: 10%;
top: 50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">

<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Indicators -->
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class="active"></li>
<li data-target="#myCarousel" data-slide-to="1"></li>
<li data-target="#myCarousel" data-slide-to="2"></li>
<li data-target="#myCarousel" data-slide-to="3"></li>
</ol>

<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">

<div class="item active">
<img src="http://madaboutwords.com/wp-content/uploads/2010/05/one.png" alt="Chania" width="460" height="345">
<div class="carousel-caption">
<h3>Chania</h3>
<p>The atmosphere in Chania has a touch of Florence and Venice.</p>
</div>
</div>



<div class="item">
<img src="http://digimind.com/blog/wp-content/uploads/2012/02/number2c.png" alt="Flower" width="460" height="345">
<div class="carousel-caption">
<h3>Flowers</h3>
<p>Beatiful flowers in Kolymbari, Crete.</p>
</div>
</div>

<div class="item">
<img src="http://www.hotel-r.net/im/hotel/gb/number-three-22.png" alt="Flower" width="460" height="345">
<div class="carousel-caption">
<h3>Flowers</h3>
<p>Beatiful flowers in Kolymbari, Crete.</p>
</div>
</div>

<div class="item">
<img src="http://www.hotel-r.net/im/hotel/gb/number-four-10.gif" alt="Flower" width="460" height="345">
<div class="carousel-caption">
<h3>Flowers</h3>
<p>Beatiful flowers in Kolymbari, Crete.</p>
</div>
</div>

</div>

<!-- Left and right controls -->
<a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
<span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
<span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>

问题似乎是添加和删除类 next 的速度太快了。因此,在添加 next 样式之前,可能会删除 next 类。代码片段是:

else {
next1 = active1.next();
next1.removeClass("mytrans");
next1.addClass("next");
//alert();
}


active1.removeClass("active").addClass("prev mytrans");
next1.removeClass("next").addClass("mytrans active");
prev1.removeClass("prev mytrans");

问题:如何在 removeClass 函数删除 next 类之前应用 next 类的样式?

最佳答案

我建议不要像这样使用 removeClass/addClass 函数。

jQuery 中最优雅的方式是像这样切换它们:

active1.toggleClass('active');
active1.toggleClass('prev mytrans');
next1.toggleClass('next');
next1.toggleClass('mytrans active');

使用这种方法,您可以创建这样的函数,它更易于维护,并且不会像单独的添加/删除那样使代码困惑:

function toggleNext(){
active1.toggleClass('active');
active1.toggleClass('prev mytrans');
next1.toggleClass('next');
next1.toggleClass('mytrans active');
}

删除/添加类的缺点之一是当 jQuery 找不到要删除/添加的类时,您的代码将无法正常工作。

http://api.jquery.com/toggleclass/

关于javascript - 为什么 jquery addClass 和 removeClass 只能与警报功能一起正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38202069/

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