我正在尝试使用div id =“”的平滑滚动功能,但不是滚动它,而是捕捉到元素。

http://jsfiddle.net/T6F6v/

$(document).ready(function() {

$('a[href*=#]').bind("click", function(e) {

var target = $(this).attr("href"); //Get the target
var scrollToPosition = $(target).offset().top;

$('html').animate({ 'scrollTop': scrollToPosition }, 500, function(target){
    window.location.hash = target;
});

e.preventDefault();
});


我想念什么吗?

最佳答案

$(document).ready(function() {缺少结尾})。就这样。

编辑:关于Calamari答案的对话表明,$('html,body').animate({...对于跨浏览器兼容性是必需的。 Firefox和IE仅响应html,而Chrome仅响应body

关于javascript - JS平滑滚动不起作用(仍在捕捉),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17868787/

10-16 19:27