0

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
4
  • what does your javascript do? Have you started the convertion? Commented Dec 12, 2015 at 5:53
  • It is used for making background image parallax while scrolling Commented Dec 12, 2015 at 5:54
  • Why would you want to make it slower by converting it to jQuery? Commented Dec 12, 2015 at 6:02
  • Easy and maintainable code with less lines Commented Dec 12, 2015 at 6:15

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks a lot William :)
No problem. Glad I could help.
I've another issue can you pls find me a solution for it?
Sure. What do you need?
You want this in the clickme: $(this) .hide() .parent() .find('.show') .show();
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.