Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Slideshow

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.

like image 702
mahmoud nezar sarhan Avatar asked Mar 01 '14 16:03

mahmoud nezar sarhan


Video Answer


1 Answers

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');
    });
});
like image 146
mikhail Avatar answered Oct 25 '22 03:10

mikhail