$("a.scroll").click(function(){
if(this.hash){
//get rid of hash
var hash = this.hash.substr(1);
//get the position of <a name>
var $toElement = $("a[name="+hash+"]");
var toPosition = $toElement.offset().top - 250;
//scroll/animate that element
$("body,html").animate({
scrollTop : toPosition
},1000,"easeOutExpo");
return false;
}
});
if(location.hash){
var hash = location.hash;
window.scroll(0,0);
$("a[href="+hash+"]").click();
}
当我尝试匹配名称字符串并将其滚动到隐藏在标题后面的位置时,我的标题已固定..在滚动时..
并且在调整窗口大小时也面临问题,标题需要额外的空间并且滚动隐藏后面的内容..如何修复它//
答案 0 :(得分:0)
<a>
元素没有hash
成员。在发布之前检查控制台是否有任何错误。您需要获取href
属性的内容。你可能需要这样的东西:
$("a.scroll").click(function() {
var href = $(this).attr("href");
if (href.indexOf("#") === 0) {
//get rid of hash
var hash = href.substr(1);
//get the position of <a name>
var $toElement = $("a[name=" + hash + "]");
var toPosition = $toElement.offset().top - 250;
//scroll/animate that element
$("body,html").animate({
scrollTop: toPosition
}, 1000, "easeOutExpo");
return false;
}
});