Instead of storing the number by how many elements you need to move, you can simply take it from the input value.
The size of the shift can be determined very simply: ΡΠΈΡΠΈΠ½Π°_ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ*Π½ΠΎΠΌΠ΅Ρ_ΠΊΠ°ΡΡΠΈΠ½ΠΊΠΈ
Π½ΠΎΠΌΠ΅Ρ_ΠΊΠ°ΡΡΠΈΠ½ΠΊΠΈ in this case coincides with the value of the input.
To move the list to the left - the value of the margin-left must be negative, as a result, the function of the change handler degenerates into the next
document.getElementById('position_new_product').addEventListener('input', function() { list.style.marginLeft = (-width * this.value) + 'px'; });
Working example:
/* ΠΊΠΎΠ½ΡΠΈΠ³ΡΡΠ°ΡΠΈΡ */ var width = 256; // ΡΠΈΡΠΈΠ½Π° ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡ var list = carousel.querySelector('#carousel div.gallery>ul'); document.getElementById('position_new_product').addEventListener('input', function() { list.style.marginLeft = (-width * this.value) + 'px'; });
.carousel { position: relative; width: 256px; } .carousel img { display: block; } .arrow { position: absolute; top: 100px; padding: 0; background: white; /*border-radius: 15px;*/ border: none; font-size: 1.8em; line-height: 24px; color: #444; display: block; } .prev { left: 7px; } .next { right: 7px; } .gallery { width: 100%; overflow: hidden; } .gallery ul { width: 9999px; margin: 0; padding: 0; list-style: none; transition: margin-left 1000ms; font-size: 0; } .gallery li { display: inline-block; }
<div id="carousel" class="carousel"> <div class="gallery"> <ul class="images"> <li class="partn"> <img src="//www.gravatar.com/avatar/802d132837bedd698ebd9b8fe7fb77ae?s=256&d=identicon"> </li> <li class="partn"> <img src="//www.gravatar.com/avatar/cbfaff96665b7567defe1b34a883db8b?s=256&d=identicon"> </li> <li class="partn"> <img src="//www.gravatar.com/avatar/1ab1d534250b21d145a3bf548b954901?s=256&d=identicon"> </li> <li class="partn"> <img src="//www.gravatar.com/avatar/4ffa137ca41767871222a5ca3d4e24ad?s=256&d=identicon"> </li> <li class="partn"> <img src="https://graph.facebook.com/905721779505680/picture?type=large"> </li> </ul> </div> <input type="range" value="0" max="4" id="position_new_product" /> </div>