[英]jQuery scroll to anchor with exception
朋友们,
我正在构建单页网站,当选择菜单链接时,它使用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>
任何建议将非常感激。
Jquery提供了一组内置过滤器,您可以在您使用的情况下使用它们:
内置过滤器not()如下: -
$("a[href^=#]:not([href=#nav-menu])").click(jump);
按如下方式构建您自己的业务过滤器: -
$("a[href^=#]").filter(function() { //you may here do whatever filteration business you want return $(this).attr("href")!='#nav-menu'; }).click(jump);
这里简单的例子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.