jQuery左右滚动按钮

时间:2013-08-29 19:38:42

标签: javascript jquery css

我一直试图解决这个问题几天,似乎无法取得任何进展。我正在尝试使用图像作为滚动条进行简单的左右滚动,但我似乎无法让它工作。这是我的代码:http://jsfiddle.net/7GrTM/1/

HTML

<div class="outerwrapper">
   <div class="innerwrapper">
     <div class="productsbox">
          <img class="box_image" src="images/productsbox1.png" style="width:222px" alt="this"/>
     </div>
     <div class="spacer"></div>
     <div class='productsbox'>  
          <img class='box_image' src="images/productsbox2.png" style='width:222px' alt="this"/>
     </div>
     <div class="spacer"></div>
     <div class='productsbox'>  
          <img class='box_image' src="images/productsbox3.png" style='width:222px' alt="this"/>                      
     </div>
     <div class="spacer"></div>
     <div class='productsbox'>  
          <img class='box_image' src="images/productsbox4.png" style='width:221px' alt="this"/>  
    </div>
    <div class="spacer"></div>
    <div class='productsbox'>  
         <img class='box_image' src="images/productsbox5.png" style='width:221px' alt="this"/>  
    </div>
    <div class="spacer"></div>
    <div class='productsbox'>  
         <img class='box_image' src="images/productsbox6.png" style='width:221px' alt="this"/>
    </div>
  </div>
</div>
    <div class="productspace">
        <img src="images/arrowleft.png" id="#left" alt="left"/>
        <img src="images/arrowright.png" id="#right" style="padding-left: 10px;" alt="right"/>
    </div>

JS

$(function () {

    $("#right, #left").click(function () {
        var dir = this.id == "right" ? '+=' : '-=';
        $("#outerwrapper").stop().animate({ scrollLeft: dir + '422' }, 1000);
    });

});

CSS

.spacer {
    width: 20px;
    height: 319px;
    display: inline-block;
}

.outerwrapper {
    margin: 0px auto;
    width: 1050px;
    height: 323px;
    display: inline-block;
    overflow: hidden;
}

.innerwrapper {
    width: 1600px;
    height: 322px;
    margin: 0 auto;
    display: inline-block;
    overflow: hidden;
}

任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:1)

您的ID错误,您已将其命名为#left#right,而不仅仅是leftright。在您的JSfiddle中,您已设法将其设置为ID:#left1#right1

解决方案:将ID更改为leftright,这样您就可以:

<div class="productspace">
    <img src="images/arrowleft.png" id="left" alt="left"/>
    <img src="images/arrowright.png" id="right" style="padding-left: 10px;" alt="right"/>
</div>

您还尝试将outerwrapper作为id,而不是class访问您的div。将外部包装器更改为ID <div id="outerwrapper">或更改jquery以查找类:$(".outerwrapper")(先前的解决方案在我看来是优越的,因此更改div id。)