Mobile Sticky Header with Jquery

My goal is to make the header visible when scrolling down, but I don't want it to scroll on mobile devices. Here is the jQuery code that I have been working with:

$(window).scroll(function () {
  if ( $(this).scrollTop() > 652 && !$('header').hasClass('open') ) {
    $('header').addClass('open');
    $('header').slideDown();
   } else if ( $(this).scrollTop() <= 652 ) {
    $('header').removeClass('open');
    $('header').slideUp();
  }
}); 

This code works for desktop, but not for mobile or tablet versions. I attempted to use CSS media queries to hide the header, like this:

@media screen {
  header { display: none;}
}

Unfortunately, the CSS approach did not produce the desired result.

Answer №1

To prevent a certain action from taking place in specific circumstances, simply impose a limitation.

Consider the following if statement:

if ($(window).width() > 767) {
     /* perform an action */          
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to ensure that a div element occupies the entire height of the webpage

After developing a small app in Angular, I'm looking to make the container element expand to the full height of the page, even when the content doesn't fill the entire space. On larger screens, the page doesn't stretch as desired. Here' ...

Eliminating image components in no particular sequence

I recently implemented Kendoui uploader for asynchronous uploads on my website. One of the cool features I added is that once an image upload is done, a neat little thumbnail preview pops up on the page: ... success: function(e) { if(e.operation == & ...

"Maximizing Data Interaction: The Integration of PHP and AngularJS

I have been developing an AngularJS App alongside a PHP RESTful API for the Backend. I am currently brainstorming about the most effective approach to harness the power of 2-Way Data Binding in AngularJS with my specific scenario. As an illustration, I ha ...

Retrieve Image URL from RSS Medium Feed

I am attempting to showcase the images from each post in medium's RSS feed using only JavaScript or Angular, without relying on JQuery. I am able to retrieve the title, creation date, and link for each post. Currently, I am developing with Ionic2. en ...

What is the best way to adjust the color of a button element?

I've been troubleshooting the mouseover function of my JavaScript button object. The goal is to have a function call (specifically show()) within the object that detects the mouseover event and changes the button's color to grey. I suspect that t ...

How to customize a select listbox in asp.net using bootstrap and remove unwanted

Whenever the drop-down selection changes, I want to dynamically update a list group box by replacing all existing items with new ones. Here's the code for the list group box: <select style="max-height: 30vh; min-height: 30vh; width: 100%; bor ...

Sending form input values to JavaScript

Struggling to design a webpage featuring one text box and a button that, when clicked, sends the value to Javascript for further processing? After spending significant time troubleshooting, I'm unsure of what's causing the issue. Below are both t ...

Guide to attaching a mouse click event listener to a child element within a Polymer custom component

I'm currently facing an issue where I am attempting to attach a click listener to one of the buttons within a custom element during the "created" life cycle callback (even attempted using the "ready" callback with the same outcome). Unfortunately, I ...

Cease the audio from playing unless you are actively hovering over the image

Is there a way to make the sound stop playing when you are not actively hovering over the picture? Any suggestions on how to achieve this would be greatly appreciated! imgarr[i].onmouseout=function(){ this.setAttribute('src',this.getAttribu ...

Exploring the Power of DataList in Angular

I am working with a <datalist> and <select> in the following setup: Updated: Example 1: <input type="text" list="codes" [(ngModel)]="codeValue" (change)="saveCode(codeValue)"> <datalist id="codes"> <option *ngFor="let c of ...

Page links in a JQuery clone are not operational

After dynamically generating page links at the top of my webpage, I attempted to clone them into a div located at the bottom. However, upon cloning, the copied page links fail to respond when clicked. Is this lack of functionality due to the fact that they ...

Angular TypeScript Directive Link function not being executed

I've been working on implementing a Role-Based-Access-Control system in my application. The allowed resources are loaded from the server after login, and I was able to verify this using raw JavaScript code. angular.module('app').directive(& ...

The function is undefined (near '... map ...')

Whenever I tap on the Pressable element in the JSX, an error pops up saying: "Undefined is not a function (near '... wines.map ...')." The issue seems to be originating from the wines.map loop in the JSX code. I am uncertain about what might be w ...

Replace the arrow and implement collapsible div using CSS

My HTML code has the following structure: <ul id="urlcss"> <li class="nav-submenu"> <a class="collapsed" href="#" data-toggle="collapse" data-target="#submenu0"> ...

Issues with displaying or hiding a DIV using javascript - popup box unresponsive

I am working with this ASPX code: <div id="IsAccountingOk" class="modalDialog"> <div> <a href="#close" title="Close" class="close">X</a><br /> <asp:Label ID="lblIsAccountingOkHeader" runat="server" Text ...

"Unfortunately, Azure Web Static Apps do not have the capability to access .env files that are prefixed with NEXT

Suppose I have an .env file set up like this: NEXT_PUBLIC_API_BASE_PATH = value1 While this .env is functioning correctly in my local environment, once deployed to Azure Web Static Apps and added to the configurationhttps://i.sstatic.net/eqiYn.png My app ...

What is the extent to which a scope variable will be accessible within the link function of Angular?

var $directive = angular.module('myApp', []); $directive.directive('myDirective', function(){ return { restrict: 'E', template: '<h4>{{title}}</h4>' compile: function(element, attrs){ ...

Failed to pass through invalid parameters "recordIds", "datasources" while navigating in VUE

While working with Vue, I encountered an issue when trying to pass parameters from one route to another. Here is what my target route looks like: { path: '/record-modification', name: 'recordModification', component: recordModi ...

Using React Native to showcase a background grid of dimensions {height: 10, width: 10}

Hey there! I'm looking to create a grid that can adapt to different device sizes and screen positions, similar to a chess grid. I want to use this as a background to help with sizing and positioning elements like text and images on the screen. Here&a ...

Is it possible to customize the formatting of a localized date using the jQuery UI datepicker?

I am currently using a jQuery UI datepicker and I want to customize the returned date by localizing it and removing the leading zeros. It appears that I can only achieve one of these options at a time. Is there any way to both localize and format the date ...