滚动到特定div

时间:2016-06-03 07:23:51

标签: javascript html

我试图弄清楚当页面加载时页面是如何自动滚动到特定div的。我已经尝试使用jQuery滚动功能,但无法正常工作。有什么建议吗?

2 个答案:

答案 0 :(得分:1)

我快速搜索了你,这样的东西能满足你的需求吗?

$(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;
      }
    }
  });
});

在此处找到:https://css-tricks.com/snippets/jquery/smooth-scrolling/

你也可以在那里测试一下!

答案 1 :(得分:-1)

您可以在jQuery中尝试类似下面的内容: -

$('html,body').animate({
    scrollTop: $(".targetDiv").offset().top},
'slow');
相关问题