Prevent page scrolling to top upon adding fixed position

Kid Diamond

I'm facing a problem where the page is scrolling to the top whenever I add my .noscroll class to the body. This way the scrollbar is still visible but greyed out whenever the overlay appears.

I know that it's the addClass() function that is the cause of this because when I comment out that line, it doesn't scroll to the top when my overlay appears.

jQuery

$('a').click(function(e) {
    //reset default anchor behavior
    e.preventDefault();

    //add noscroll class to body
    $("body").addClass("noscroll"); 

    //open overlay box
    openOverlayBox(); 
});

The openOverlayBox() function is simply a full browser screen overlay with a black tint.

CSS

body.noscroll{
    position: fixed;
    width: 100%;
    overflow-y: scroll;
}

Body HTML

<a href="#">Test</a>

How can I make the scroll position stay the same after the .noscroll class has been added to the body?

EDIT 1: I'm trying to achieve the same as on Facebook. If you view a picture or video, the overlay appears, the scrollbar is greyed out but the scrolled position is maintained.

EDIT 2: I found a very close solution to my issue, but the only thing is that this does not grey the scrollbar out, but just removes it. Also when the content is centered in the middle, it still makes the content jump a little to the right because the scrollbar from the body is hidden.

EDIT 3: After Cuberto's answer and some research of myself I found out what needs to be done to get it working I want. However I don't have a clue how I would start doing it. But this is what should solve it. I when opening the overlay I need to set my main div on position: fixed and a negative top value of the scroll position. When exiting the overlay, the position: fixed; and top attribute should be removed and the same scroll position set as when the overlay was opened.

sbking

Taking a quick look at how facebook does it, it's something like this:

<body>
  <div class="main" style="position: fixed; top: -400px"></div>
  <div class="overlay" style="position: absolute; width: 100%; height: 100%"></div>
</body>

Sorry for the inline styles. You need to programmatically set the main div's top style to your scroll position.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Need a CSS solution to stay top left with page scrolling (not position:fixed)

From Dev

Prevent page from scrolling to top

From Dev

JQuery Position:Fixed 'NAVBAR' by scrolling the page

From Dev

JQuery Position:Fixed 'NAVBAR' by scrolling the page

From Dev

Adding a fixed-position view on top of a view

From Dev

Position fixed acting like position relative ( scrolling with page )

From Dev

debug lag of calculate margin top for Position fixed sidebar scrolling to bottom

From Dev

adding "position: fixed;" prevents all scrolling, even on unattached elements

From Dev

Prevent navigation to the top of the page after click on fixed top menu

From Dev

Header Div position fixed on top of page but on load show a div on top of it

From Dev

Revealing A Div Fixed to Page Top After Scrolling 95 Pixels

From Dev

Parts of page are invisible when scrolling on Safari 7 with position:fixed elements

From Dev

Position absolute but with fixed scrolling

From Dev

jquery-steps: how to prevent page scrolling to top?

From Dev

How to prevent white space "bounce" after scrolling to the top of the page and the bottom

From Dev

Prevent fixed-position background-image: cover from resizing in mobile browsers upon address bar hide

From Dev

Prevent a Fixed Positioned Background from Jumping to the Top of a Page on a Click Event?

From Dev

Fixed Page Scrolling

From Dev

two Position Fixed on top

From Dev

Bootstrap 3 Fixed Top Navbar 'Flickering' On Mobile Scrolling using jQuery One-Page Scrolling Effect

From Dev

scrolling an element on top of a fixed element

From Dev

#sidebar position:fixed at certain scrolling

From Dev

How can I set a div to position:fixed, when scrolling it to the browsers top?

From Dev

position: fixed overlapping page

From Dev

Prevent the listview from scrolling to its top position when ItemSource changed Windows 8.1

From Dev

Is there a way to prevent the gridview from scrolling to its top position when its adapter's data is changed?

From Dev

How can I prevent the browser from scrolling on top of the page when clicking the checkbox?

From Dev

how do I prevent max-height transition from scrolling the page to the top?

From Dev

How to prevent page from scrolling to top after css3 modal is closed?

Related Related

  1. 1

    Need a CSS solution to stay top left with page scrolling (not position:fixed)

  2. 2

    Prevent page from scrolling to top

  3. 3

    JQuery Position:Fixed 'NAVBAR' by scrolling the page

  4. 4

    JQuery Position:Fixed 'NAVBAR' by scrolling the page

  5. 5

    Adding a fixed-position view on top of a view

  6. 6

    Position fixed acting like position relative ( scrolling with page )

  7. 7

    debug lag of calculate margin top for Position fixed sidebar scrolling to bottom

  8. 8

    adding "position: fixed;" prevents all scrolling, even on unattached elements

  9. 9

    Prevent navigation to the top of the page after click on fixed top menu

  10. 10

    Header Div position fixed on top of page but on load show a div on top of it

  11. 11

    Revealing A Div Fixed to Page Top After Scrolling 95 Pixels

  12. 12

    Parts of page are invisible when scrolling on Safari 7 with position:fixed elements

  13. 13

    Position absolute but with fixed scrolling

  14. 14

    jquery-steps: how to prevent page scrolling to top?

  15. 15

    How to prevent white space "bounce" after scrolling to the top of the page and the bottom

  16. 16

    Prevent fixed-position background-image: cover from resizing in mobile browsers upon address bar hide

  17. 17

    Prevent a Fixed Positioned Background from Jumping to the Top of a Page on a Click Event?

  18. 18

    Fixed Page Scrolling

  19. 19

    two Position Fixed on top

  20. 20

    Bootstrap 3 Fixed Top Navbar 'Flickering' On Mobile Scrolling using jQuery One-Page Scrolling Effect

  21. 21

    scrolling an element on top of a fixed element

  22. 22

    #sidebar position:fixed at certain scrolling

  23. 23

    How can I set a div to position:fixed, when scrolling it to the browsers top?

  24. 24

    position: fixed overlapping page

  25. 25

    Prevent the listview from scrolling to its top position when ItemSource changed Windows 8.1

  26. 26

    Is there a way to prevent the gridview from scrolling to its top position when its adapter's data is changed?

  27. 27

    How can I prevent the browser from scrolling on top of the page when clicking the checkbox?

  28. 28

    how do I prevent max-height transition from scrolling the page to the top?

  29. 29

    How to prevent page from scrolling to top after css3 modal is closed?

HotTag

Archive