Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intelligent Scrolling

I was wondering, how some modern websites know, whether I am scrolling. And if so, where "I am" at the moment on the page and then perform specific functions.

For example the page 9gag.com. On the right side is a green button "Y U No Signup?!", when I scroll further down, so that this button disappears, something similar appears on the top of the page.

Another example is the title of every of the pictures. When I'm looking at them and scrolling down, the title "follows me", but only until reaching the end of the particular picture, then it stops.

like image 990
Evgenij Reznik Avatar asked Aug 04 '26 23:08

Evgenij Reznik


2 Answers

its easy.. for example with jquery you can call a function within .scroll() triggering whenever you start scrolling... just use .offset() from top or bottom and you have you position and can fire whatever you want

like image 141
ggzone Avatar answered Aug 06 '26 14:08

ggzone


For a similar task I created a small jQuery plugin called «fixedAfter» The main idea behind is to set a static position for such objects and then dynamically changing their position on window scroll event, smthg like

$.fn.fixedAfter = function(pos) {
    var $this = this, $window = $(window), obj;
    $window.bind('scroll.fixedAfter', function(e) {
        obj = ($window.scrollTop() < pos)
            ? { position: 'static' } /* or { position: absolute, top: pos } */
            : { position: 'fixed', top: 0 }
        $this.css(obj);
    });

    /* if a page is loaded by an internal link (e.g. page.html#internal-id) */
    $(document).ready(function() {
        $window.trigger('scroll.fixedAfter');
    })
};

so if you want to fix a button on the top of the viewport, let say after 100px of scroll, just call the plugin with

$('button').fixedAfter(100);
like image 29
Fabrizio Calderan Avatar answered Aug 06 '26 12:08

Fabrizio Calderan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!