Modify CSS when scrolling, based on the size of the screen

I've implemented this JQuery script to modify elements in my header as the user scrolls. Is there a way to limit these modifications based on the screen size?

Here's the code snippet:

$(window).scroll(function() {

    // Once the user has scrolled 100px from the top...
    if ( $(window).scrollTop() >= 10 ) {
        $('#scrollthing').css('display', 'none');
        $('#ticket-amount').css('margin-bottom', '15px');

    // Otherwise, remove any inline styles to revert back to the original design
    } else {
        $('#scrollthing').attr('style', '');
    }
});

Appreciate your help!

Answer №1

To obtain the dimensions of the viewing area:

var viewAreaWidth = $(window).width();
var viewAreaHeight = $(window).height();

When the page is resized:

$(window).resize(function() {
      // Perform necessary actions upon resize
      if($(window).width() < 405 )
         // Do something here
});

Answer №2

Here is the code snippet you requested.




$(window).scroll(function() {
// check if window width is less than 400 and scroll top is greater than or equal to 10
if ($(window).width() < 400 && $(window).scrollTop() >= 10) {
// make your changes here
}
else { 
// else condition for when the if statement does not apply
}
});

This implementation may not be the most efficient for performance, but it should get the job done. You can consider wrapping it in a document ready function if needed.

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

A self-destructing javascript/jquery function that removes itself after running

For a very detailed analytics insight, I am sending an ajax request to my controller in order to update the user's interaction (such as which pages they visit or like) and store this information in my MongoDB. All I want is for this script to be dele ...

Adjust the svg rate using jQuery or JavaScript

Seeking help with a gauge I found on CodePen - struggling to adjust bubble values... <path id="Fill-13" fill="#F8B50F" d="M3.7 88.532h26.535v-#.795H3.7z"/> Can change the bars in JS, but not the bubbles using jq/js. Adjust the gauge with values be ...

Basic draggable pop-up window using HTML5 for a seamless experience

Can anyone provide me with helpful resources or tutorials to create a draggable popup using HTML5 without jQuery? I want to keep it simple and avoid external libraries like jQuery UI. Currently, I have an absolutely positioned div that looks like a popup ...

Guide on how to style and arrange multiple checkboxes in both horizontal and vertical rows using CSS

Is it possible to align a collection of checkboxes both vertically and horizontally, even when the labels vary in size? I found an example that demonstrates vertical alignment. Click here to view. How can I neatly arrange these checkboxes so that they ar ...

Replace the content within the iFrame completely

Is it possible to have a textarea where I can input HTML code and see a live preview of the webpage in an iframe as I type? For example, here is the code I'd like to write in the textarea: <!DOCTYPE html> <html> <head> ...

What is causing the button on the update div to not respond when clicked after receiving a Jquery ajax response?

As a newcomer to Jquery, I am facing a challenge where I want the user to be able to select an item and have a table displayed with editable information. I expect this table to automatically update once the user clicks on the "modifier" button. The tables ...

What is the process for integrating CSS-styled text into CKEditor?

Currently, I am developing plugins that are designed to incorporate various elements like images and tables into the editor. My objective is to be able to view these elements in their final visual form within the editor itself. To achieve this, I plan on a ...

Incorporate an XNA game directly into a website

Is it possible to launch an XNA game directly from a web page without the need for any installations? Something along the lines of Java, Silverlight, or similar to WPF Browser Applications (XBAP). Situation: Visit a webpage, Prompted to install the g ...

When a div is covered by an if statement

One of the challenges I am facing is managing a view with multiple projects, some of which are active while others are not. In my function, if a project's deadline has passed, it displays '0' days left on the view, indicating that these are ...

Tips for making a horizontal grid layout with three items in each row

I have a scenario where I need to render a group of Player components using map() loop, and I want them to be displayed horizontally side by side using a Grid component from material-ui. Currently, the components are rendering in a vertical layout: https ...

Refresh WebPage automatically after a Servlet successfully uploads and processes an image

I have a webpage that includes an image and a button. When the button is clicked, it uploads the image by submitting a form to a file upload servlet. However, after successfully uploading the image, the servlet does not display it in the img tag. Here is ...

Customize the drawer background in Vue Material

Recently, I developed a vuejs application incorporating Google's material design components. I've been exploring options to customize the background color. <template> <div class="page-container"> <md-app> ...

Display or conceal elements by utilizing ng-show/ng-hide according to specific conditions

Here is the code snippet I am working with: <input class="form-field form-control" type="text" name="Website" ng-model="vm.infodata.Website" placeholder="Website Address" maxlength="50" required ng-pattern="/^(www\.)?[a-zA-Z0-9_&bs ...

Understanding the document.domain feature in Javascript

I have a website called foo.com hosted on server bar. Within this website, I have created a subdomain called api.foo.com. To connect the subdomain with Google Apps, I have set up a CNAME entry pointing to ghs.google.com. Now, I am facing an issue when att ...

CSS: border-radius // background-color: white; rounded corners

Having recently delved into the world of HTML/CSS, I am currently working on a fun project to enhance my web development skills. In this project, I have created a search bar and successfully added round corners with border-radius. However, an issue arises ...

Altering the text of dropdown items prior to the ASP.NET autopostback

Recently, I inherited a project from a client that is plagued with some irritating issues. One particular problem involves a dropdown menu that triggers an autopostback event upon selection change, inserting the selected text into a T-SQL query. The troubl ...

Tips for creating a customized dropdown menu that opens upwards without relying on Bootstrap

Looking for some assistance with creating an animated dropdown menu that slides upwards. Any help is appreciated! $('.need-help').click(function() { $('.need_help_dropdown').slideToggle(); }); .need-help { background:url(../im ...

The AJAX call returned undefined, leading to an error when trying to access the length property

I've scoured various resources to find a solution for this issue, but unfortunately, I haven't had any luck with implementing the code. The problem lies with my JSON parser function, which is designed to construct a table based on data received ...

Instructions on removing rows by using buttons within a JavaScript-generated table

This snippet displays JS code to create a quiz index table and HTML code to display the index. function load(){ var data = [ { "id": "qc1111", "quizName": "Quiz1", "course": "111", "dueDate": "1/ ...

Engaging with a remotely loaded HTML in a JQUERY UI Dialog

My Experience with JQuery UI Plugin For a while now, I've been utilizing the JQuery UI plugin to create dynamic dialog boxes that load content on the go. Imagine this scenario: I load an HTML document that showcases various selectable items with che ...