【发布时间】:2017-11-05 01:28:53
【问题描述】:
我们正在构建一个单页浏览器网站,其中包含产品组合项目的详细信息页面。现在我们在主页上的菜单滚动到该页面上的内容。
所以我有这样的菜单项。
<ul>
<li><a href="section-1">Section 1</a></li>
<li><a href="section-2">Section 2</a></li>
<li><a href="section-3">Section 3</a></li>
<li><a href="section-4">Section 4</a></li>
</ul>
内容 div 是这样构建的
<div id="section-1">Content</div>
现在这是我正在使用的 jquery,它在主页上运行良好
// Function: Scroll navigation to row
$('.navbar a').on('click', function(e) {
e.preventDefault();
var $row = $($(this).attr('href'));
$('html, body').animate({
scrollTop: ($row.offset().top - menuHeight - 78)
}, 1000);
});
所以现在,当我在详细信息页面上时,我只想单击任何菜单项,然后返回到正确内容的主页。
但是点击它不起作用,我收到了这个错误。
Uncaught TypeError: Cannot read property 'top' of undefined
at HTMLAnchorElement.<anonymous> (website.js:26)
at HTMLAnchorElement.dispatch (jquery.js:3)
at HTMLAnchorElement.v.handle (jquery.js:3)
我已经尝试了很多东西,将绝对路径添加到 url,但我想解决这个问题,因为它不会滚动到内容..
【问题讨论】:
标签: jquery html wordpress scroll menu