2

What I want to achieve is to make some of the divs in background scroll slower than the rest to make that parallax scrolling effect. I've found and modified this piece of jQuery code:

$(window).scroll(function () {

    $('.anim').css({
        'top': -($(this).scrollTop() / 3) + "px"
    });

});

and it works fine with one exception, my div has "top:200px" and as far as I understand the first time I scroll it resets top to 0 and the does its thing proper way. Here's the demo so you can see why it doesn't look ok, don't mind the "black world" cut, it's just a placeholder for now. When you scroll first time you can notice a "jump" to top:0. Is there anyway to not let it happen?

http://klaunfizia.pl/damian/

1 Answer 1

6

You're setting the absolute y position in your line

'top': -($(this).scrollTop() / 3) + "px"

You need to adapt it to the div' starting position

'top': 200-($(this).scrollTop() / 3) + "px"
Sign up to request clarification or add additional context in comments.

1 Comment

@user2660811 to make it easier, save the original position the first time you attempt to change it in a data attribute. Like if (!$(element).data('original-top')) $(element).data('original-top', $(element).css('top')); and then 'top': $(element).data('original-top')-($(this).scrollTop() / 3);

Your Answer

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.