I'm building a Javascript slider for my site, and I seem to be having a strange issue. I want the sliding motion to occur when I click on my "next slide" link. When I click on "Next slide" the first time, the image simply jumps from the first to the second; however when I click "Next slide" for the second time, the image slides to the third the way I want it to. Does anyone know why the sliding motion seems to skip the initial click? See code below.
.html
<script>
var Slider = function() { this.initialize.apply(this, arguments) }
Slider.prototype = {
initialize: function(slider) {
this.ul = slider.children[0]
this.li = this.ul.children
// make <ul> as large as all <li>’s
this.ul.style.width = (this.li[0].clientWidth * this.li.length) + 'px'
this.currentIndex = 0
},
goTo: function(index) {
// filter invalid indices
if (index < 0 || index > this.li.length - 1)
return
// move <ul> left
this.ul.style.left = '-' + (100 * index) + '%'
this.currentIndex = index
},
goToPrev: function() {
this.goTo(this.currentIndex - 1)
},
goToNext: function() {
this.goTo(this.currentIndex + 1)
}
}
</script>
<div class="slider">
<ul>
<li><img src="commercial1.png"></li>
<li><img src="commercial2.png"></li>
<li><img src="commercial3.png"></li>
</ul>
</div>
<script>
var sliders = []
$('.slider').each(function() {
sliders.push(new Slider(this))
})
</script>
<div class="controls">
<ul>
<li><a href="javascript:sliders[0].goToPrev()">.goToPrev()</a></li>
<li><a href="javascript:sliders[0].goToNext()">.goToNext()</a></li>
</ul>
</div>
.CSS
.slider {
width: 1000px; height: 500px;
overflow: hidden;
}
.slider > ul {
position: relative;
-webkit-transition: 0.5s left;
-moz-transition: 0.5s left;
-ms-transition: 0.5s left;
-o-transition: 0.5s left;
list-style: none;
margin: 0; padding: 0;
}
.slider > ul > li {
float: left;
width: 1000px; height: 500px;
}
.controls {
padding-top:10px;
}
Reason it isn't working is because you have provided the initial state for it to transition from.
Simply adjust your css to this
.slider > ul {
position: relative;
transition: left .5s ease-in-out;
left:0;
}
*note the left:0;
And that should do it
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.