首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >jQuery滚动到Div

jQuery滚动到Div
EN

Stack Overflow用户
提问于 2018-02-08 00:41:33
回答 2查看 0关注 0票数 0

我正在制作一个FAQ页面,并在顶部有按钮跳转到一个类别(跳转到p我用作类别标签的标签,例如<p id="general">我的常规类别)。而不是只是跳到类别,我想添加滚动效果。我想像http://www.dynamicdrive.com/dynamicindex3/scrolltop.htm滚动到我的网页的所需部分。该链接是一个脚本,带有一个不错的滚动效果的页面顶部。我需要类似的东西,将滚动到我链接到的地方。例如,如果我想去混杂。类别,我想只能够拥有<a href="#misc">Miscellaneous</a>并滚动页面的那一部分。

EN

回答 2

Stack Overflow用户

发布于 2018-02-08 08:48:51

代码语言:txt
复制
$(function() {
  $('a[href*=#]:not([href=#])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
      if (target.length) {
        $('html,body').animate({
          scrollTop: target.offset().top
        }, 1000);
        return false;
      }
    }
  });
});
票数 0
EN

Stack Overflow用户

发布于 2018-02-08 10:25:41

它是经常需要同时移动body和html对象。

代码语言:txt
复制
$('html,body').animate({
   scrollTop: $("#divToBeScrolledTo").offset().top
});

ShiftyThomas是对的:

代码语言:txt
复制
$("#divToBeScrolledTo").offset().top + 10 // +10 (pixels) reduces the margin.

因此:

代码语言:txt
复制
$("#divToBeScrolledTo").offset().top - 10 // -10 (pixels) would increase the margin between the top of your window and your element.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100003474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档