<html> <head> <style type="text/css"> .random-box { width: 400px; height: 200px; border: 1px solid #000; margin: 100px; overflow: hidden; position: relative; } .random-box:before { height: 200px; width: 1px; background: red; position: absolute; left: 0; right:0; margin: auto; display: block; content: ''; z-index: 999; } .items { position: relative; } .item { width: 150px; height: 150px; background: red; margin: 20px; float: left; } .item.red { background: red; } .item.blue { background: blue; } .item.yellow { background: yellow; } .item.orange { background: orange; } .item.pink { background: pink; } .item.brown { background: brown; } .item.aqua { background: aqua; } </style> </head> <body> <div class="random-box"> <div class="items"> </div> </div> <script> function rand(i) { $(".items").css("margin-left",i); } $("#rand").on("click", function() { var i = 0; var speed = 5; var count = $(".item").length; $(".items").css("width", count*190); var los = setInterval(function(){ i = i - speed; if ((i*(-1))%500==0) { speed=speed-1; if (speed==0) { clearInterval(los); } } if ((i*(-1))>count*160) { i = 260; } rand(i); }, 1); }); </script> </body> </html>
Wszystko działa tak, że mam div "items" i w nim boxy, które są losowane, przesuwam go zwyczajnie margin-left, natomiast pojawia się problem gdy przesunę już cały div i chciałbym wrócić do początku, wtedy ustawiam margin-left: na 0, teoretycznie działa, ale widac taki duży "przeskok / przestrzeń ". Ma ktoś pomysł jak to naprawić?