firefox中的Jquery动画错误

Jquery animate bug in firefox

本文关键字:错误 动画 Jquery 中的 firefox      更新时间:2023-09-26

我使用这段代码,但好像偏移量从来都不好。偏移的目的是:我的菜单栏保持在顶部。所以当我点击菜单项时,它会滚动到顶部。减去菜单的高度。但我总是有一个小故障,再次滚动。

在chrome中可以正常工作,但在firefox和IE中不行。清楚了吗?

 $('html, body').animate({
       scrollTop: $(this.hash).offset().top - menu.offsetHeight
     }, 300, function(){
       window.location.hash = hash;
     });

.animate()方法允许我们在任何数值CSS属性

上创建动画效果

所以scrollTop不是CSS属性,它是jQuery函数。如果它是相对/绝对定位元素,则将其更改为margin-top, padding-toptop

我终于明白了。

不是scrollTop

:

window.location.hash = hash;

在IE和Chrome中,当分配一个新值给hash属性时,它的滚动到它。