gpt4 book ai didi

javascript - 从此代码中删除 Jquery

转载 作者:行者123 更新时间:2023-11-28 19:21:00 24 4
gpt4 key购买 nike

嘿,我正在尝试将这个视差代码实现为纯 js

$(document).ready(function(){

function draw() {
requestAnimationFrame(draw);
// Drawing code goes here
scrollEvent();
}
draw();

});

function scrollEvent(){

if(!is_touch_device()){
viewportTop = $(window).scrollTop();
windowHeight = $(window).height();
viewportBottom = windowHeight+viewportTop;

if($(window).width())

$('[data-parallax="true"]').each(function(){
distance = viewportTop * $(this).attr('data-speed');
if($(this).attr('data-direction') === 'up'){ sym = '-'; } else { sym = ''; }
$(this).css('transform','translate3d(0, ' + sym + distance +'px,0)');
});

}
}

function is_touch_device() {
return 'ontouchstart' in window // works on most browsers
|| 'onmsgesturechange' in window; // works on ie10
}

这就是我走了多远

(function() {

function draw() {
requestAnimationFrame(draw);
// Drawing code goes here
scrollEvent();
}
draw();

})();


function getElementsByAttribute(attribute, context) {
var nodeList = (context || document).getElementsByTagName('*');
var nodeArray = [];
var iterator = 0;
var node = null;

while (node = nodeList[iterator++]) {
if (node.getAttribute(attribute)) nodeArray.push(node);
}

return nodeArray;
}
function scrollEvent(){


viewportTop = $(window).scrollTop();
windowHeight = $(window).height();
viewportBottom = windowHeight+viewportTop;
els = getElementsByAttribute('data-parallax');

for (var i = 0; i < els.length; i++) {
distance = viewportTop * els[i].getAttribute('data-speed');
if(els[i].getAttribute('data-direction') === 'up'){ sym = '-'; } else { sym = ''; }
els[i].style.webkitTransform = "translate3d(0, " + sym + distance +"px,0)";

};


}

function is_touch_device() {
return 'ontouchstart' in window // works on most browsers
|| 'onmsgesturechange' in window; // works on ie10
}

所以基本上我需要替换这两行

    viewportTop = $(window).scrollTop();
windowHeight = $(window).height();

或者甚至有什么。我失踪了?提前致谢

最佳答案

对于窗口的高度,您可以使用

windowHeight = window.innerWidth;

对于scrollTop,您可以通过此链接获取帮助

Converting Jquery to Javascript

关于javascript - 从此代码中删除 Jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28914912/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com