【问题标题】:Next/Prev buttons onClick scroll to next and previous <td>'s下一个/上一个按钮 onClick 滚动到下一个和上一个 <td> 的
【发布时间】:2012-10-25 13:34:12
【问题描述】:

我仍在学习 JavaScript,需要一些帮助。我正在制作下一个和上一个按钮,这些按钮会导致滚动到页面上的下一个或上一个&lt;td&gt;。我正在尝试将其应用于 virb 站点的现有结构。 标记看起来像这样:

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我正在努力

【问题讨论】:

标签: javascript button scroll next


【解决方案1】:

试试:

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

【讨论】:

  • 谢谢!这有效,但只需单击一下。当我第二次单击下一个时,它没有继续到下一个图像。我会做更多的测试,但我仍然愿意接受建议。
【解决方案2】:

以下内容会跟踪您当前要查看的项目。您需要类似的方法来减少“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++;     
            //});
        });

【讨论】:

  • 谢谢。我想我现在明白了。我将 .eq(currentIdx) 更改为 .eq(currentIdx + 1) 因为有一次点击返回了第一个项目。当我创建递减的代码时它也起作用了。这是带有更新代码的 JSFiddle... 看到我的编辑有任何问题吗? http://jsfiddle.net/garretthaas/kzqVp/12/
猜你喜欢
  • 2013-02-14
  • 1970-01-01
  • 2015-07-09
  • 1970-01-01
  • 2013-12-14
  • 2017-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多