Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 滚动链接到另一页';s剖面平滑_Javascript_Jquery_Html_Dom - Fatal编程技术网

Javascript 滚动链接到另一页';s剖面平滑

Javascript 滚动链接到另一页';s剖面平滑,javascript,jquery,html,dom,Javascript,Jquery,Html,Dom,我有index.php,它有不同的部分,如top、service、aboutus和contactus。我有头文件。php在includes文件夹中,我将它分别包含到每个文件中。我为部分添加了平滑滚动的jquery 现在我有了具有相同标题的gallery.php。现在我想调用gallery到index/section的每个部分。因此,我将href=“#服务”更改为href=index.php#服务。现在它可以从索引和图库页面工作 问题: 如果我从index.php调用该部分,平滑滚动就可以工作。当

我有
index.php
,它有不同的部分,如
top
service
aboutus
contactus
。我有
头文件。php
在includes文件夹中,我将它分别包含到每个文件中。我为部分添加了平滑滚动的jquery

现在我有了具有相同标题的
gallery.php
。现在我想调用gallery到index/section的每个部分。因此,我将
href=“#服务”
更改为
href=index.php#服务
。现在它可以从索引和图库页面工作

问题: 如果我从
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

调用,您会遇到一些javascript代码冲突。你必须在代码中进行调试。我在这里添加了jquery代码。请检查。你的意思是我必须在页脚或超链接中添加脚本文件吗?我在页脚中有相同的链接。它也像页眉一样工作。