当用户单击“联系我”按钮时,我希望屏幕滑动到#contact 元素,但不知道如何操作。我尝试了各种不同的代码片段并尝试根据我的需要对其进行调整,但似乎没有任何效果。
网站在这里; http://dombracher.com/
只是想让屏幕滑动到上面提到的div,而不是快速捕捉到它。
提前致谢。
最佳答案
$(document).ready(function() {
$("a[href^='#']").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 或 jQuery 滑动到元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14287482/