Not able to add Previous function to simple jQuery Slider












1















Can you please take a look at this demo and let me know how I can enable previous button to slide to? As you can see the next button is working fine but I am not able to slide back.






$("#slideshow > div:gt(0)").hide();

$("#next").on("click", function() {
$('#slideshow > div:first')
.fadeOut(1000)
.next()
.fadeIn(1000)
.end()
.appendTo('#slideshow');
});

$("#prev").on("click", function() {
$('#slideshow > div:prev')
.fadeOut(1000)
.next()
.fadeIn(1000)
.end()
.appendTo('#slideshow');
});

#slideshow {
margin: 80px auto;
position: relative;
width: 240px;
height: 240px;
padding: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.4);
}

#slideshow>div {
position: absolute;
top: 10px;
left: 10px;
right: 10px;
bottom: 10px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="next"> next</button>
<button id="prev"> prev</button>

<div id="slideshow">
<div>
<img src="http://farm6.static.flickr.com/5224/5658667829_2bb7d42a9c_m.jpg">
</div>
<div>
<img src="http://farm6.static.flickr.com/5230/5638093881_a791e4f819_m.jpg">
</div>
<div>
Pretty cool eh? This slide is proof the content can be anything.
</div>
</div>












share|improve this question

























  • I already look at that post but couldnt' figure it out how is working on this example

    – Behseini
    Nov 13 '18 at 14:19






  • 1





    if it doesn't'! so why did you marked my question as duplicated!?

    – Behseini
    Nov 13 '18 at 14:21











  • Again? why duplicated!? please correct your mark?

    – Mona Coder
    Nov 13 '18 at 15:02
















1















Can you please take a look at this demo and let me know how I can enable previous button to slide to? As you can see the next button is working fine but I am not able to slide back.






$("#slideshow > div:gt(0)").hide();

$("#next").on("click", function() {
$('#slideshow > div:first')
.fadeOut(1000)
.next()
.fadeIn(1000)
.end()
.appendTo('#slideshow');
});

$("#prev").on("click", function() {
$('#slideshow > div:prev')
.fadeOut(1000)
.next()
.fadeIn(1000)
.end()
.appendTo('#slideshow');
});

#slideshow {
margin: 80px auto;
position: relative;
width: 240px;
height: 240px;
padding: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.4);
}

#slideshow>div {
position: absolute;
top: 10px;
left: 10px;
right: 10px;
bottom: 10px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="next"> next</button>
<button id="prev"> prev</button>

<div id="slideshow">
<div>
<img src="http://farm6.static.flickr.com/5224/5658667829_2bb7d42a9c_m.jpg">
</div>
<div>
<img src="http://farm6.static.flickr.com/5230/5638093881_a791e4f819_m.jpg">
</div>
<div>
Pretty cool eh? This slide is proof the content can be anything.
</div>
</div>












share|improve this question

























  • I already look at that post but couldnt' figure it out how is working on this example

    – Behseini
    Nov 13 '18 at 14:19






  • 1





    if it doesn't'! so why did you marked my question as duplicated!?

    – Behseini
    Nov 13 '18 at 14:21











  • Again? why duplicated!? please correct your mark?

    – Mona Coder
    Nov 13 '18 at 15:02














1












1








1








Can you please take a look at this demo and let me know how I can enable previous button to slide to? As you can see the next button is working fine but I am not able to slide back.






$("#slideshow > div:gt(0)").hide();

$("#next").on("click", function() {
$('#slideshow > div:first')
.fadeOut(1000)
.next()
.fadeIn(1000)
.end()
.appendTo('#slideshow');
});

$("#prev").on("click", function() {
$('#slideshow > div:prev')
.fadeOut(1000)
.next()
.fadeIn(1000)
.end()
.appendTo('#slideshow');
});

#slideshow {
margin: 80px auto;
position: relative;
width: 240px;
height: 240px;
padding: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.4);
}

#slideshow>div {
position: absolute;
top: 10px;
left: 10px;
right: 10px;
bottom: 10px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="next"> next</button>
<button id="prev"> prev</button>

<div id="slideshow">
<div>
<img src="http://farm6.static.flickr.com/5224/5658667829_2bb7d42a9c_m.jpg">
</div>
<div>
<img src="http://farm6.static.flickr.com/5230/5638093881_a791e4f819_m.jpg">
</div>
<div>
Pretty cool eh? This slide is proof the content can be anything.
</div>
</div>












share|improve this question
















Can you please take a look at this demo and let me know how I can enable previous button to slide to? As you can see the next button is working fine but I am not able to slide back.






$("#slideshow > div:gt(0)").hide();

$("#next").on("click", function() {
$('#slideshow > div:first')
.fadeOut(1000)
.next()
.fadeIn(1000)
.end()
.appendTo('#slideshow');
});

$("#prev").on("click", function() {
$('#slideshow > div:prev')
.fadeOut(1000)
.next()
.fadeIn(1000)
.end()
.appendTo('#slideshow');
});

#slideshow {
margin: 80px auto;
position: relative;
width: 240px;
height: 240px;
padding: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.4);
}

#slideshow>div {
position: absolute;
top: 10px;
left: 10px;
right: 10px;
bottom: 10px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="next"> next</button>
<button id="prev"> prev</button>

<div id="slideshow">
<div>
<img src="http://farm6.static.flickr.com/5224/5658667829_2bb7d42a9c_m.jpg">
</div>
<div>
<img src="http://farm6.static.flickr.com/5230/5638093881_a791e4f819_m.jpg">
</div>
<div>
Pretty cool eh? This slide is proof the content can be anything.
</div>
</div>








$("#slideshow > div:gt(0)").hide();

$("#next").on("click", function() {
$('#slideshow > div:first')
.fadeOut(1000)
.next()
.fadeIn(1000)
.end()
.appendTo('#slideshow');
});

$("#prev").on("click", function() {
$('#slideshow > div:prev')
.fadeOut(1000)
.next()
.fadeIn(1000)
.end()
.appendTo('#slideshow');
});

#slideshow {
margin: 80px auto;
position: relative;
width: 240px;
height: 240px;
padding: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.4);
}

#slideshow>div {
position: absolute;
top: 10px;
left: 10px;
right: 10px;
bottom: 10px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="next"> next</button>
<button id="prev"> prev</button>

<div id="slideshow">
<div>
<img src="http://farm6.static.flickr.com/5224/5658667829_2bb7d42a9c_m.jpg">
</div>
<div>
<img src="http://farm6.static.flickr.com/5230/5638093881_a791e4f819_m.jpg">
</div>
<div>
Pretty cool eh? This slide is proof the content can be anything.
</div>
</div>





$("#slideshow > div:gt(0)").hide();

$("#next").on("click", function() {
$('#slideshow > div:first')
.fadeOut(1000)
.next()
.fadeIn(1000)
.end()
.appendTo('#slideshow');
});

$("#prev").on("click", function() {
$('#slideshow > div:prev')
.fadeOut(1000)
.next()
.fadeIn(1000)
.end()
.appendTo('#slideshow');
});

#slideshow {
margin: 80px auto;
position: relative;
width: 240px;
height: 240px;
padding: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.4);
}

#slideshow>div {
position: absolute;
top: 10px;
left: 10px;
right: 10px;
bottom: 10px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="next"> next</button>
<button id="prev"> prev</button>

<div id="slideshow">
<div>
<img src="http://farm6.static.flickr.com/5224/5658667829_2bb7d42a9c_m.jpg">
</div>
<div>
<img src="http://farm6.static.flickr.com/5230/5638093881_a791e4f819_m.jpg">
</div>
<div>
Pretty cool eh? This slide is proof the content can be anything.
</div>
</div>






javascript jquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 15:34









Peter B

12.9k51941




12.9k51941










asked Nov 13 '18 at 14:07









BehseiniBehseini

2,287164483




2,287164483













  • I already look at that post but couldnt' figure it out how is working on this example

    – Behseini
    Nov 13 '18 at 14:19






  • 1





    if it doesn't'! so why did you marked my question as duplicated!?

    – Behseini
    Nov 13 '18 at 14:21











  • Again? why duplicated!? please correct your mark?

    – Mona Coder
    Nov 13 '18 at 15:02



















  • I already look at that post but couldnt' figure it out how is working on this example

    – Behseini
    Nov 13 '18 at 14:19






  • 1





    if it doesn't'! so why did you marked my question as duplicated!?

    – Behseini
    Nov 13 '18 at 14:21











  • Again? why duplicated!? please correct your mark?

    – Mona Coder
    Nov 13 '18 at 15:02

















I already look at that post but couldnt' figure it out how is working on this example

– Behseini
Nov 13 '18 at 14:19





I already look at that post but couldnt' figure it out how is working on this example

– Behseini
Nov 13 '18 at 14:19




1




1





if it doesn't'! so why did you marked my question as duplicated!?

– Behseini
Nov 13 '18 at 14:21





if it doesn't'! so why did you marked my question as duplicated!?

– Behseini
Nov 13 '18 at 14:21













Again? why duplicated!? please correct your mark?

– Mona Coder
Nov 13 '18 at 15:02





Again? why duplicated!? please correct your mark?

– Mona Coder
Nov 13 '18 at 15:02












1 Answer
1






active

oldest

votes


















1














Use a counter.



You cannot use the pseudo-class :prev selector because it does not exist



Is there a "previous sibling" CSS selector?






$(function() {
$("#slideshow > div:gt(0)").hide();
var $divs = $('#slideshow > div'),
nofSlides = $divs.length,
counter = 0;

$(".but").on("click", function() {
dir = $(this).is("#next") ? 1 : -1;
$divs.eq(counter).stop().fadeOut(1000); // use stop to not queue
counter += dir;
if (counter >= nofSlides) counter = 0;
if (counter < 0) counter = nofSlides - 1;
$divs.eq(counter).stop().fadeIn(1000);
});
});

#slideshow {
margin: 80px auto;
position: relative;
width: 240px;
height: 240px;
padding: 10px;
box-shadow: 0 0 20px rgba(0, 0, 0, 0.4);
}

#slideshow>div {
position: absolute;
top: 10px;
left: 10px;
right: 10px;
bottom: 10px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" id="prev" class="but"> prev</button>
<button type="button" id="next" class="but"> next</button>
<div id="slideshow">

<div>1
<img src="http://farm6.static.flickr.com/5224/5658667829_2bb7d42a9c_m.jpg">
</div>
<div>2
<img src="http://farm6.static.flickr.com/5230/5638093881_a791e4f819_m.jpg">
</div>
<div>3 Pretty cool eh? This slide is proof the content can be anything.
</div>
</div>








share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282835%2fnot-able-to-add-previous-function-to-simple-jquery-slider%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Use a counter.



    You cannot use the pseudo-class :prev selector because it does not exist



    Is there a "previous sibling" CSS selector?






    $(function() {
    $("#slideshow > div:gt(0)").hide();
    var $divs = $('#slideshow > div'),
    nofSlides = $divs.length,
    counter = 0;

    $(".but").on("click", function() {
    dir = $(this).is("#next") ? 1 : -1;
    $divs.eq(counter).stop().fadeOut(1000); // use stop to not queue
    counter += dir;
    if (counter >= nofSlides) counter = 0;
    if (counter < 0) counter = nofSlides - 1;
    $divs.eq(counter).stop().fadeIn(1000);
    });
    });

    #slideshow {
    margin: 80px auto;
    position: relative;
    width: 240px;
    height: 240px;
    padding: 10px;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.4);
    }

    #slideshow>div {
    position: absolute;
    top: 10px;
    left: 10px;
    right: 10px;
    bottom: 10px;
    }

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <button type="button" id="prev" class="but"> prev</button>
    <button type="button" id="next" class="but"> next</button>
    <div id="slideshow">

    <div>1
    <img src="http://farm6.static.flickr.com/5224/5658667829_2bb7d42a9c_m.jpg">
    </div>
    <div>2
    <img src="http://farm6.static.flickr.com/5230/5638093881_a791e4f819_m.jpg">
    </div>
    <div>3 Pretty cool eh? This slide is proof the content can be anything.
    </div>
    </div>








    share|improve this answer






























      1














      Use a counter.



      You cannot use the pseudo-class :prev selector because it does not exist



      Is there a "previous sibling" CSS selector?






      $(function() {
      $("#slideshow > div:gt(0)").hide();
      var $divs = $('#slideshow > div'),
      nofSlides = $divs.length,
      counter = 0;

      $(".but").on("click", function() {
      dir = $(this).is("#next") ? 1 : -1;
      $divs.eq(counter).stop().fadeOut(1000); // use stop to not queue
      counter += dir;
      if (counter >= nofSlides) counter = 0;
      if (counter < 0) counter = nofSlides - 1;
      $divs.eq(counter).stop().fadeIn(1000);
      });
      });

      #slideshow {
      margin: 80px auto;
      position: relative;
      width: 240px;
      height: 240px;
      padding: 10px;
      box-shadow: 0 0 20px rgba(0, 0, 0, 0.4);
      }

      #slideshow>div {
      position: absolute;
      top: 10px;
      left: 10px;
      right: 10px;
      bottom: 10px;
      }

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <button type="button" id="prev" class="but"> prev</button>
      <button type="button" id="next" class="but"> next</button>
      <div id="slideshow">

      <div>1
      <img src="http://farm6.static.flickr.com/5224/5658667829_2bb7d42a9c_m.jpg">
      </div>
      <div>2
      <img src="http://farm6.static.flickr.com/5230/5638093881_a791e4f819_m.jpg">
      </div>
      <div>3 Pretty cool eh? This slide is proof the content can be anything.
      </div>
      </div>








      share|improve this answer




























        1












        1








        1







        Use a counter.



        You cannot use the pseudo-class :prev selector because it does not exist



        Is there a "previous sibling" CSS selector?






        $(function() {
        $("#slideshow > div:gt(0)").hide();
        var $divs = $('#slideshow > div'),
        nofSlides = $divs.length,
        counter = 0;

        $(".but").on("click", function() {
        dir = $(this).is("#next") ? 1 : -1;
        $divs.eq(counter).stop().fadeOut(1000); // use stop to not queue
        counter += dir;
        if (counter >= nofSlides) counter = 0;
        if (counter < 0) counter = nofSlides - 1;
        $divs.eq(counter).stop().fadeIn(1000);
        });
        });

        #slideshow {
        margin: 80px auto;
        position: relative;
        width: 240px;
        height: 240px;
        padding: 10px;
        box-shadow: 0 0 20px rgba(0, 0, 0, 0.4);
        }

        #slideshow>div {
        position: absolute;
        top: 10px;
        left: 10px;
        right: 10px;
        bottom: 10px;
        }

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <button type="button" id="prev" class="but"> prev</button>
        <button type="button" id="next" class="but"> next</button>
        <div id="slideshow">

        <div>1
        <img src="http://farm6.static.flickr.com/5224/5658667829_2bb7d42a9c_m.jpg">
        </div>
        <div>2
        <img src="http://farm6.static.flickr.com/5230/5638093881_a791e4f819_m.jpg">
        </div>
        <div>3 Pretty cool eh? This slide is proof the content can be anything.
        </div>
        </div>








        share|improve this answer















        Use a counter.



        You cannot use the pseudo-class :prev selector because it does not exist



        Is there a "previous sibling" CSS selector?






        $(function() {
        $("#slideshow > div:gt(0)").hide();
        var $divs = $('#slideshow > div'),
        nofSlides = $divs.length,
        counter = 0;

        $(".but").on("click", function() {
        dir = $(this).is("#next") ? 1 : -1;
        $divs.eq(counter).stop().fadeOut(1000); // use stop to not queue
        counter += dir;
        if (counter >= nofSlides) counter = 0;
        if (counter < 0) counter = nofSlides - 1;
        $divs.eq(counter).stop().fadeIn(1000);
        });
        });

        #slideshow {
        margin: 80px auto;
        position: relative;
        width: 240px;
        height: 240px;
        padding: 10px;
        box-shadow: 0 0 20px rgba(0, 0, 0, 0.4);
        }

        #slideshow>div {
        position: absolute;
        top: 10px;
        left: 10px;
        right: 10px;
        bottom: 10px;
        }

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <button type="button" id="prev" class="but"> prev</button>
        <button type="button" id="next" class="but"> next</button>
        <div id="slideshow">

        <div>1
        <img src="http://farm6.static.flickr.com/5224/5658667829_2bb7d42a9c_m.jpg">
        </div>
        <div>2
        <img src="http://farm6.static.flickr.com/5230/5638093881_a791e4f819_m.jpg">
        </div>
        <div>3 Pretty cool eh? This slide is proof the content can be anything.
        </div>
        </div>








        $(function() {
        $("#slideshow > div:gt(0)").hide();
        var $divs = $('#slideshow > div'),
        nofSlides = $divs.length,
        counter = 0;

        $(".but").on("click", function() {
        dir = $(this).is("#next") ? 1 : -1;
        $divs.eq(counter).stop().fadeOut(1000); // use stop to not queue
        counter += dir;
        if (counter >= nofSlides) counter = 0;
        if (counter < 0) counter = nofSlides - 1;
        $divs.eq(counter).stop().fadeIn(1000);
        });
        });

        #slideshow {
        margin: 80px auto;
        position: relative;
        width: 240px;
        height: 240px;
        padding: 10px;
        box-shadow: 0 0 20px rgba(0, 0, 0, 0.4);
        }

        #slideshow>div {
        position: absolute;
        top: 10px;
        left: 10px;
        right: 10px;
        bottom: 10px;
        }

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <button type="button" id="prev" class="but"> prev</button>
        <button type="button" id="next" class="but"> next</button>
        <div id="slideshow">

        <div>1
        <img src="http://farm6.static.flickr.com/5224/5658667829_2bb7d42a9c_m.jpg">
        </div>
        <div>2
        <img src="http://farm6.static.flickr.com/5230/5638093881_a791e4f819_m.jpg">
        </div>
        <div>3 Pretty cool eh? This slide is proof the content can be anything.
        </div>
        </div>





        $(function() {
        $("#slideshow > div:gt(0)").hide();
        var $divs = $('#slideshow > div'),
        nofSlides = $divs.length,
        counter = 0;

        $(".but").on("click", function() {
        dir = $(this).is("#next") ? 1 : -1;
        $divs.eq(counter).stop().fadeOut(1000); // use stop to not queue
        counter += dir;
        if (counter >= nofSlides) counter = 0;
        if (counter < 0) counter = nofSlides - 1;
        $divs.eq(counter).stop().fadeIn(1000);
        });
        });

        #slideshow {
        margin: 80px auto;
        position: relative;
        width: 240px;
        height: 240px;
        padding: 10px;
        box-shadow: 0 0 20px rgba(0, 0, 0, 0.4);
        }

        #slideshow>div {
        position: absolute;
        top: 10px;
        left: 10px;
        right: 10px;
        bottom: 10px;
        }

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <button type="button" id="prev" class="but"> prev</button>
        <button type="button" id="next" class="but"> next</button>
        <div id="slideshow">

        <div>1
        <img src="http://farm6.static.flickr.com/5224/5658667829_2bb7d42a9c_m.jpg">
        </div>
        <div>2
        <img src="http://farm6.static.flickr.com/5230/5638093881_a791e4f819_m.jpg">
        </div>
        <div>3 Pretty cool eh? This slide is proof the content can be anything.
        </div>
        </div>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 15:36

























        answered Nov 13 '18 at 15:31









        mplungjanmplungjan

        87.6k21124181




        87.6k21124181






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53282835%2fnot-able-to-add-previous-function-to-simple-jquery-slider%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            The Sandy Post

            Danny Elfman

            Pages that link to "Head v. Amoskeag Manufacturing Co."