朋友们,
我正在构建一个单页网站,使用jQuery函数来滚动到锚点,当菜单链接被选中时。以下是我使用的代码:
(function($) {
var jump = function(e)
{
if (e) {
e.preventDefault();
var target = $(this).attr("href");
} else {
var target = location.hash;
}
$('html,body').animate(
{
scrollTop: $(target).offset().top - 150
}, 1500, 'swing', function()
{
location.hash = target - 150;
});
}
$('html, body').hide()
$(document).ready(function()
{
$('a[href^=#]').bind("click", jump);
if (location.hash) {
setTimeout(function() {
$('html, body').scrollTop(0).show()
jump()
}, 0);
} else {
$('html, body').show()
}
});
})(jQuery)
现在这个函数被用于所有带有“href”属性的html的a元素。我需要修改上述函数,使它适用于所有已定义的链接,但不包括这个带有锚点
#nav-menu
的链接: <a href="#nav-menu" id="toggle"><span></span></a>
任何建议都将不胜感激。
$('a[href^=#]:not(#toggle)').bind("click", jump);
, Also use.on()
- Satpal.on()
”是什么意思。但是,你评论的第一部分解决了我的问题。谢谢。 - user2963789.bind()
已被弃用,推荐使用.on()
方法,因此“_也使用.on()
_”旨在建议使用.on()
方法。 - Satpal