简体   繁体   English

jQuery“ .on('click')”事件仅工作一次。 图像滑块

[英]jQuery “.on('click')” event works only once. Image Slider

I've been looking for a solution that I couldn't find. 我一直在寻找找不到的解决方案。

I built a simple image slider where every time you click on the picture this should change with the follow one. 我建立了一个简单的图像滑块,每当您单击图片时,该滑块就会随着下一个变化。 Specifically, on-click event move the css property z-index to the next picture using addClass and removeClass. 具体来说,单击事件使用addClass和removeClass将css属性z-index移动到下一张图片。

The problem is that it works only the first time you press on and never again, this means I can show only the first and the second picture. 问题在于它仅在您第一次按下时起作用,并且再也不会起作用,这意味着我只能显示第一张和第二张图片。 I cannot find solution, what I'm looking for is just how to get the on-click event work every time I click on the picture? 我找不到解决方案,我要寻找的只是每次单击图片时如何使点击事件起作用?

 var currentImg = $(".active"); var nextImg = currentImg.next(); $(".imageSlider").on("click", function() { alert("ciao"); currentImg.removeClass(".active").css("z-index", 0); nextImg.addClass(".active").css("z-index", 10); }); 
 .active {z-index: 10;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="imageSlider"> <div class="images"> <img src="JLT1.jpg" class="active" height="500px" width="700px"> <img src="JLT2.jpg" height="500px" width="700px"> <img src="JLT3.jpg" height="500px" width="700px"> <img src="JLT4.jpg" height="500px" width="700px"> </div> </div> 

  1. currentImg = $(".active"); nextImg = currentImg.next(); should be placed in function 应该放在功能上
  2. removeClass(".active") => removeClass("active") . removeClass(".active") => removeClass("active") addClass(".active") => addClass("active")
  3. next() is a problem. next()是一个问题。

Except z-index, you can try another attribute -- opacity. 除z-index外,您可以尝试另一个属性-不透明度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM