gpt4 book ai didi

javascript - 在 Jquery 上循环访问类

转载 作者:行者123 更新时间:2023-11-29 15:28:08 26 4
gpt4 key购买 nike

我正在创建一个 slider ,它基本上是 4 个圆圈,当您单击下一个按钮时会更改大小和位置。这是工作:http://bluemoontesting.co.uk/bluemoon2016/people.html

单击下一步按钮时它工作正常,但单击后退按钮时不会反转。有人可以帮忙吗?

$(document).ready(function () {
var steps = ['right-center', 'bottom-right', 'bottom-left', 'left', 'left-center', 'top', ],
allClasses = steps.join(' ');
$('.animate-slider.next').click(function() {
$('#a').removeClass(allClasses).addClass(steps[0]);
$('#b').removeClass(allClasses).addClass(steps[1]);
$('#c').removeClass(allClasses).addClass(steps[2]);
$('#d').removeClass(allClasses).addClass(steps[3]);
$('#e').removeClass(allClasses).addClass(steps[4]);
$('#f').removeClass(allClasses).addClass(steps[5]);
steps.push(steps.shift()); // move first element to the end

// to rotate the other direction you would pop and unshift instead
// steps.unshift(steps.pop()); // move last element to the beginning
});

});

$(document).ready(function () {
var steps = ['right-center', 'bottom-right', 'bottom-left', 'left', 'left-center', 'top', ],
allClasses = steps.join(' ');
$('.animate-slider.back').click(function() {
$('#a').removeClass(allClasses).addClass(steps[0]);
$('#b').removeClass(allClasses).addClass(steps[1]);
$('#c').removeClass(allClasses).addClass(steps[2]);
$('#d').removeClass(allClasses).addClass(steps[3]);
$('#e').removeClass(allClasses).addClass(steps[4]);
$('#f').removeClass(allClasses).addClass(steps[5]);
steps.unshift(steps.pop()); // move first element to the end

// to rotate the other direction you would pop and unshift instead
// steps.unshift(steps.pop()); // move last element to the beginning
});

最佳答案

从每个圈子中删除 id 并添加一个类“circle”来代替

$(function() {

currentState = 0;

var steps = ['right-center', 'bottom-right', 'bottom-left', 'left', 'left-center', 'top', ],
allClasses = steps.join(' ');

$('.circle').each(function(i){
$(this).addClass(steps[i]);
});
$('.animate-slider.next').click(function() {
currentState++;
$('.circle').each(function(i) {
console.log((currentState + i) % steps.length);
$(this).removeClass(allClasses).addClass(steps[(currentState + i) % steps.length]);
});
});


$('.animate-slider.back').click(function() {
currentState --;
currentState = currentState<=0 ? steps.length-Math.abs(currentState): currentState;

$('.circle').each(function(i) {
console.log((currentState + i) % steps.length, currentState, i);
$(this).removeClass(allClasses).addClass(steps[(currentState + i) % steps.length]);
});
});


})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="controls">
<a class="animate-slider next">Next</a>
<a class="animate-slider back">Back</a>
</div>
<div class="circles">
<div data-offset="-10" class="ball circle" style="transform: translateX(1.59031px) translateY(13.4848px);">
<span class="team-1 active">Core objectives</span>
<span class="team-2">Be Authentic</span>
<span class="team-3">Express the brand values, not yourself</span>
<span class="team-4">Core objectives</span>
<span class="team-5">Core objectives</span>
<span class="team-6">Core objectives</span>
</div>
<div id="b" data-offset="10" class="ball circle" style="transform: translateX(-1.59031px) translateY(-13.4848px);">
<span class="team-1 active">UX as branding</span>
<span class="team-2">Every step of the process, every time</span>
<span class="team-3">Complicate, then clarify</span>
<span class="team-4">Core objectives</span>
<span class="team-5">Core objectives</span>
<span class="team-6">Core objectives</span></div>
<div id="c" data-offset="15" class="ball circle" style="transform: translateX(-2.38547px) translateY(-20.2273px);">
<span class="team-1 active">Analytics, SEO &amp; PP</span>
<span class="team-2">Be engaging</span>
<span class="team-3">Restriction is freedom</span>
<span class="team-4">Core objectives</span>
<span class="team-5">Core objectives</span>
<span class="team-6">Core objectives</span></div>
<div id="d" data-offset="-5" class="ball circle" style="transform: translateX(0.795157px) translateY(6.74242px);">
<span class="team-1 active">Be holistic</span>
<span class="team-2">Be distinctive</span>
<span class="team-3">What’s the story?</span>
<span class="team-4">Core objectives</span>
<span class="team-5">Core objectives</span>
<span class="team-6">Core objectives</span></div>
<div id="e" data-offset="-15" class="ball circle" style="transform: translateX(2.38547px) translateY(20.2273px);">
<span class="team-1 active">Functionality</span>
<span class="team-2">Be practical</span>
<span class="team-3">Beware fashion</span>
<span class="team-4">Core objectives</span>
<span class="team-5">Core objectives</span>
<span class="team-6">Core objectives</span></div>
<div id="f" data-offset="-10" class="ball circle" style="transform: translateX(1.59031px) translateY(13.4848px);">
<span class="team-1 active">Risk management</span>
<span class="team-2">Risk management</span>
<span class="team-3">Core objectives</span>
<span class="team-4">Core objectives</span>
<span class="team-5">Core objectives</span>
<span class="team-6">Core objectives</span></div>
</div>

此示例包含 css LINK

关于javascript - 在 Jquery 上循环访问类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37205206/

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