[英]JQuery - Class: How to toggle/switch classes between 3 or more tags in a sequence?
How can I easily toggle/switch the classes between three or more divs in a sequence by click?如何通过单击轻松地在三个或更多 div 之间切换/切换类?
Example html:示例 html:
<div class="box">Box 1</div>
<div class="box active">Box 2</div>
<div class="box">Box 3</div>
<div class="next">Next</div>
<div class="back">Back</div>
This way just works with two:这种方式只适用于两个:
$(".next, .back").click(function(){
$(".box").toggleClass("active");
});
Simply to check if nth-child has the active class and loop.只需检查 nth-child 是否有活动的 class 和循环。
$(".next").click(function(){ $(".box").each( function(i, j) { if( $(this).hasClass('active') ) { $(this).removeClass('active'); $(".box:nth-child("+(((i+1)%$(".box").length)+1)+")").addClass('active'); return false; } }); }); $(".back").click(function(){ $(".box").each( function(i, j) { if( $(this).hasClass('active') ) { $(this).removeClass('active'); $(".box:nth-child("+(((i-1)+$(".box").length)%$(".box").length+1)+")").addClass('active'); return false; } }); });
.active { background-color: red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <div class="box">Box 1</div> <div class="box active">Box 2</div> <div class="box">Box 3</div> <div class="back">Back</div> <div class="next">Next</div> </div>
This is pretty generic这个很一般
$(".nav").on("click", function() { var dir = $(this).is(".next")? 1: -1; // which direction are we going? var active = $(".box.active").index() - 1; var $boxes = $(".box"); active += (1 * dir); if (active < 0) active = $boxes.length - 1; // wrap else if (active >= $boxes.length) active = 0; $(".box").removeClass("active"); $(".box").eq(active).addClass("active"); });
.active { color: red }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="box">Box 1</div> <div class="box active">Box 2</div> <div class="box">Box 3</div> <br/> <div class="nav back">Back</div> <div class="nav next">Next</div>
I've created another solution that gets the button's ID (which I've added) and uses that to dictate where to go.我创建了另一个解决方案,它获取按钮的 ID(我已添加)并使用它来指示 go 的位置。
var box = $('.box'); var maxBoxes = box.length; $("#nextBtn, #backBtn").click(function() { var getBtn = $(this).get(0).id, activeBox = $('.box.active'), position = activeBox.index(); activeBox.removeClass('active'); if (getBtn === 'nextBtn') { (position == maxBoxes)? box.eq(0).addClass('active'): activeBox.next().addClass('active'); } else { (position === 1)? box.last().addClass('active'): activeBox.prev().addClass('active'); } });
.active { color: red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="box">Box 1</div> <div class="box">Box 2</div> <div class="box active">Box 3</div> <div id="nextBtn" class="next">Next</div> <div id="backBtn" class="back">Back</div>
I think that the following should be efficient, because it will not do unnecessary checks.我认为以下应该是有效的,因为它不会做不必要的检查。
var last = $(".active")
function toggle(dir){
var future = last[dir]();
if(!(future.length && future.hasClass('box'))) {
return;
}
last.toggleClass("active")
last = future
last.toggleClass("active");
}
$(".next").click(toggle.bind(void 0, 'next'));
$(".back").click(toggle.bind(void 0, 'prev'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.