简体   繁体   English

$(“body”)。scrollTop()在Safari中不会更新

[英]$(“body”).scrollTop() doesn't update in safari

I'm working on a website: 我在网站上工作:

http://beta.projektopia.se/ http://beta.projektopia.se/

the body has several background-images that are updated on scroll like this: 正文有几个背景图像,在滚动上更新如下:

$(document).ready(function(){

    $(document).scroll(function(){
        var scrollfactor=$("body").scrollTop()*0.2;
        var centerscrollpos =scrollfactor+613;
        var docheight = $(document).height();
        var windowheight = $(window).height();
        var bottompos = (docheight-980)-((docheight-windowheight)*0.2)+scrollfactor;
        var scrollpos = 'center '+scrollfactor+'px,center '+bottompos+'px, center '+ centerscrollpos+'px,center 0px';
        $("body").css("background-position", scrollpos);
    });
});

Lots of calculations, but the important thing is that a scrollpos is created that should change the position of the background when you scroll, to create a parallax-effect. 大量的计算,但重要的是创建了一个scrollpos,它应该在滚动时改变背景的位置,以创建视差效果。 It works great in chrome, but in firefox, the variable scrollfactor, that is suppose to get the current scroll-position, doesn't update. 它在chrome中工作得很好,但在firefox中,变量scrollfactor(假设获取当前滚动位置)不会更新。

ps, some people have this issue due to lack of correct doctype. ps,由于缺乏正确的doctype,有些人会遇到此问题。 I believe i have declared it correctly like this: 我相信我已经正确地宣布它:

<!DOCTYPE html>

The scrollTop jQuery method has been known to be problematic . 已知scrollTop jQuery方法存在问题

Depending on the browser, you may need to use $('html, body').scrollTop() or $(document).scrollTop() or $(window).scrollTop() . 根据浏览器的不同,您可能需要使用$('html, body').scrollTop()$(document).scrollTop()$(window).scrollTop()

For me, the problem was any kind of overflow property on html element. 对我来说,问题是html元素上的任何overflow属性。 The moment I removed it, my .scrollTop() started working as it should. 我删除它的那一刻,我的.scrollTop()开始正常工作。 I still wanted global overflow-x: hidden property on my page, so I just set it on body element. 我仍然想在我的页面上使用全局overflow-x: hidden属性,所以我只需将它设置在body元素上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM