简体   繁体   English

如何使div在本节中循环

[英]How to make div loop around in section

I'm working on a Roulette site, but i don't have any idea how to make the divs loop around. 我正在轮盘网站上工作,但是我不知道如何使div循环。 I need the divs to loop around when they get out of the section border. 当它们离开截面边界时,我需要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> 

so when i click on the button, it should move and then once they go out of the section border, the divs should loop around to the left and show again on the left. 因此,当我单击按钮时,它应该移动,然后一旦它们离开部分边框,div应当在左侧循环并再次在左侧显示。

Thanks in advance! 提前致谢!

You don't have to animate all the elements, you can only animate the first one that will push the other, then you need to use overflow in order to be able to put the last element at the start. 您不必为所有元素设置动画,只可以为第一个元素添加动画,然后推入另一个元素,然后需要使用溢出功能才能将最后一个元素放在开始位置。

So you may try something like this (that can be improved of course) : 因此,您可以尝试这样的操作(当然可以改进):

 $("#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> 

Just add a class with keyframe animation to your jquery code, and set the pixels and time you want: 只需将具有keyframe animation的类添加到您的jquery代码中,然后设置所需的像素和时间即可:

 $("#right").click(function() { $("div").addClass("animate") }) 
 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; } div#six { width: 15%; height: 50px; margin-left: 5px; background: red; float: left; z-index: -1; left: 0px; position: relative; } div#five { width: 15%; height: 50px; background: black; float: left; margin-left: 5px; z-index: -1; left: 0px; position: relative; } div#two { width: 15%; height: 50px; margin-left: 5px; background: red; float: left; z-index: -1; left: 0px; position: relative; } div#three { width: 15%; height: 50px; margin-left: 5px; background: black; float: left; z-index: -1; left: 0px; position: relative; } div#four { width: 15%; height: 50px; margin-left: 5px; background: red; float: left; z-index: -1; left: 0px; position: relative; } .animate{ animation: roulette 7s linear infinite; } @keyframes roulette { from {left: -550px;} to {left: 550px;} } 
 <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> 

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

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