为什么jquery addClass和removeClass只能使用alert函数正常工作?

时间:2016-07-05 11:16:34

标签: javascript jquery html css

我正在尝试使用jquery创建自己的滑块。以下是工作示例:



$(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>
&#13;
&#13;
&#13;

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

&#13;
&#13;
$(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);
});
&#13;
.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%;
}
&#13;
<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>
&#13;
&#13;
&#13;

问题似乎是正在添加和删除类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类的样式?

2 个答案:

答案 0 :(得分:1)

我建议不要使用像这样的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/

答案 1 :(得分:0)

<html>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script type="text/javascript" >
        $(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 ").addClass(" prev");
                } else {
                    prev1 = active1.prev();
                    prev1.removeClass("mytrans ").addClass(" prev");
                }

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


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


                next1.removeClass("next").addClass("mytrans active");


                prev1.removeClass("prev mytrans");

            }

            setInterval(rotateimages, 3000);
        });


    </script>
    <style>

        .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%;
        }


    </style>
    <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>
</html>

试试这段代码。测试