Stop jQuery fixed position scrolling when bottom of scrolling element reaches end of parent element

dabra904

I'm using the below jQuery (answered by James Montagne) to begin fixed position scrolling after the user scrolls down 250px so that the element stays fixed to the top of the browser.

$(window).scroll(function(){
    $("#theFixed").css("top",Math.max(0,250-$(this).scrollTop()));
});

QUESTION: In addition, I'd like to stop the fixed position scrolling when the bottom of the scrolling fixed position element comes to the end of the parent div element. This way preventing the fixed position element from overflowing the parent element and getting cutoff.

My example code is here: http://jsfiddle.net/b43hj/2020/

<div>
    <div id="theFixed" style="position: fixed; top: 250px; background-color: red">
        0 SOMETHING<br />
        1 SOMETHING<br />
        2 SOMETHING<br />
        3 SOMETHING<br />
        4 SOMETHING<br />
        5 SOMETHING<br />
        6 SOMETHING<br />
        7 SOMETHING<br />
        8 SOMETHING<br />
        9 SOMETHING<br />
        10 SOMETHING<br />
        11 SOMETHING<br />
        12 SOMETHING<br />
        13 SOMETHING<br />
        14 SOMETHING<br />
        15 SOMETHING<br />
        16 SOMETHING<br />
        17 SOMETHING<br />
        18 SOMETHING<br />
        19 SOMETHING<br />
        20 SOMETHING<br />
        21 SOMETHING<br />
        22 SOMETHING<br />
        23 SOMETHING<br />
        24 SOMETHING<br />
        25 SOMETHING<br />
        26 SOMETHING<br />
        27 SOMETHING<br />
        28 SOMETHING<br />
        29 SOMETHING<br />
    </div>
    THIS IS A LONG SENTENCE THAT GOES PAST THE RED SOMETHINGS
    <br>
    THIS IS A LONG SENTENCE THAT GOES PAST THE RED SOMETHINGS
    <br>
    ... END
</div>

Screenshot: screenshot of issue

(Add-on question to this original question and answer: Stopping fixed position scrolling at a certain point?)

Miguel Jose R. Raudez

To prevent the fixed element from overflowing the parent element, I present this solution. Example code

$(window).scroll(function(){
    var scroll_top = $(this).scrollTop(); // get scroll position top
    var height_element_parent =  $("#theFixed").parent().outerHeight(); //get high parent element
    var height_element = $("#theFixed").height(); //get high of elemeneto
    var position_fixed_max = height_element_parent - height_element; // get the maximum position of the elemen
    var position_fixed = scroll_top < 250 ? 250 - scroll_top : position_fixed_max > scroll_top ? 0 : position_fixed_max - scroll_top;
    $("#theFixed").css("top",position_fixed);
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Stop jQuery fixed position scrolling when bottom of scrolling element reaches end of parent element

From Dev

How to Stop Jumping Position fixed element on scrolling

From Dev

How to Stop Jumping Position fixed element on scrolling

From Dev

Fixed position element is flashing in Chrome when scrolling

From Dev

jQuery Stop Scrolling at the last element

From Dev

Stop fixed element scrolling at certain point?

From Dev

Stop fixed element scrolling at certain point?

From Dev

Overflow scrolling not working on position fixed element iOS

From Dev

How to stop a UIScrollView from scrolling when it reaches the bottom

From Dev

Open bottom sheet when sibling scrolling reaches the end?

From Dev

Scrolling window to the bottom of an element

From Dev

scrolling an element on top of a fixed element

From Dev

Jquery Scrolling Element to Position of Newly Appended Element (Calculating relative offset to parent)

From Dev

How to keep an html element fixed at the bottom of the window while resizing, but not at scrolling?

From Dev

How to keep <li> elements fixed top and bottom of <ul> element when scrolling

From Dev

stop fixed element from scrolling into the footer when the browser window is a smaller height

From Dev

iOS Safari issue - Element becomes invisible while scrolling when changing position absolute to fixed

From Dev

How to make an element fixed but relative to it's parent on scrolling only?

From Dev

How to position a child element at the bottom of a fixed position parent

From Dev

Position fixed sidebar with overflow scroll not scrolling to bottom

From Dev

JQuery - Prevent scrolling when an element is shown

From Dev

Jumping element by element when scrolling

From Dev

Absolute element becomes fixed when vertical scrolling past it

From Dev

JQuery smooth scrolling to one element when clicking a corresponding element

From Dev

Add class when element hits top of page AND remove class when element reaches bottom of parent

From Dev

The three first Element in custom ListView are repeated and change position when scrolling

From Dev

JQuery Position:Fixed 'NAVBAR' by scrolling the page

From Dev

JQuery Position:Fixed 'NAVBAR' by scrolling the page

From Dev

jquery scrollto not scrolling to correct element

Related Related

  1. 1

    Stop jQuery fixed position scrolling when bottom of scrolling element reaches end of parent element

  2. 2

    How to Stop Jumping Position fixed element on scrolling

  3. 3

    How to Stop Jumping Position fixed element on scrolling

  4. 4

    Fixed position element is flashing in Chrome when scrolling

  5. 5

    jQuery Stop Scrolling at the last element

  6. 6

    Stop fixed element scrolling at certain point?

  7. 7

    Stop fixed element scrolling at certain point?

  8. 8

    Overflow scrolling not working on position fixed element iOS

  9. 9

    How to stop a UIScrollView from scrolling when it reaches the bottom

  10. 10

    Open bottom sheet when sibling scrolling reaches the end?

  11. 11

    Scrolling window to the bottom of an element

  12. 12

    scrolling an element on top of a fixed element

  13. 13

    Jquery Scrolling Element to Position of Newly Appended Element (Calculating relative offset to parent)

  14. 14

    How to keep an html element fixed at the bottom of the window while resizing, but not at scrolling?

  15. 15

    How to keep <li> elements fixed top and bottom of <ul> element when scrolling

  16. 16

    stop fixed element from scrolling into the footer when the browser window is a smaller height

  17. 17

    iOS Safari issue - Element becomes invisible while scrolling when changing position absolute to fixed

  18. 18

    How to make an element fixed but relative to it's parent on scrolling only?

  19. 19

    How to position a child element at the bottom of a fixed position parent

  20. 20

    Position fixed sidebar with overflow scroll not scrolling to bottom

  21. 21

    JQuery - Prevent scrolling when an element is shown

  22. 22

    Jumping element by element when scrolling

  23. 23

    Absolute element becomes fixed when vertical scrolling past it

  24. 24

    JQuery smooth scrolling to one element when clicking a corresponding element

  25. 25

    Add class when element hits top of page AND remove class when element reaches bottom of parent

  26. 26

    The three first Element in custom ListView are repeated and change position when scrolling

  27. 27

    JQuery Position:Fixed 'NAVBAR' by scrolling the page

  28. 28

    JQuery Position:Fixed 'NAVBAR' by scrolling the page

  29. 29

    jquery scrollto not scrolling to correct element

HotTag

Archive