【发布时间】:2014-03-23 19:22:31
【问题描述】:
我不得不通过给它一个绝对位置并用 jquery 的 scrollTop() 给它一个最高值来伪造一个 div 在其容器内但相对于窗口的固定位置。到目前为止,这似乎是一个不错的解决方案,但固定效果仅适用于 Chrome。在 Firefox 和 ie10 中它移动缓慢,在 ie9 中它会做一些类似振动的事情
if ($.browser.webkit) {
//First I had to do some hack in order to get the scrollTop() same return in all browsers
var bodyPos = $('body').scrollTop();
}else{
var bodyPos = $('html, body').scrollTop();
}
//then I can calculate the point relative to the top of the window
var pos1 = $('#four').position().top;
var imgPos = bodyPos - pos1
$('#fixed1').css({'top': imgPos})
有人知道如何使这种效果跨浏览器吗?
谢谢
【问题讨论】:
-
您是否正在对滚动处理程序进行去抖动处理?
-
不要重新发明轮子——看看Tether
-
@Bojangles 我试过 man,但没有成功,哈哈。 Tether 做的正是我需要的,你是对的。我只是不想加载更多脚本。不过我时间很紧,所以我会用它,谢谢。
-
@StephenThomas 感谢您的建议,这可能是我应该做的,我会为下一个项目尝试它,这将是一个很好的教训。
-
@Bojangles 我不能让它工作,在这种情况下你会选择什么元素作为目标、容器、前一个 div?
标签: javascript jquery html fixed scrolltop