0

I have a jQuery function to make a div stick to the top of the page, after it is scrolled below the original position. However, after a button click (postback), you have to scroll above the div first before it works again.

I think this may be the troublemaker:

MaintainScrollPositionOnPostback="true"

My jQuery:

$(document).ready(function () {
function fixedNav() {
    if ($(window).scrollTop() > 150) {
        $('#navigation').addClass('fixedNav');
        $('#mainContent').addClass('pushedMainContent');
    }
    else {
        $('#navigation').removeClass('fixedNav');
        $('#mainContent').removeClass('pushedMainContent');
    }
}
function isPostBack() {
    fixedNav();
}
$(window).scroll(function () {
    fixedNav();
})
})

I have been going over the function for an hour and I can't seem to find a fix. (I even added the same function to the isPostBack but that didn't help) Any help/suggestions are appreciated.

Fix: (thank you @charlietfl)

<script>see "My jQuery"</script>
<body onload="fixedNav()">
user6875529
  • 101
  • 9

1 Answers1

1

The problem is that this

$(window).scroll(function() {
    fixedNav();
})

creates a function to be called when someone scrolls, but when the page reloads, nobody is scrolling. And, if I'm not mistaken, the page will load with the classes that are in the markup, not the classes that were last applied.

So you have to manually call fixedNav(), to reapply the new classes.


I don't think this is relevant to your question but google 'document.ready vs onload' for answers to the difference b/t ready and onload. (If you want to compare two things, always use vs when googling.

Here's an SO answer: ready vs onload

wazz
  • 4,953
  • 5
  • 20
  • 34