原來的插件:
jQuery.fn.anchorAnimate = function(settings) {
settings = jQuery.extend({
speed : 1100
}, settings);
return this.each(function(){
var caller = this
$(caller).click(function (event) {
event.preventDefault()
var locationHref = window.location.href
var elementClick = $(caller).attr("href")
var destination = $(elementClick).offset().top;
$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
window.location.hash = elementClick
});
return false;
})
})
}
修改:
jQuery.fn.anchorAnimate = function(settings) {
settings = jQuery.extend({
speed : 1100,
offset: 0
}, settings);
return this.each(function(){
var caller = this
$(caller).click(function (event) {
event.preventDefault()
var locationHref = window.location.href
var elementClick = $(caller).attr("href")
var destination = $(elementClick).offset().top;
$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination+settings.offset}, settings.speed, function() {
window.location.hash = elementClick
});
return false;
})
})
}
請注意,我只是增加了一個offset
選項。所以,如果你的固定div有60像素的高度,然後只需用$('#whatever').anchorAnimate({offset: 60});
謝謝!!!!我試圖通過添加一個靜態值,例如60像素。但我認爲「window.location.hash = elementClick」是問題所在。 – Vivid 2012-07-22 07:27:19
您的更改不能解決問題。 :-( – Vivid 2012-07-22 07:37:24