f1era
f1era

Reputation: 13

How can I add this scroll function?

I need to implement a scroll function in my site.
When I will scroll down, document will be scrolled until a fixed position, which I want.
For example:

$(“.scroll-block”).scrollTop(250);

I realized this function. Pseudo-code:

when (scrollTop > 0)
do (scrollTop(250))

But then scroll is fixed. And any scroll-actions doesn’t work.
But I have three block. And I want to keep the possibility to scroll to all three blocks. How can I do that?

P.S.: sorry for my terrible English. It’s my first post on this platform for communication.
P.S.S.: and first topic on English language.

Upvotes: 0

Views: 41

Answers (1)

Vj-
Vj-

Reputation: 722

Register a scroll callback like so:

jQuery(window).scroll(scrollCallback);

In the callback you can get the scroll position and do whatever you want at specific heights.

function scrollCallback(){
    //gets the current windows scroll top value
    var scrollPos = jQuery(window).scrollTop();

    //get the position of the top of the elements you want to trigger actions on.
    var first = jQuery('the-element-you-want-to-select1').position().top;
    var second = jQuery('the-element-you-want-to-select2').position().top;

    if(scrollPos > first){
        //do something for first height
    }else if(scrollPos > second){
        //do something for second height
    }
    ..
    ..
}

Upvotes: 1

Related Questions