3

When a user scrolls past a div I need it's css changed to position:fixed. Much like what is done here: http://imakewebthings.com/jquery-waypoints/shortcuts/sticky-elements/ but with just plain jquery, without the plugin.
The div must also stop scrolling once the user scrolls to another div.
For example:

<div id="stuff"></div>
<div id="this"></div>//must start scrolling with user when user reaches it.
<div id="footer"></div>// when #this reaches within say, 10px, of #footer it must stop in it's current position, in order to prevent overlap

I'm assuming i'd use .scroll() and .css(), but I have no idea where to go from there.

0

3 Answers 3

8

I noticed in your answer #this disappears abruptly once you get down to #footer. I've tweaked your answer to allow for #this to stick to #footer and scroll which is a lot smoother.

My demo uses slightly different markup and is a little more verbose so jump over to jsFiddle and check it out.

Demo: jsfiddle.net/Marcel/e9hyw/

Sign up to request clarification or add additional context in comments.

1 Comment

Please note, my demo uses the throttle/debonce plugin which is recommended when the scroll() event is being utilised.
3

This is what finally worked for me:

    jQuery(document).ready(function() {
    var topOfrel = jQuery("#this").offset().top;
    var topOffooter = jQuery("#footer").offset().top - jQuery(window).height(); 
    var topOffootero = topOffooter ;
    var boxheight = jQuery(window).height() - 130;//adjusting for position below
    jQuery(window).scroll(function() {
        var topOfWindow = jQuery(window).scrollTop();
        if (topOfrel < topOfWindow && topOffooter > topOfWindow) {
            jQuery("#this").css("position", "fixed").css("top", "130px").css("overflow","auto").css("height", boxheight + "px");

        } else {
            jQuery("#this").css("position", "static");
        }
    });
});

Comments

0

the page you reference uses jQuery:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js">

and also has other Javascript included on it that we don't see

incidentally you can view it here:

http://imakewebthings.com/jquery-waypoints/waypoints.js
http://imakewebthings.com/jquery-waypoints/shortcuts/sticky-elements/waypoints-sticky.js

the functions he uses can be seen there, but i would not say it's a quick answer. Based on his scripts he attaches a listener to the element after the page loads:

<script type="text/javascript">
    $(document).ready(function() {
        $('.my-sticky-element').waypoint('sticky');
    });
</script>

If you have specific questions ask away, but i doubt many will go through explaining the entire script for you.

2 Comments

I was looking for jquery only option, with no add-on.
The central difference here is that the code you are looking for will either be included inline as a large block, or saved in a separate javascript file (or addon) of your own. Technically his is not not an addon but just his code to perform what you're looking for. You're looking for a listener on a Dom element that tracks current y position and if less than 0 on y set an absolute position. again, the best answer I could give was to look at the functions in his "addon" and see if you can replicate just the part you want.

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.