与 FF 和 IE 相比,jQuery 在 Chrome 中返回错误的元素位置
我正在编写一个函数来根据我存储在数组中的预定义元素位置来测试滚动条位置。页面上可以有任意数量的这种元素类型,这就是为什么我使用 for 循环来测试滚动条位置。
这段代码在 Firefox 中工作正常,但由于某种原因,jQuery 在 Chrome 中为我的每个 .post-info ($pi) 项目返回完全不同的位置,甚至 $(document).height 错误。这是我的 javascript 还是 css 的问题?我将假设我的 JS 因为我已经在 css 中的大多数元素上测试了我的position:relative 。
这是我正在运行的 jQuery:
(function(){
var i,
st,
length,
arrPos = []
$wn = $(window);
$pi = $('.post-info');
$pi.each(function(){
arrPos.push($(this).offset().top - 80); // offset -80 so new item sticks quicker.
});
height = $(document).height();
arrPos.push(height); // add bottom position of document to array for last item.
length = arrPos.length;
$wn.scroll(function(){ //Scroll Event Listener
st = $wn.scrollTop();
for(i=0; i<=length; i++){
// i = index, n = next
n = i+1;
if(st > arrPos[i] && st < arrPos[n]) _stick(i);
}
});
非常感谢。
I'm writing a function to test the scroll bar position against predefined element positions I have stored in an array. There can be any number of this element type on the page, which is why I'm using a for loop to test my scroll bar position.
This code works fine in Firefox, but for some reason jQuery is returning completely different positions for each of my .post-info ($pi) items in Chrome, and is even getting the $(document).height wrong. Is this an issue with my javascript, or with my css? I'll assume my JS as I've tested my position:relative on most elements in my css.
Here is the jQuery I'm running:
(function(){
var i,
st,
length,
arrPos = []
$wn = $(window);
$pi = $('.post-info');
$pi.each(function(){
arrPos.push($(this).offset().top - 80); // offset -80 so new item sticks quicker.
});
height = $(document).height();
arrPos.push(height); // add bottom position of document to array for last item.
length = arrPos.length;
$wn.scroll(function(){ //Scroll Event Listener
st = $wn.scrollTop();
for(i=0; i<=length; i++){
// i = index, n = next
n = i+1;
if(st > arrPos[i] && st < arrPos[n]) _stick(i);
}
});
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
谷歌浏览器在 $(document).ready 之后设置高度和宽度
如果您有任何图像,那么您的页面尺寸在这一特定时刻将不正确。尝试首先预加载图像,完成后调用您的函数。
希望这有帮助:)
Google Chrome set the height and width after $(document).ready
If you have any images, than your page dimensions won't be the right one at this particular moment. Try to preload your images first and when it's completed, than call your function.
Hope this help :)