Freeze element (position:fixed) for specific scroll range

Collarbone

I'm working on this site (http://styleguide.co/medhelp/) that has 5 sections. For one of the sections (Styles), I've got a sidenav I'm trying to get to stick in the visible frame only as long as users are scrolling in that section.

Here's what I've done thus far - I'm telling the section title & sidenav to stick after the top of the section has begun:

$(window).scroll(function(event) {

    var sw = $('.fixed'),
    pg = $('.styles'),
    diff = pg[0].offsetTop - window.pageYOffset;

    if (diff < 80 ) {
        $('.fixed').css('position', 'fixed');
        $('.fixed').css('top', '160px');
        $('.styles').css('position', 'fixed');
        $('.styles').css('top', '70px');

    }
    else {
        $('.fixed').css('position', 'relative');
        $('.fixed').css('top', '0px');
        $('.styles').css('position', 'relative');
        $('.styles').css('top', '0px');
    }
});

I can't seem to figure out a good way to make the section title "Style" and the sidenav appear/disappear while I scroll to/from that section. Any advice? What could I do better? A simple solution demo in jsfiddle would really help!

Please click on this link & scroll down/up to know what I'm referring to: http://styleguide.co/medhelp/

Jake Rowsell

I'm not going to give you a fiddle, but you need to determine when the next section would stick based on its offset from the top. At the moment what you are doing is:

// if difference top and element < 80 -> fix to top, else position is relative

First of all this means the condition will never be undone. What you need to do in order to continue is:

// once next contact section comes into screen 
//(offset from the top of the screen <= screen height), give 
var winHeight = $(window).height();
var calcTop = 80 - (winHeight - (winHeight - $('#nextSelector').offset().top);
$('.fixed').css('top', calcTop);

This will give the illusion of your text scrolling up as the new section comes up. I hope this helps. Also, when scrolling back up it doesn't re-stick, but you probably are aware of that.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Detect when static element overlaps fixed element position on scroll

From Dev

scroll property in a child element of a parent div whose position is fixed

From Dev

Keep element position fixed to parent using transform on scroll

From Dev

javascript for position fixed on scroll

From Dev

position fixed in child element but sync parents x scroll with child elements left position

From Dev

jQuery position DIV fixed on scroll

From Dev

Scroll to anchor position with fixed header

From Dev

JQuery Scroll with position:fixed CSS

From Dev

div image position fixed + scroll

From Dev

Scroll within a fixed position overlay

From Dev

Define fixed position from scroll

From Dev

Smooth scroll issue with position:fixed;

From Dev

scroll and position: fixed inside overflow: scroll div

From Dev

Scroll AndroidStaggeredGrid to specific position

From Dev

Recyclerview scroll to a specific position

From Dev

Element position on scroll

From Dev

Position a fixed element below another fixed element

From Dev

Fixed position element inside absolute position element

From Dev

Fixed position element inside absolute position element

From Dev

How to do hide and show element on specific page position on scroll always when getting to that place with jquery on scroll

From Dev

Set specific fixed range JFreechart

From Dev

position fixed element is overlapping into footer

From Dev

Firefox position fixed element flash

From Dev

How to scroll div with page without position: fixed?

From Dev

Child of position: fixed; won't scroll

From Dev

Position fixed and overflow-y:scroll issue

From Dev

jQuery position DIV fixed at top on scroll

From Dev

Position fixed sidebar with overflow scroll not scrolling to bottom

From Dev

How to get position:fixed; behavior with scroll bar

Related Related

  1. 1

    Detect when static element overlaps fixed element position on scroll

  2. 2

    scroll property in a child element of a parent div whose position is fixed

  3. 3

    Keep element position fixed to parent using transform on scroll

  4. 4

    javascript for position fixed on scroll

  5. 5

    position fixed in child element but sync parents x scroll with child elements left position

  6. 6

    jQuery position DIV fixed on scroll

  7. 7

    Scroll to anchor position with fixed header

  8. 8

    JQuery Scroll with position:fixed CSS

  9. 9

    div image position fixed + scroll

  10. 10

    Scroll within a fixed position overlay

  11. 11

    Define fixed position from scroll

  12. 12

    Smooth scroll issue with position:fixed;

  13. 13

    scroll and position: fixed inside overflow: scroll div

  14. 14

    Scroll AndroidStaggeredGrid to specific position

  15. 15

    Recyclerview scroll to a specific position

  16. 16

    Element position on scroll

  17. 17

    Position a fixed element below another fixed element

  18. 18

    Fixed position element inside absolute position element

  19. 19

    Fixed position element inside absolute position element

  20. 20

    How to do hide and show element on specific page position on scroll always when getting to that place with jquery on scroll

  21. 21

    Set specific fixed range JFreechart

  22. 22

    position fixed element is overlapping into footer

  23. 23

    Firefox position fixed element flash

  24. 24

    How to scroll div with page without position: fixed?

  25. 25

    Child of position: fixed; won't scroll

  26. 26

    Position fixed and overflow-y:scroll issue

  27. 27

    jQuery position DIV fixed at top on scroll

  28. 28

    Position fixed sidebar with overflow scroll not scrolling to bottom

  29. 29

    How to get position:fixed; behavior with scroll bar

HotTag

Archive