Jquery DIY slider 多次使用问题

标签 jquery html css slider

我正在使用 DIY Slider,但有一个我无法处理的问题。当我尝试在一页中多次使用此 slider 时,第二个不会运行。

我尝试了所有可能的想法。

$(".slider").diyslider({
    width: "400px", // width of the slider
    height: "200px", // height of the slider
    display: 1, // number of slides you want it to display at once
    loop: false // disable looping on slides
}); // this is all you need!

// use buttons to change slide
$("#go-left").bind("click", function(){
    // Go to the previous slide
    $(".slider").diyslider("move", "back");
});
$("#go-right").bind("click", function(){
    // Go to the previous slide
    $(".slider").diyslider("move", "forth");
});

http://jsfiddle.net/bj4yZ/155/ - 这是工作中的“不工作”演示。

最佳答案

您所要做的就是更改每个元素的 ID。如果您不这样做,而只是复制并粘贴 html 和 js,所有事件都将附加到第一个 slider 。

http://jsfiddle.net/bj4yZ/788/

<button id="go-left">&laquo;</button> <button id="go-right">&raquo;</button>

<div class="slider"><!-- The slider -->
    <div><!-- A mandatory div used by the slider -->
        <!-- Each div below is considered a slide -->
        <div class="a">Div #1</div>
        <div class="b">Div #2</div>
        <div class="c">Div #3</div>
        <div class="d">Div #4</div>
        <div class="e">Div #5</div>
    </div>
</div>

<button id="go-left2">&laquo;</button> <button id="go-right2">&raquo;</button>

<div class="slider2"><!-- The slider -->
    <div><!-- A mandatory div used by the slider -->
        <!-- Each div below is considered a slide -->
        <div class="a">Div #1</div>
        <div class="b">Div #2</div>
        <div class="c">Div #3</div>
        <div class="d">Div #4</div>
        <div class="e">Div #5</div>
    </div>
</div>

和js

$(".slider").diyslider({
    width: "400px", // width of the slider
    height: "200px", // height of the slider
    display: 3, // number of slides you want it to display at once
    loop: false // disable looping on slides
}); // this is all you need!

// use buttons to change slide
$("#go-left").bind("click", function(){
    // Go to the previous slide
    $(".slider").diyslider("move", "back");
});
$("#go-right").bind("click", function(){
    // Go to the previous slide
    $(".slider").diyslider("move", "forth");
});



$(".slider2").diyslider({
    width: "400px", // width of the slider
    height: "200px", // height of the slider
    display: 3, // number of slides you want it to display at once
    loop: false // disable looping on slides
}); // this is all you need!

// use buttons to change slide
$("#go-left2").bind("click", function(){
    // Go to the previous slide
    $(".slider2").diyslider("move", "back");
});
$("#go-right2").bind("click", function(){
    // Go to the previous slide
    $(".slider2").diyslider("move", "forth");
});

希望对您有所帮助 ;)

关于Jquery DIY slider 多次使用问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26133063/

相关文章:

html - 让div填满剩余空间

jquery - Bootstrap 3 更新会在 Bootstrap 对话框打开或关闭时摇动 UI。我怎样才能摆脱这种行为?

html - 如何使用 css 定位除最后一个元素之外的所有元素?

javascript - 几秒钟后隐藏 iframe 内容

javascript - 为 iFrame 创建 float 水平滚动条

javascript - 生成选择列表项问题

javascript - React 中具有固定记录数的多行

jquery - 如何将两个 jquery 对象包装成一个?

html - 具有自定义背景图像的单选按钮在 Internet Explorer 中变黑

php - 用PHP模拟 "Custom Quotes"