嘿,我正在尝试将此视差代码实现为普通的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();
或者甚至是某事。我错过了? 提前谢谢
答案 0 :(得分:3)
可以使用
windowHeight = window.innerWidth;
并且对于scrollTop,您可以获得此链接的帮助