I made a jquery slideshow and here is the code :
HTML
<div id="slideshow">
<img src="1.jpg">
<img src="2.jpg">
<img src="3.jpg">
</div>
<div id="previous">previous</div>
<div id="next">Next</div>
css
#slideshow {
width: 700px;
height: 400px;
position: relative;
overflow: hidden;
}
#slideshow img {
position: absolute;
}
jquery
$(document).ready(function() {
$('#slideshow img:gt(0)').hide();
$('#previous').click(function() {
$('#slideshow img:first').fadeOut(1000).prev().fadeIn(1000).end().appendTo('#slideshow');
});
$('#next').click(function() {
$('#slideshow img:first').fadeOut(1000).next().fadeIn(1000).end().appendTo('#slideshow');
});
});
The "next" button works, but when I click "previous" the image disappears! Can anyone help me?
Here is the fiddle.
Fiddle: http://jsfiddle.net/tAaCN/4/
Since your selector is using img:first
, you cannot use .prev()
to access the previous element since you are already at the first child.
You can instead select the last element and "prepend" it as a first child of the slideshow.
$(function() {
$('#slideshow img:gt(0)').hide();
$('#previous').click(function() {
$('#slideshow img:first').fadeOut(1000);
$('#slideshow img:last').fadeIn(1000).prependTo('#slideshow');
});
$('#next').click(function() {
$('#slideshow img:first').fadeOut(1000).next().fadeIn(1000).end().appendTo('#slideshow');
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With