Skip to content

Instantly share code, notes, and snippets.

@fhdalikhan
Created October 11, 2020 22:10
Show Gist options
  • Save fhdalikhan/0a0a119f7b07657a583161ec3848046c to your computer and use it in GitHub Desktop.
Save fhdalikhan/0a0a119f7b07657a583161ec3848046c to your computer and use it in GitHub Desktop.
For Removing Class and Adding Class based on the scroll position
<div id="menuBar" class="nav-menu"></div>
<div style="margin-bottom: 999em;"></div>
<script type="text/javascript">
$(document).ready(function(){
$(window).on('scroll', function () {
var $body = $('body');
var $target = $('#menuBar');
if ($body.scrollTop() > 100 && $target.hasClass('nav-menu')){
$target.removeClass("nav-menu");
}
else if( $body.scrollTop() <= 100 && !$target.hasClass('nav-menu') ){
$target.addClass('nav-menu');
}
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment