簡體   English   中英

如何平滑滾動到div id?

[英]How to smooth scroll to div id?

我試圖設置要在單擊菜單中的div時要滾動到的div。 問題是滾動到div並沒有滾動到代碼中的div,它總是滾動到頁面上的其他地方,很奇怪。 我正在使用下面的代碼:

$(".dns-anchor").click(function() {
    $('html, body').animate({
        scrollTop: $("#dns-scroll").offset().top
    }, 2000);
});

ID#dns-scroll是我要滾動到的div。

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

要么

$("#button").click(function() {
    $('html, body').animate({
        scrollTop: $("#myDiv").offset().top
    }, 2000);
});

請檢查此鏈接:- 平滑滾動到div id jQuery

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM