我有以下功能:
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
},2000,function()
{
location.hash = target;
});
}
$('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();
}
});
允许滚动到特定的div元素
<div id="anchor_element">blabla</div>
如果我点击链接:
<a href="#anchor_element">Scroll here</a>
我希望上述功能只有在html链接中才包含特定的rel属性,例如“myscroll”:
<a href="#anchor_element" rel="myscroll">Scroll here</a>
如何在此模式下修改工作功能? 感谢
答案 0 :(得分:3)
尝试Attribute Equals Selector [name="value"]
$('a[href^=#][rel="myscroll"]').bind("click", jump);
<小时/> Has Attribute Selector [name]
如果您希望它与rel
属性
$('a[href^=#][rel]').bind("click", jump);
<小时/> 更新
$('a[href^=#],a[rel="myscroll"]').bind("click", jump);
//^ works for both a start with # and with rel="myscroll"