简体   繁体   English

滚动效果不适用于Chrome

[英]Scroll Effect doesn't work on chrome

I have a scroll effect that changes the colour of the background by finding the users position on the page and using that to set the colour. 我有一个滚动效果,可以通过在页面上找到用户位置并使用它来设置颜色来更改背景的颜色。 This works fine on safari and in Atom Html Previewer however when I checked it on chrome it didn't work. 这在safari和Atom Html Previewer中工作正常,但是当我在chrome上检查它不起作用时。 My website is harry-day.con 我的网站是harry-day.con

Any suggestions on why it doesn't work or how to fix it? 关于为什么它不起作用或如何解决的任何建议? I tried modifying the JS slightly but nothing worked. 我尝试稍微修改JS,但没有任何效果。

Here is the JavaScript I am using: 这是我正在使用的JavaScript:

function scroll(){
    var body = document.body,
    html = document.documentElement;
    var height = html.scrollHeight - html.clientHeight;
    var color = Math.round((body.scrollTop / height) * 255);
    body.style.backgroundColor = "rgb("+color+","+color+","+color+")";
}

Edit: I tried using var color = Math.round((html.scrollTop / height) * 255); 编辑:我尝试使用var color = Math.round((html.scrollTop / height) * 255); however now it does not work when I tested it locally in Safari. 但是现在当我在Safari中本地测试时,它不起作用。

You can check this for a cross-browser way of getting current scroll position: 您可以检查为获取当前滚动位置的跨浏览器的方式:

var doc = document.documentElement;
var top = (window.pageYOffset || doc.scrollTop)  - (doc.clientTop || 0);

 function scroll(){ var body = document.body; var doc = document.documentElement; var top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0); var height = doc.scrollHeight - doc.clientHeight; var color = Math.round((top / height) * 255); body.style.backgroundColor = "rgb("+color+","+color+","+color+")"; } window.addEventListener('scroll', scroll); 
 body { height: 1400px; } 

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

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