繁体   English   中英

顺利滚动链接到另一页面的部分

[英]Scroll link to another page's section smoothly

我有index.php ,它具有不同的部分,如topserviceaboutuscontactus 。我的header.php位于include文件夹中,并将其分别包含到每个文件中。

现在我有了具有相同标题的gallery.php ,现在我想调用从图库到索引/部分的每个部分。 因此,我将href="#service"更改为href=index.php#service 。现在,它既可以在索引页面又可以在图库页面使用。

问题:如果我从index.php调用该部分,则可以顺利滚动。 当我从gallery.php调用一个部分时,它只是加载了index.php的上部并突然移动到被调用的部分,我希望它能平稳地移动,如何实现呢?

$(document).ready(function(){   
  $("a").on('click', function(event) {

    if (this.hash !== "") {
      // Prevent default anchor click behavior
      event.preventDefault();

      // Store hash
      var hash = this.hash;

      // Using jQuery's animate() method to add smooth page scroll
      // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
      $('html, body').animate({
        scrollTop: $(hash).offset().top
      }, 800, function(){

        // Add hash (#) to URL when done scrolling (default click behavior)
        window.location.hash = hash;
      });
    } // End if
  });
});

您的代码看起来不错,但是您必须创建footer.php并从footer.php调用函数,这样您的问题才能得到解决。

当我使用时,我们必须从footer调用所有与jquery相关的东西,但这取决于您的要求,因此在这种情况下,您可以从footer.php调用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM