我有一些原始的JavaScript代码,我希望它们可以在jquery中重写。那我该怎么做呢?
(function(){
var parallax = document.querySelectorAll(".parallax"),
speed = 0.1;
window.onscroll = function(){
[].slice.call(parallax).forEach(function(el,i){
var windowYOffset = window.pageYOffset,
elBackgrounPos = "100% " + (windowYOffset * speed) + "px";
el.style.backgroundPosition = elBackgrounPos;
});
};
})();
答案 0 :(得分:1)
这是一个jsfiddle:
https://jsfiddle.net/mckinleymedia/d3qt5p94/2/
代码:
var parallax = $(".parallax"),
speed = 0.1;
window.onscroll = function(){
parallax.css('backgroundPosition','0 ' + (window.pageYOffset * speed) + "px");
};