2012-11-05 136 views
0

我还在学习JavaScript并需要一些帮助。我正在制作下一个和上一个按钮,导致滚动到页面上的下一个或上一个<td>'s。我正在试图将这个实现到virb站点的现有结构。 标记看起来是这样的:下一个/上一个按钮onClick滚动到下一个和上一个​​的

div id="next">next</div><div id="prev">prev</div> 
<table> 
<tr><td>code that includes img</td></tr> 
<tr><td>code that includes img</td></tr> 
<tr><td>code that includes img</td></tr> 
<tr><td>code that includes img</td></tr> 
<tr><td>code that includes img</td></tr> 
</table> 

这是我的工作脚本:

jQuery.easing.def = "easeInOutQuad"; 
    $("#next").click(function(){ 
     //$(this).animate(function(){ 
      $('html, body').animate({ 
       scrollLeft: $("td").next.offset().left 
       }, 600); 
     //}); 

    });​ 

​Here is the jsFiddle我的工作

+0

这里是更新的小提琴http://jsfiddle.net/garretthaas/kzqVp/8/ – glh16

回答

0

以下内容会跟踪您当前要查看的项目。你需要一个类似的thig来减少'prev'例程中的索引。

您可能还需要添加延迟或事件截止点,以便动画过程中的多次点击不会将currentIdx的值取为高于或低于要显示的项目范围。

var currentIdx = 0; 
jQuery.easing.def = "easeInOutQuad"; 
     $("#next").click(function(){ 

      //$(this).animate(function(){ 
       $('html, body').animate({ 
        scrollLeft: $("td").eq(currentIdx).offset().left 
        }, 600); 
       currentIdx++;  
      //}); 
     }); 
+0

谢谢。我想我现在明白了。我将.eq(currentIdx)更改为.eq(currentIdx + 1),因为有一次返回第一项的点击。当我创建了递减的代码位时,它也起作用了。这里是更新代码的JSFiddle ...查看我编辑的任何问题? [http://jsfiddle.net/garretthaas/kzqVp/12/](http://jsfiddle.net/garretthaas/kzqVp/12/) – glh16

+0

小提琴似乎很适合我:-) –

+0

太棒了。再次感谢。 – glh16

1

尝试:

scrollLeft: $("td").next().offset().left 
+0

谢谢!工作,但只有一次点击。当我再次点击下一张图像时,它不会继续显示下一张图像。我会做更多的测试,但我仍然接受建议。 – glh16

+0

这是更新的小提琴jsfiddle.net/garretthaas/kzqVp/8 – glh16

相关问题