Mobile vs Desktop Scroll/Resize Issues
Ad
I have a navbar on a page which I want to have stick to the top once it reaches a certain point from the top on desktop devices. On mobile devices (less than 780) I want it to always stick no matter how far on the page has scrolled down.
Right now I'm running into an issue where both of the window scroll/resize events keep toggling the style on and off which is causing all sorts of mayhem. What would be a good way to repurpose this code so it behaves properly and acts seamlessly when the browser is resized?
$(document).ready(function() {
$(window).scroll(function() {
var $box = $("#box");
if ($(window).scrollTop() > 30) {
$box.addClass("fixed");
}
if ($(window).scrollTop() < 30) {
$box.removeClass("fixed");
}
if ($(window).width() > 780) {
$box.removeClass("fixed");
}
if ($(window).width() < 780) {
$box.addClass("fixed");
}
});
});
#box {
width: 100%;
background-color: red;
}
.fixed {
position: fixed;
top: 0;
}
<div id="box">
This is the nav
</div>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
<p>
Content
</p>
Ad
Answer
Ad
Check for the window width first, then only if its a desktop, check the scrollTop.
Based on OPs comment, this now runs on resize
and on scroll
$(document).ready(function() {
function headerFunction(){
var $box = $("#box");
if ($(window).width() < 780) {
$box.addClass("fixed");
} else { // window is larger than 780
if ($(window).scrollTop() > 30) {
$box.addClass("fixed");
} else {
$box.removeClass("fixed");
}
}
}
$(window).scroll(function() {
headerFunction();
});
$(window).resize(function(){
headerFunction();
});
});
Ad
source: stackoverflow.com
Related Questions
Ad
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad