Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - Smooth parallax scrolling with mouse wheel

I have a page where I'm applying a parallax effect. This is accomplished using translate3d. Now, while this works well, I'm wondering how I can override the default "steps" when scrolling with the mouse wheel?

If I scroll with the scrollbars, everything is fine. But with the mouse wheel, it's all jumpy.

I'm doing this in a pretty straight forward way:

    var prefix = Modernizr.prefixed('transform');
    $window.on('scroll', function(){
        var scroll_top = $window.scrollTop();
        if(scroll_top < forside_infographics_offset){
            $_('#slider').css(prefix , "translate3d(0,"+(scroll_top/3)+"px,0)");
        }

    });

Now, I've seen this site where the scrolling is super smooth, also with a mouse wheel with steps on it. I've tried to look at the code, and he's using requestAnimationFrame is seems, but how he accomplish this excact scrolling effect, I'm not sure.

http://cirkateater.no/

Any ideas?

like image 279
Kenny Bones Avatar asked Dec 12 '13 11:12

Kenny Bones


People also ask

What is smooth scrolling JavaScript?

Today, we will explore how smooth scrolling works on the web by building a smooth scrolling library from scratch that will have the following features: zero dependencies. animations with cubic Bézier curves and easing presets — the most interesting part! ability to scroll inside any element and not just window.

Is parallax scrolling accessible?

As aesthetically pleasing parallax scrolling can be, the feature does have some drawbacks. For people suffering from vestibular sensitivities (motion sensitivities or visual vertigo), parallax scrolling can be as debilitating as being spun on a playground roundabout; the effect causes nausea and dizziness.

Does parallax scrolling work on mobile?

Parallax scrolling works relatively pain free in Android devices, albeit a little choppy as Android stutters to a certain degree the execution of code defined inside any onscroll event to conserve battery life. In iOS devices like on a iPad or iPhone, this conservation effort is taken to new heights.


2 Answers

After doing a lot of research, I found a pretty easy solution :) http://bassta.bg/demos/smooth-page-scroll/

Interestingly enough, I didn't have to alter my existing code at all. This overrides the default scroll behaviour, while leaving the event open for me to use like I would normally do.

EDIT: This is a really bad idea. Never ever hijack and override expected behavior. I guess I was overly fascinated with animations back then and overdid everything. Thankfully we all learn and expand our perceptions of good UX principles :)

like image 57
Kenny Bones Avatar answered Oct 05 '22 14:10

Kenny Bones


Scrolling using the mouse wheel requires special handling. Reason being each mouse wheel scroll doesn't scroll the content by a certain amount of pixels. Each scroll causes your page to jump and then each jump results in the "jumpy" jittery animation as the background image is trying to position itself at these jumps.

Using a library will solve the problem most of the time, but it is also worth understanding what problems it is trying to solve under the hood.

Just for reference sakes, the mouse events are mousewheel and DOMMouseScroll

like image 40
pbojinov Avatar answered Oct 05 '22 15:10

pbojinov