[英]Uncaught TypeError: Cannot read property 'top' of undefined - Scrolling error?
Here is my HTML: 这是我的HTML:
<ul class="nav-list">
<li class="current"><a class="smoothscroll" href="#home" title="">Home</a></li>
<li><a class="smoothscroll" href="signup.html" title="">Sign up</a></li>
<li><a class="smoothscroll" href="signin.html" title="">Sign in</a></li>
</ul>
Here is my JavaScript: 这是我的JavaScript:
var ssSmoothScroll = function() {
$('.smoothscroll').on('click', function (e) {
var target = this.hash,
$target = $(target);
e.preventDefault();
e.stopPropagation();
$('html, body').stop().animate({
'scrollTop': $target.offset().top
}, cfg.scrollDuration, 'swing').promise().done(function () {
// check if menu is open
if ($('body').hasClass('menu-is-open')) {
$('#header-menu-trigger').trigger('click');
}
window.location.hash = target;
});
});
};
I trawled through several answers to this and still cannot find why mine isn't working. 我浏览了几个答案,但仍然找不到为什么我的系统无法正常工作。 Any help is much appreciated.
任何帮助深表感谢。
I want to continue on to either signup.html or signin.html but neither of the links work if I click them. 我想继续访问signup.html或signin.html,但是如果单击它们,则两个链接均不起作用。 They only work if I right click and open in new tab.
仅当我右键单击并在新选项卡中打开时,它们才起作用。
The only reason offset
will return undefined
is if you call it on an empty jQuery set. offset
返回undefined
的唯一原因是如果您在空的jQuery集合上调用它。 So that tells us that $(target)
is returning an empty jQuery set, presumably because target
contains a selector that doesn't match any elements. 这样就告诉我们
$(target)
返回一个空的jQuery集,大概是因为target
包含一个与任何元素都不匹配的选择器。
You probably want to add a guard to allow for that: 您可能想要添加一个保护措施以允许这样做:
if (!$target.length) {
return;
}
...just after $target = ...
. ...就在
$target = ...
之后
Looking at your HTML, though: You probably just want to remove the smoothscroll
class from the links that take you to another page (the second two in your example). 不过,
smoothscroll
一下HTML:您可能只想从将您带到另一个页面(示例中的后两个)的链接中删除smoothscroll
类。 The only purpose they seem to have is to make jumping to anchors within the page scroll rather than jump. 他们似乎唯一的目的是使跳转到页面滚动中的锚点而不是跳转。 Since those links are off-page, I'd just not put the class on them in the first place.
由于这些链接不在页面上,因此我只是不会将课程放在第一位。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.