Div that stays in place exclusively for mobile devices

At the top, there are two divisions and on a cellphone device, the second one is meant to become fixed after the user scrolls past it.

To see an example of what I am trying to achieve, check out this link: https://jsfiddle.net/livibetter/HV9HM/

Specifically, I want this functionality to be exclusive to cellphones. On desktops, the behavior of the top two divisions should be different. Currently, they are fixed and functioning correctly on desktop displays.

For cellphones, I have disabled the 'fixed' styling for both division one and two. Once the user goes beyond division two while scrolling, I aim to make it fixed and behave as demonstrated in the provided example link.

Here are some code samples from the fiddle:

function sticky_relocate() {
    var window_top = $(window).scrollTop();
    var div_top = $('#sticky-anchor').offset().top;
    if (window_top > div_top) {
        $('#sticky').addClass('stick');
        $('#sticky-anchor').height($('#sticky').outerHeight());
    } else {
        $('#sticky').removeClass('stick');
        $('#sticky-anchor').height(0);
    }
}

#sticky {
    padding: 0.5ex;
    width: 600px;
    background-color: #333;
    color: #fff;
    font-size: 2em;
    border-radius: 0.5ex;
}

#sticky.stick {
    margin-top: 0 !important;
    position: fixed;
    top: 0;
    z-index: 10000;
    border-radius: 0 0 0.5em 0.5em;
}

body {
    margin: 1em;
}

p {
    margin: 1em auto;
}

Answer №1

One way to accomplish this is by utilizing media queries in your CSS code, like the following example:

@media screen and (max-width: 430px)
{
 .cssEntry {
  background: pink;
 }
}

REMINDER: Feel free to adjust the mobile width as needed, but typically 430px should suffice.

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

Changing the path of a polyline through code manipulation can sometimes result in a lingering phantom line

Users can edit lines on the map by dragging their vertices. When a vertex is moved, the system checks if it is near another object and attaches the line to that object if necessary. During this process, when the user moves the line, the "set_at" event is ...

What is the best way to organize nestedGroups in the vis.js timeline?

Running into an issue with the nestedGroups in my code. Despite sorting the array before creating the items and nestedGroups, I'm encountering a problem where the first item is showing up in the last position on the timeline. https://i.sstatic.net/Ne ...

The Data from Req.Body Doesn't Appear After Adding Form Fields

I've been struggling to find a solution to this issue, but here's the gist of it: I have a button that allows users to add a question and answer pair one at a time by clicking on an "Add Question" button. This is achieved through the append featu ...

jqgrid - dismiss the dialogue box

Currently, I am encountering an issue with jqgrid 4.3.1 while using form editing with local data. The problem lies in the form not closing after adding or editing data. Below is a snippet of my code: $('#studentset').jqGrid({ data: mydata, ...

Mastering the art of utilizing promises effectively in conjunction with callback functions

Currently, I have a service that retrieves a list of students asynchronously using a callback function: studentModule.factory('StudentService', function (DB_URL) { return { getAllStudents: function (callback) { ...

Using the same jQuery function for multiple elements with identical class names

Here is some HTML code generated through a PHP while loop: <div class="WriteCom <?php echo $Class?>"> <form class="c_comment " method="post"><input type="text" class="Comment_In" ></form></div> </div> Additiona ...

Swapping out a View's content with the data fetched from an AJAX request

I'm currently developing a counselor portal using Asp.NET and Kendo UI controls within an MVC3 application. Within this system, there is a button labeled AddStudents which triggers the display of a partial view containing student information when clic ...

JavaScript regular expressions only recognize certain characters

When submitting a form, I need to validate a field and ensure that only specific characters are allowed. The permitted characters include: a-z A-Z 0-9 % . " ' & - @ # $ * / + = [ ] ! I attempted to use the following regex for validation: var r ...

How to Customize the Drawer Color in Material-UI v5

I'm currently using MUI v5 in my project and I am encountering some challenges while attempting to style a Drawer component with a black background color. As this update is relatively new, I have not been able to find much information on customizing t ...

Display issue with ThreeJS cube

Currently, I'm delving into the world of ThreeJS and decided to incorporate the library into my existing NextJS project. My goal was simple - to display a cube on the front page. However, despite my best efforts, nothing seems to be appearing on the s ...

display a dual-column list using ngFor in Angular

I encountered a situation where I needed to display data from an object response in 2 columns. The catch is that the number of items in the data can vary, with odd and even numbers. To illustrate, let's assume I have 5 data items to display using 2 co ...

The Google Maps API is not providing any data in response to my jQuery JSONP request

Similar Topic: Unpacking Google Geo API (Reverse Geocoding) using jQuery $(window).load(function() { var purl = "http://maps.googleapis.com/maps/api/geocode/json?latlng=32.759294499999996,-97.32799089999999&sensor=false"; $.getJSON(purl,f ...

Can someone explain the significance of receiving a TypeError when trying to access properties of null (specifically 'useRef') in a React application?

I encountered an issue while working on a React project...the browser console displays the following error. What does this mean? And how can I resolve it? react.development.js:1545 Uncaught TypeError: Cannot read properties of null (reading 'useRef ...

Is there a way to reorganize the layout of a container on mobile using this code?

Is it possible to rearrange this code for mobile view? Currently, on desktop, the <div class="grid4-12"> (left) and <div class="grid8-12"> (right) are stacked on top of each other. How can I adjust the code so that on a r ...

HTML inputs do not account for the flex-basis CSS property

For some odd reason, inputs seem to be having trouble interpreting flex-basis correctly. Here's a basic example showcasing how inputs don't follow the rules and extend beyond their parent block (check out this JSFiddle): <div> <inpu ...

Following conventional methods often results in oversized containers and monolithic code

I've heard that it's recommended to use classes for containers and functions for components. Containers handle state, while components are simple functions that receive and send props to and from the containers. The issue I'm encountering i ...

How to incorporate a phone number input with country code using HTML

Can anyone help me create a phone number input field with a country code included? I've tried a few methods but haven't had much success. Here is the code I've been working with: <div class="form-group "> <input class= ...

Parsing JSON data as files are being read in an asynchronous manner

I have a task of reading multiple JSON files and consolidating their data into a single array. Here is my approach: const files = ['file0.json', 'file1.json', 'file2.json', 'file3.json'] To achieve this, I utilize ...

Leveraging ng-model-options in conjunction with ngModelController

I am attempting to postpone the validation of a custom textbox component. My goal is to only validate the input when it loses focus. The current component does not employ ng-model within the input itself, but instead utilizes the ngModelController in the c ...

Is there a way to activate a click event when I click on a button that is located outside of the dialog element?

In my Vue 3 app, I am using the native HTML dialog element as a modal. I have managed to position a button outside of the modal with absolute positioning. However, I am facing an issue where I cannot trigger a click event (displayNextModal) when clicking ...