Multiple sliders on one page

本小妞迷上赌 提交于 2019-12-08 09:12:08

问题


Ok, I builded a slider in javascript and Jquery (with help of you guys) But now I want to have multiple sliders on 1 page. While using just one javascript. BUT...the slider can be different in width (or number of items): also the name of the slider is different because of the css width.

So How do I use 1 javascript to controle different sliders

Here is my code:

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <script src="http://code.jquery.com/jquery-latest.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<style type="text/css">
#temp{
height: 300px;
}

#container{
    width: 500px;
    height: 150px;
    background:#CDFAA8;
    overflow:hidden;
    position:absolute;
    left: 13px;
    }

#slider{
    width: 800px;
    height: 150px;
    background:#063;
    position:absolute;
    left: 0px;
}

#block1{
    width: 100px;
    height: 150px;
    background:#067;
    float: left;
}

#block2{
    width: 100px;
    height: 150px;
    background:#079;
    float: left;
}

#move_right{
    height: 150px;
    width: 20px;
    background: #3f3f3f;
    position: absolute;
    right:0px;
    z-index: 200;
    opacity: 0.2;
}

#move_left{
    height: 150px;
    width: 20px;
    background: #3f3f3f;
    position: absolute;
    left:0px;
    z-index: 200;
    opacity: 0.2;
}​
</style>
</head>

<body>
<div id="temp">
<div id="container">
    <div id="move_left"><button id="right">&laquo;</button></div><div id="move_right"><br><br><button id="left">&raquo;</button></div>
<div id="slider">

    <div id="block1">1</div>    
    <div id="block2">2</div>
    <div id="block1">3</div>    
    <div id="block2">4</div> 
    <div id="block1">5</div>    
    <div id="block2">6</div>
    <div id="block1">7</div>    
    <div id="block2">8</div>


</div>
</div>
</div>


<div id="slider">

    <div id="block1">1</div>    
    <div id="block2">2</div>
    <div id="block1">3</div>    
    <div id="block2">4</div> 
    <div id="block1">5</div>    
    <div id="block2">6</div>
    <div id="block1">7</div>    
    <div id="block2">8</div>


</div>
</div>
</div>

JavaScript

(function($) {
    var slider = $('#slider'),
        step = 500,
        left = parseInt(slider.css('left'), 10),
        max = $('#container').width() - slider.width(),
        min = 0;

    $("#left").click(function() {
        if (left > max) {
            var newLeft = left - step;
            left = (newLeft>max) ? newLeft : max;
            $("#slider").animate({
                "left": left + 'px'
            }, "slow");
        }
    });

    $("#right").click(function() {
        if (left < 0) {
            var newLeft = left + step;
            left = (newLeft<min) ? newLeft : min;
            slider.animate({
                "left": left + 'px'
            }, "slow");
        }
    });
})(jQuery);

回答1:


This should be fine:

(function($) {
    $('#temp #container').each(function(){
        var slider = $(this).find('#slider'),
        parent = $(this),
        step = 500,
        left = parseInt(slider.css('left'), 10),
        max = parent.width() - slider.width(),
        min = 0;

    parent.find("#left").click(function() {
        if (left > max) {
            var newLeft = left - step;
            left = (newLeft>max) ? newLeft : max;
            slider.animate({
                "left": left + 'px'
            }, "slow");
        }
    });

    parent.find("#right").click(function() {
        if (left < 0) {
            var newLeft = left + step;
            left = (newLeft<min) ? newLeft : min;
            slider.animate({
                "left": left + 'px'
            }, "slow");
        }
    });
});
})(jQuery);​

FIDDLE




回答2:


In theory you could do some code which can take a selector to a wrapper element (which has the required slider elements inside) as some parameter. And then you can from this element create selectors which are more dynamic. I'm not sure where you get "step = 500" from, but that's maybe something you could grab dynamically from some relevant element.



来源:https://stackoverflow.com/questions/9658840/multiple-sliders-on-one-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!