aaron_martone — 2013-03-06T19:34:12-05:00 — #1
Long story short, I have a slider with a next/prev on the edge of the window in the vertical center of a page. For visual element clipping reasons, I'd like to make the item disappear when the element is X pixels (or less) from the top of the window when users start to scroll down.
I'm guessing I would need to bind something to the scroll of the body to check the distance, and if too low, fade the elements out.
paul_wilkins — 2013-03-06T19:49:56-05:00 — #2
A good way to achieve that is to set up a timer that checks the distance on a regular basis, such as on a 500ms basis.
aaron_martone — 2013-03-06T20:02:33-05:00 — #3
Is that considered a safe amount of time that wouldn't burden most browsers/computer setups?
paul_wilkins — 2013-03-06T21:33:26-05:00 — #4
Yes, half a second is plenty of time to not burdon a browser. They can even handle 50ms, but for the task that you have in mind, 500ms (half a second) is plenty enough. Feel free to increase it though to whatever you feel comfortable with. By experimenting with different delay speeds, you can check on how that will affect things with the effect that you want to apply.
jimmybrion — 2013-03-07T05:39:16-05:00 — #5
My opinion would be to bind event on scroll, something like :
before that unbind the event : $(window).unbind("scroll");
paul_wilkins — 2013-03-07T05:40:52-05:00 — #6
Using the onscroll event would result in extreme performance problems, because that function will be called every single time that the page scrolls. It's been known to grind IE to a halt, for example.
pullo — 2013-03-07T05:57:16-05:00 — #7
So are you saying that it's computationally less expensive to fire a function that checks an element's position (where a reference to the element is cached) every 500ms, than to attach an onscroll event handler to the main window?
paul_wilkins — 2013-03-07T05:59:47-05:00 — #8
It would be interesting to see a performance comparison of the different techniques when something is being faded in and out, perhaps with http://jsperf.com/
pullo — 2013-03-07T06:04:42-05:00 — #9
The reason I ask this, is because I had to do something similar recently and wasn't sure of the right way to go.
I contemplated both techniques and ended up using onscroll.
paul_wilkins — 2013-03-25T17:39:56-04:00 — #10
What worries me about using onscroll, is in regard to the fading technique that is going to be firing its own set of timers to perform the fade. That seems to be a recipe for things becoming quite messy when scrolling.
Also, whenever you are scrolling, the event onscrolling event is triggering as fast as it can on the computer, which can be every 20 milliseconds. (50 times a second).
Contrasting that with checking every 500 ms (twice a second) seems to be preferable in a situation that doesn't require precise timing, but I could be wrong.
It would be interesting to have a test page of what the OP is intending to fade, so that different techniques could be explored and experimented with.
logic_ali — 2013-03-25T19:23:35-04:00 — #11
I deal with that simply by comparing the time between calls. If the difference is too small, don't execute the handler's code.
pullo — 2013-03-28T08:39:13-04:00 — #12
Any chance you could provide us with a link to the page with the slider?