Using scrollTop inside of a fixed element with overflow-y:scroll

addMitt

I can't seem to get scrolltop to work when my content is inside of a fixed position container that has overflow-y: scroll specified.

Here is my relevant code:

/* some container with many large content blocks inside */
#container {
    position: fixed;
    width: 300px;
    height: 300px;
    overflow-y: scroll;
}

/* button that has a data-path attribute that specifies where the container should scroll to*/
$(".button").on("click", function(){
    var path = $(this).attr("data-path");
    var anchor = $("#" + path);
    var position = anchor.position().top;
    $("#container").animate({scrollTop: position});
});

I believe this fiddle illustrates my dilemma quite well: http://jsfiddle.net/Qndu5/

If you scroll from the top down to an element, it works great. After that, all bets are off. It is completely incapable of scrolling from any position other than the top. It either horribly misses the mark, or scrolls all the way back to the top, even though the position values being fed to it are seemingly correct.

Surely I'm missing something here, but I'm not sure what I am not understanding. Thanks for any help provided!

Morten Olsen

What you are missing is the scrollTop when calculating the position, so if the view is already scrolled, that need to be added to the calculation var position = anchor.position().top + $("#container").scrollTop();

http://jsfiddle.net/x36Rm/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

scroll and position: fixed inside overflow: scroll div

From Dev

Scroll to the right in element with fixed width and hidden overflow

From Dev

Position fixed and overflow-y:scroll issue

From Dev

Absolute element inside table is causing the table to enable overflow-y: scroll somehow

From Dev

How to get the width of a html element inside an element with overflow scroll

From Dev

Animating scrollTop in fixed height overflow without jank

From Dev

Reducing scroll jank when using overflow-y: scroll;

From Dev

Vertical scrollbar inside a div without overflow-y: scroll

From Dev

Table cell overflow-y scroll, with absolute divs inside it

From Dev

Overflow-y scroll content disappears inside of div container?

From Dev

getBoundingClientRect() inside »overflow:scroll«

From Dev

Position element in centre of visible viewport window with overflow-y:scroll

From Dev

how to make content to be fixed inside scroll div using only css?

From Dev

how to make content to be fixed inside scroll div using only css?

From Dev

Element using overflow: scroll has scrollbar going off screen

From Dev

Show element inside a DIV with overflow-y on top of all others

From Dev

How to position sticky header on fixed position div with overflow-y: scroll

From Dev

.scrollTop to an element with fixed banner on top of screen

From Dev

Keep element position fixed to parent using transform on scroll

From Dev

How to end scroll when user finger gets outside of overflow-y:scroll element?

From Dev

Position fixed sidebar with overflow scroll not scrolling to bottom

From Dev

iOS 7 overflow scroll for fixed Div

From Dev

position:fixed navigation header overlapping browser scroll bar when using overflow-x

From Dev

Fixed element inside draggable fixed element

From Dev

How can I have an element with "overflow-y: auto" but still allow the browser to scroll when i drag an item horizontally using jquery ui sortable?

From Dev

How to fit the child div inside parent and scroll the overflow text only in the Y axis

From Dev

How to lengthen scroll when using scrollTop()?

From Dev

Bootstrap 3 - drag element horizontal with vertical scrollbar, overflow-y: scroll

From Dev

iOS Safari appears to never fire scroll event if the element under mouse has only "overflow-y: scroll" and scrolling horizontally

Related Related

  1. 1

    scroll and position: fixed inside overflow: scroll div

  2. 2

    Scroll to the right in element with fixed width and hidden overflow

  3. 3

    Position fixed and overflow-y:scroll issue

  4. 4

    Absolute element inside table is causing the table to enable overflow-y: scroll somehow

  5. 5

    How to get the width of a html element inside an element with overflow scroll

  6. 6

    Animating scrollTop in fixed height overflow without jank

  7. 7

    Reducing scroll jank when using overflow-y: scroll;

  8. 8

    Vertical scrollbar inside a div without overflow-y: scroll

  9. 9

    Table cell overflow-y scroll, with absolute divs inside it

  10. 10

    Overflow-y scroll content disappears inside of div container?

  11. 11

    getBoundingClientRect() inside »overflow:scroll«

  12. 12

    Position element in centre of visible viewport window with overflow-y:scroll

  13. 13

    how to make content to be fixed inside scroll div using only css?

  14. 14

    how to make content to be fixed inside scroll div using only css?

  15. 15

    Element using overflow: scroll has scrollbar going off screen

  16. 16

    Show element inside a DIV with overflow-y on top of all others

  17. 17

    How to position sticky header on fixed position div with overflow-y: scroll

  18. 18

    .scrollTop to an element with fixed banner on top of screen

  19. 19

    Keep element position fixed to parent using transform on scroll

  20. 20

    How to end scroll when user finger gets outside of overflow-y:scroll element?

  21. 21

    Position fixed sidebar with overflow scroll not scrolling to bottom

  22. 22

    iOS 7 overflow scroll for fixed Div

  23. 23

    position:fixed navigation header overlapping browser scroll bar when using overflow-x

  24. 24

    Fixed element inside draggable fixed element

  25. 25

    How can I have an element with "overflow-y: auto" but still allow the browser to scroll when i drag an item horizontally using jquery ui sortable?

  26. 26

    How to fit the child div inside parent and scroll the overflow text only in the Y axis

  27. 27

    How to lengthen scroll when using scrollTop()?

  28. 28

    Bootstrap 3 - drag element horizontal with vertical scrollbar, overflow-y: scroll

  29. 29

    iOS Safari appears to never fire scroll event if the element under mouse has only "overflow-y: scroll" and scrolling horizontally

HotTag

Archive