我正在使用使用jquery錨鏈接將頁面動畫化爲錨點的較舊腳本。它效果很好,但我需要它只是動畫製作特定div的內容,而不是整個頁面。有關如何定位div而不是頁面的想法?jquery鏈接到動畫滾動div而不是整個頁面
$(document).ready(function() {
$("a.anchorLink").anchorAnimate()
});
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;
})
})
}
就像一般性評論一樣,依靠Javascript的自動分號插入通常是一個糟糕的主意,這是通常被認爲是壞事的語言的「特徵」,如果你不是小心。明確行結束將有助於防止錯誤。如果你真的不喜歡行結尾,也許可以嘗試使用CoffeeScript,而替代語法不需要行結尾,但它生成的JS將是有效的,並且在正確的位置有行結束。 – GregL 2012-01-15 23:51:18