<分区>
编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将有助于其他人回答问题。
关闭 5 年前。
<分区>
编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将有助于其他人回答问题。
关闭 5 年前。
在这个网站
www.homepreneurawards.com
当点击导航栏菜单时,它不会滑动到特定的 div 内容可能是什么问题
<header class="header type-1">
<div class="container">
<div class="logo-block">
<a class="logo" href="#"><img src="http://homepreneurawards.com/Homepreneur/img/logo.gif" alt=""></a>
</div>
<nav class="main-nav">
<ul class="anchor-navigation">
<li><a class="anchor-scroll" href="#aboutbrandavatar">ABOUT</a></li>
<li><a class="anchor-scroll" href="#whoishomepreneur">HOMEPRENEUR ?</a></li>
<li><a class="anchor-scroll" href="#categories">CATEGORIES</a></li>
<li><a class="anchor-scroll" href="#jurymembers">JURY MEMBERS</a></li>
<li><a class="anchor-scroll" href="#timeline">PROCESS FLOW</a></li>
<li><a class="anchor-scroll" href="#organizers">ORGANIZERS</a></li>
<li><a class="anchor-scroll" href="#sponsorspartners">SPONSORS</a></li>
<li><a class="anchor-scroll" href="#partnerscontent">PARTNERS</a></li>
<li><a class="anchor-scroll" href="#contact">CONTACT US</a></li>
</ul>
</nav>
<button class="cmn-toggle-switch"><span></span></button>
</div>
</header>
最佳答案
问题出在你在 anchors.navigation.js 中的函数
function setImmediateAnchor(anchorObject, time) {
scrollFlag = 1;
if (currentAnchor) { -- THIS LINE
$('html,body').stop().animate({
'scrollTop': contentTop[anchorObject.attr('href')].top
}, time, function() {
$(scrollLinkSelector).parent().removeClass('active');
anchorObject.parent().addClass('active');
window.location.hash = '#/' + anchorObject.attr('href').substr(1);
currentAnchor = anchorObject.attr('href');
scrollFlag = 0;
});
}
}
条件 if (currentAnchor) 失败,因为 currentAnchor 为空。您已将其设置为 currentAnchor = window.location.hash.replace('/', '') 当您的页面加载时这将是空的,因为您在 http://www.homepreneurawards.com 之后的 url 中没有任何内容|
有 2 个您认为可行的解决方案。
在您的初始化代码下方添加一个条件,以检查 currentAnchor 是否为空,然后将其设置为某个值,例如“#initialized”,如下所示。
var contentTop = {},
contentOffset = 87,
currentAnchor = window.location.hash.replace('/', ''),
scrollFlag = 0,
scrollLinkSelector = '.anchor-scroll';
-- add this condition.
if(currentAnchor =='')
{
currentAnchor = '#initialized';
}
您可以简单地检查函数 setImmediateAnchor 中的空值,如下面第 3 行所示,并处理空值。
function setImmediateAnchor(anchorObject, time) {
scrollFlag = 1;
if (currentAnchor == '' || currentAnchor) { -- THIS LINE
$('html,body').stop().animate({
'scrollTop': contentTop[anchorObject.attr('href')].top
}, time, function() {
$(scrollLinkSelector).parent().removeClass('active');
anchorObject.parent().addClass('active');
window.location.hash = '#/' + anchorObject.attr('href').substr(1);
currentAnchor = anchorObject.attr('href');
scrollFlag = 0;
});
}
}
或者你可以使用任何其他可行的方式,核心问题在于不允许你滚动的情况,因为你的变量是空的。您可以采用您认为可行的任何一种方式进行修复。
关于javascript - onclick 滑动向下翻页不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44430064/