【问题标题】:Jquery scrollTop() with offset?带有偏移量的Jquery scrollTop()?
【发布时间】:2014-04-15 14:48:02
【问题描述】:

我正在使用 jquery scrollTop(),但我遇到了一些问题

这是 HTML

<a class="jumper" href="#first">Jump</a>

<div class="first"></div>
<div id="second"></div>
<div id="third"></div>
<div id="fourth"></div>
<div id="fifth"></div>

还有 Jquery

  $(document).ready(function () {

        $('.jumper').click(function () {

            $('html, body').animate({
                scrollTop: $("#fourth").offset().top
            }, 2000);

        });


    });

它工作正常,但我需要的不是将元素#third 滚动到页面顶部,只是将其滚动到大约 100px 小大约几个像素以下,因为我想在前一个元素中留下一些东西以供查看to,有可能吗?

这是工作小提琴

http://jsfiddle.net/X9FUg/4/

我想让黄色元素显示在 100 像素左右?

【问题讨论】:

  • $("#fourth").offset().top - 100?

标签: jquery scroll


【解决方案1】:

请注意,可能有更好的解决方案...但首先想到的就是减去 100。

$("#fourth").offset().top - 100

offset().top 返回一个没有 'px' 部分的数字,所以 offset().top - 100 应该可以正常工作。 (https://api.jquery.com/offset/)

【讨论】:

    【解决方案2】:

    是的,请尝试以下解决方案,它会为您工作。在这里,您可以设置任何您想要的值,而不是 100。

        $(document).ready(function () {
            $('.jumper').click(function () {
                $('html, body').animate({
                  scrollTop: $("#fourth").offset().top - 100
                }, 2000);
            });
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多