简体   繁体   中英

Next and Prev button not work when click both

i'm trying to use banner slider with jquery.currentBanner value increase and decrease when i click button but banner not change.

SCRIPT:

$(document).ready(function () {
    var currentBanner = 0;
    $("#prev").bind("click", function () {
        currentBanner -= 1;
        $("#banner_div img").eq(currentBanner).fadeIn("slow");
    });

    $("#next").bind("click",function () {
        currentBanner += 1;
        $("#banner_div img").eq(currentBanner).fadeIn("slow");
    });

    $("#banner_div img").eq(currentBanner).css("display", "block");
});

HTML:

<a href="#" id="next>next</a>
<a href="#" id="prev">prev</a>
<div id="banner_div">
    <img src="image1.jpg" alt="" />
    <img src="image2.jpg" alt="" />
    <img src="image3.jpg" alt="" />
    <img src="image4.jpg" alt="" />
</div>

Right off the bat

<a href="#" id="next>next</a>

is missing the closing quote for your id attribute:

<a href="#" id="next">next</a>

Here's a jsFiddle demo of what I would do .

jQuery :

$(document).ready(function () {

    var $images = $("#banner_div img"),
        totalImages = $images.length,
        currentIndex = 0;

    $images.eq(currentIndex).fadeIn("slow");

    $("#prev").on("click", function() {
        if (currentIndex > 0) {
            $images.eq(currentIndex).stop(true,true).hide(0);
            currentIndex -= 1;
            $images.eq(currentIndex).stop(true,true).fadeIn("slow");
        }
    });

    $("#next").on("click",function () {
        if (currentIndex < totalImages-1) { 
            $images.eq(currentIndex).stop(true,true).hide(0);
            currentIndex += 1;
            $images.eq(currentIndex).stop(true,true).fadeIn("slow");
        }
    });

});​

CSS :

#banner_div img { display: none; }​

Compact version of Morteza's code:

$(document).ready(function () {
    var currentBanner = 0;
    $("#banner_div img").eq(currentBanner).show("fade");

    $("#prev").bind("click", function () {
        $("#banner_div img").eq(currentBanner--).hide("fade").prev().show("fade");
    });

    $("#next").bind("click",function () {
        $("#banner_div img").eq(currentBanner++).hide("fade").next().show("fade");
    });
});     

use methode $(selector).show("fade") and $(selector).hide("fade")

$(document).ready(function () {
    var currentBanner = 0;
    $("#banner_div img").hide();
    $("#banner_div img").eq(currentBanner).show("fade");

    $("#prev").bind("click", function () {
        $("#banner_div img").eq(currentBanner).hide("fade");
        decrease(currentBanner);
        $("#banner_div img").eq(currentBanner).show("fade");
    });

    $("#next").bind("click",function () {
        $("#banner_div img").eq(currentBanner).hide("fade");
        increase(currentBanner);
        $("#banner_div img").eq(currentBanner).show("fade");
    });

});     

when increase and decrease check currentBanner beetween 0 and banners count.

function increase(n){
    n++;
    if(n >= $("#banner_div img").length)
        n=0;
}
function decrease(n){
    n;
    if(n < 0 )
        n=$("#banner_div img").length -1;
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM