jQuery scrollTop跨浏览器不一致 [英] jQuery scrollTop inconsistent across browsers

查看:85
本文介绍了jQuery scrollTop跨浏览器不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Chrome和Safari中, $(body)。scrollTop(1000)符合预期。

In Chrome and Safari, $("body").scrollTop(1000) goes where expected.

在IE和FF中,没有任何反应。

In IE and FF, nothing happens.

在IE和FF中, $(窗口).scrollTop(1000)工作,但他们去不同的地方。它也可以在Chrome和Safari中使用,但它们也可以在不同的地方使用。它们似乎关闭了300-500像素。

In IE and FF, $(window).scrollTop(1000) works, but they go to different places. It also works in Chrome and Safari, but they both go to a different place as well. They seems to be up to 300-500 pixels off.

是否有任何一致的方法来设置跨浏览器工作的scrollTop属性,如果没有,为什么不jQuery抽象这个吗?

Is there any consistent way to set the scrollTop property that works cross browser, and if not, why doesn't jQuery abstract this?

我也希望为它制作动画,这在Chrome和Safari中运行良好,但在其他浏览器中没有。

I'd like to animate it as well, which works fine in Chrome and Safari, but not in the other browsers.

我是浏览器检测的唯一选择吗? (不好的做法)还是有更好的方法?

Is my only option to do browser detection? (bad practice) Or is there some better way?

推荐答案

$(jQuery.browser.webkit? body:html)。animate({scrollTop:myTop},myDur);

Webkit浏览器(Chrome / Safari,两者兼而有之) Mac和Win)使用body,其他人(FF / Opera / IE 7-9)使用html

Webkit browsers (Chrome/Safari, both Mac and Win) use "body", others (FF/Opera/IE 7-9) use "html"

喜欢浏览器检测。

这篇关于jQuery scrollTop跨浏览器不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆