因此,我正在尝试在文档中获取一些视差滚动。所以我做到了:
window.onscroll = function() {scrollPost()};
function scrollPost () {
var i = window.scrollY;
blockShift(i);
console.log(i);
}
function blockShift (i){
var blockOne = document.querySelector(".block-1");
var blockTwo = document.querySelector(".block-2");
var y = i - 900
if (i < 980){
blockOne.style.transform = `translateY(${y/3}px)`
blockTwo.style.transform = `translateY(${y/-3}px)`
}
}
但是,我想知道是否有一种更准确的方法来获取数字?问题之一是scrollY数取决于屏幕尺寸而变化。我也尝试过。
var x = window.screen.width;
var i = window.scrollY;
var number = Math.round(i/x);
哪个给我一个0.xxxxxxx数字。我怎么会更好呢?
编辑:这也很好
var x = window.screen.width;
var i = window.scrollY;
var a = i / x;
var numb = Math.round(a * 10) / 10;