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
jQuery平滑滚动并链接到其他页面_Jquery_Laravel_Anchor_Smooth Scrolling - Fatal编程技术网

jQuery平滑滚动并链接到其他页面

jQuery平滑滚动并链接到其他页面,jquery,laravel,anchor,smooth-scrolling,Jquery,Laravel,Anchor,Smooth Scrolling,这是我的菜单结构: HTML 通过这种方式,我可以平滑地滚动到页面的特定部分(在本例中为高亮显示) 它工作正常,但当我进入联系人页面时: www.mywebsite.com/contact 当我点击highlights时,它不会返回主页,而是简单地将参数添加到URL: www.mywebsite.com/contact#highlights 当然,因为亮点在主页上。 如果我为这样的高光创建链接: <a href="/#highlights">Highlights</a&g

这是我的菜单结构:

HTML

通过这种方式,我可以平滑地滚动到页面的特定部分(在本例中为高亮显示)

它工作正常,但当我进入联系人页面时:

www.mywebsite.com/contact 
当我点击highlights时,它不会返回主页,而是简单地将参数添加到URL:

www.mywebsite.com/contact#highlights
当然,因为亮点在主页上。 如果我为这样的高光创建链接:

<a href="/#highlights">Highlights</a>
  $('a[href="/#highlights"]').click(function() {
    $('html, body').animate({
        scrollTop: $("/#highlights").offset().top
    }, 1000);
  });
但它也不起作用

编辑。我用的是拉威尔5.5

EDIT2:Gezzasa发布了一个绝妙的想法,我做了一些调整

为了正常工作,主页应该有一个额外的类,因此下一个jQuery代码应该像它一样工作。 假设我在主页上添加了类“front”,并使用Gezzasa的思想,这是正常工作的:

HTML


我通过改变一些东西来修复它

我在a标记上添加了一个名为
数据锚点
的数据参数。你可以随便叫它什么

然后我将id放在那里,这样我就可以完全忽略href并防止
a
标记的默认行为

然后,在加载dom后,我使用一个侦听器将click事件添加到元素中

现在,如果锚定在页面上,它将平滑滚动


我希望我已经回答了你的问题,否则我可能理解错了。可能有更好的解决方案。这对我很有用。

如果将
/#highlights
更改为
/index.html#highlights
滚动条:$(“/#highlights”).offset().top},1000),会发生什么
不起作用,因为
/#highlights
是jquery的无效选择器。我的网站是Laravel,所以如果我改为/index.php#highlights,它将转到页面的特定部分,但没有任何平滑滚动。/index.php#highlights和/#highlights的作用相同。这是一个好主意,但并不完整,因为当我,我在另一页,它不会带我回去。我找到了解决方案,我会编辑我的主线程,你可以编辑你的答案,我会接受它。请在3分钟后再次查看帖子。好的,现在你可以编辑你的答案了。非常感谢你!
<a href="/#highlights">Highlights</a>
  $('a[href="/#highlights"]').click(function() {
    $('html, body').animate({
        scrollTop: $("/#highlights").offset().top
    }, 1000);
  });
<nav>
  <ul>
    <li><a href="/#highlights" data-anchor="#highlights">Highlights</a></li>
    <li><a href="/gallery">Gallery</a></li>
    <li><a href="/contact">Contact</a></li>
  </ul>
</nav>

<div id="highlights">
  //some content inside for the highlights
</div>
  $('.front [data-anchor]').click(function(e) {
    e.preventDefault();
    var goToAnchor = $(this).attr("data-anchor");
    $('html, body').animate({
      scrollTop: $(goToAnchor).offset().top
    }, 1000);
  });