最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

animation - How to solve element flicker while scrolling with parallax effect in JavaScript? - Stack Overflow

programmeradmin2浏览0评论

I am trying to re-create website with parallax effect using JavaScript. That means that I have two or more layers, that are moving different speeds while scrolling.

In my case I'm moving only one layer, the other one remains static: layer 1 = website text; layer 2 = element background;

for this I'm using simple source code (with jQuery 1.6.4):

var docwindow = $(window);

function newpos(pos, adjust, ratio){
   return ((pos - adjust) * ratio)  + "px";
}

function move(){
   var pos = docwindow.scrollTop();
   element.css({'top' : newpos(pos, 0, 0.5)});
}

$(window).scroll(function(){
   move();
});

The Problem: - All calculations are done right and the effect "works" as expected. But there is some performance glitch under some browsers (Chrome MAC/Windows, Opera MAC, IE, paradoxically not Safari).

What do I see during scrolling? - While scrolling the background moves in one direction together with scroll, but it seems to occasionally jump few pixels back and then forth, which creates very disturbing effect (not fluid).

Solutions that I tried: - adding a timer to limit scroll events - using .animate() method with short duration instead of .css() method.

I've also observed, that the animation is smooth when using .scrollTo method (scrollTo jQuery plugin). So I suspect that there is something wrong with firing scroll events (too fast).

Have you observed the same behavior? Do you know, how to fix it? Can you post a better solution?

Thanks for all responses

EDIT #1: Here you can find jsfiddle demonstration (with timer): /

I am trying to re-create website with parallax effect using JavaScript. That means that I have two or more layers, that are moving different speeds while scrolling.

In my case I'm moving only one layer, the other one remains static: layer 1 = website text; layer 2 = element background;

for this I'm using simple source code (with jQuery 1.6.4):

var docwindow = $(window);

function newpos(pos, adjust, ratio){
   return ((pos - adjust) * ratio)  + "px";
}

function move(){
   var pos = docwindow.scrollTop();
   element.css({'top' : newpos(pos, 0, 0.5)});
}

$(window).scroll(function(){
   move();
});

The Problem: - All calculations are done right and the effect "works" as expected. But there is some performance glitch under some browsers (Chrome MAC/Windows, Opera MAC, IE, paradoxically not Safari).

What do I see during scrolling? - While scrolling the background moves in one direction together with scroll, but it seems to occasionally jump few pixels back and then forth, which creates very disturbing effect (not fluid).

Solutions that I tried: - adding a timer to limit scroll events - using .animate() method with short duration instead of .css() method.

I've also observed, that the animation is smooth when using .scrollTo method (scrollTo jQuery plugin). So I suspect that there is something wrong with firing scroll events (too fast).

Have you observed the same behavior? Do you know, how to fix it? Can you post a better solution?

Thanks for all responses

EDIT #1: Here you can find jsfiddle demonstration (with timer): http://jsfiddle/4h9Ye/1/

Share Improve this question edited Jan 25, 2012 at 18:20 Marakoss asked Jan 25, 2012 at 17:05 MarakossMarakoss 6231 gold badge11 silver badges25 bronze badges 3
  • 3 window.onscroll is jittery, it does not fire for every pixel moved. – epascarello Commented Jan 25, 2012 at 17:14
  • I think it isn't necessary to fire event for every pixel (that would be even more heavy). Is there any option that I can use instead? – Marakoss Commented Jan 25, 2012 at 17:25
  • "I suspect that there is something wrong with firing scroll events (too fast)." yes, you are right. – c69 Commented Jan 25, 2012 at 17:34
Add a ment  | 

1 Answer 1

Reset to default 11

I think you should be using scrollTop() instead and change the background position to fixed. The problem is that setting it to absolute will make it move by default when you scroll up or down.

The flicker occurs because the position is updated as part of the default browser scroll and updated again as part of your script. This will render both positions instead of just the one you want. With fixed, the background will never move unless you tell it so.

I've created a demo for you at http://jsfiddle/4h9Ye/2/ .

发布评论

评论列表(0)

  1. 暂无评论