ilonacodes
Patreon
Investor Profile
Contact

Financial tips and advice on investment for developers:
"Learn how to free extra monthly cash from your dev-salary for investment"

There are so many different ways to free money for investments without thinking of getting another job. Download my Top-15 cash-freeing tips cheat sheet.

Get my 15 Tips to Invest More Cash Monthly!
10
Mar
2018
#scrolling
#parallax
#javascript
#frontend
#learntocode
#animate
#coding
#letsgetcoding
#womenwhocode

Front-end Shorts: How to Create Parallax Effect With Vanilla JS

10 Mar, 2018

Hi, there! I hope you enjoy your weekend, and ready for a new post.

Recently there are a lot of sites, which have featured with parallax scrolling effect, that represents them in more interactive “professionally-made” way.

This fact made me recreate the effect using Vanilla JavaScript, an example of which you can see below.

Let’s a look at the implementation of parallax effect in Vanilla JavaScript.

I have used a ‘scroll’ event on browser ‘window,’ and retrieved the number of pixels the document is currently scrolled along the vertical axis from ‘window.pageYOffset’.

The constant ‘background’ consists of the image for which we are going to apply the parallax effect.

Finally, I compute the scrolling interval in pixels for the ‘top’ attribute.


    window.addEventListener('scroll', function (e) {
        // will not work on IE < 9
        var scrolled = window.pageYOffset;
        // will not work on IE < 8
        const background = document.querySelector('.background');
        background.style.top = - (scrolled * 0.2) + 'px';
    });

Tune the coefficient ‘0.2’ to your liking.

If you liked the article, it would make me happy if you shared it on your favourite social networks!

If you have any questions, suggestions or recommendations feel free to reach out to me on Twitter.

Happy coding!

Tags | #scrolling #parallax #javascript #frontend #learntocode #animate #coding #letsgetcoding #womenwhocode
Next: Front-end Shorts: How to Scroll to Element in jQuery With Animation

My name is Ilona, and I'm a software engineer, founder, and blogger who is (besides programming) also passionate about startups, finance and investment

"I like to work hard for things that are worth it."
The latest articles:
Numbrs Review - App For Personal Finances (Germany & UK only)
How Developers Can Get Symbols From Stock Indexes With Python
Bitcoin Profit Calculator For Developers

Imprint
Privacy Policy
© 2021 ilonacodes
© 2021 ilonacodes
Imprint
Privacy Policy