I've some raw JavaScript code and I want them to be re-written in jquery. So how do I do it?
(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;
});
};
})();
asked Dec 12, 2015 at 5:52
Firnas
3892 gold badges9 silver badges24 bronze badges
-
what does your javascript do? Have you started the convertion?depperm– depperm2015年12月12日 05:53:46 +00:00Commented Dec 12, 2015 at 5:53
-
It is used for making background image parallax while scrollingFirnas– Firnas2015年12月12日 05:54:34 +00:00Commented Dec 12, 2015 at 5:54
-
Why would you want to make it slower by converting it to jQuery?Mottie– Mottie2015年12月12日 06:02:20 +00:00Commented Dec 12, 2015 at 6:02
-
Easy and maintainable code with less linesFirnas– Firnas2015年12月12日 06:15:24 +00:00Commented Dec 12, 2015 at 6:15
1 Answer 1
Here's a jsfiddle:
https://jsfiddle.net/mckinleymedia/d3qt5p94/2/
And the code:
var parallax = $(".parallax"),
speed = 0.1;
window.onscroll = function(){
parallax.css('backgroundPosition','0 ' + (window.pageYOffset * speed) + "px");
};
answered Dec 12, 2015 at 7:11
William Schroeder McKinley
7686 silver badges15 bronze badges
Sign up to request clarification or add additional context in comments.
7 Comments
Firnas
Thanks a lot William :)
William Schroeder McKinley
No problem. Glad I could help.
Firnas
I've another issue can you pls find me a solution for it?
William Schroeder McKinley
Sure. What do you need?
William Schroeder McKinley
You want this in the clickme: $(this) .hide() .parent() .find('.show') .show();
|
lang-js