gpt4 book ai didi

javascript - 如何使div在部分中循环

转载 作者:行者123 更新时间:2023-11-28 02:35:36 25 4
gpt4 key购买 nike

我正在做轮盘网站,但我不知道如何让 div 循环。当 div 离开部分边界时,我需要它们循环。

$("#right").click(function() {
$("#one").css("left", "300px");
$("#two").css("left", "300px");
$("#three").css("left", "300px");
$("#four").css("left", "300px");
$("#five").css("left", "300px");
$("#six").css("left", "300px");

});

$("#left").click(function() {
$("#one").css("left", "0px");
});
section {
width: 80%;
height: 50px;
border: 5px solid gray;
margin: auto;
padding: 7px;
z-index: 1;
transition: left 2s ease;
overflow: hidden;
}

div#one {
width: 15%;
height: 50px;
background: black;
float: left;
z-index: -1;
left: 0px;
position: relative;
transition: left 2s ease;
loop: true;
}

div#six {
width: 15%;
height: 50px;
margin-left: 5px;
background: red;
float: left;
z-index: -1;
left: 0px;
position: relative;
transition: left 2s ease;
loop: true;
}

div#five {
width: 15%;
height: 50px;
background: black;
float: left;
margin-left: 5px;
z-index: -1;
left: 0px;
position: relative;
transition: left 2s ease;
}

div#two {
width: 15%;
height: 50px;
margin-left: 5px;
background: red;
float: left;
z-index: -1;
left: 0px;
position: relative;
transition: left 2s ease;
}

div#three {
width: 15%;
height: 50px;
margin-left: 5px;
background: black;
float: left;
z-index: -1;
left: 0px;
position: relative;
transition: left 2s ease;
}

div#four {
width: 15%;
height: 50px;
margin-left: 5px;
background: red;
float: left;
z-index: -1;
left: 0px;
position: relative;
transition: left 2s ease;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section>
<div id="one"></div>
<div id="two"></div>
<div id="three"></div>
<div id="four"></div>
<div id="five"></div>
<div id="six"></div>
</section>

<button id="right">
left: 100px;
</button>

所以当我点击按钮时,它应该移动,然后一旦它们离开部分边界,div 应该绕到左边并再次显示在左边。

提前致谢!

最佳答案

您不必为所有元素制作动画,您可以只为第一个元素制作动画,然后您需要使用 overflow 以便能够将最后一个元素放在开头。

所以你可以尝试这样的事情(当然可以改进):

$("#right").click(function() {
$('section').prepend($('section div').last().css('margin-left','-30%'));
$('section >div').eq(1).css('margin-left', '0%');

});

$("#left").click(function() {
$("#one").css("left", "0px");
});
section {
width: 80%;
height: 50px;
border: 5px solid gray;
margin: auto;
padding: 7px;
z-index: 1;
transition: left 2s ease;
overflow: hidden;
display: flex;
}

section>div {
flex: 0 0 25%;
margin: 0 5px;
position: relative;
color: #fff;
text-align: center;
transition: 1s ease;
}

#one,
#three,
#five {
background: red;
}

#two,
#four,
#six {
background: black;
}

section>div:first-child {
margin-left: -30%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section>
<div id="one">1</div>
<div id="two">2</div>
<div id="three">3</div>
<div id="four">4</div>
<div id="five">5</div>
<div id="six">6</div>
</section>

<button id="right">
left: 100px;
</button>

关于javascript - 如何使div在部分中循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47648847/

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