Fixing a div at the top post scroll - bug on iOS mobile device

I am looking to achieve a similar effect as demonstrated in the example below: https://css-tricks.com/scroll-fix-content/

Essentially, the goal is to have a div become fixed at the top of the page after scrolling to a certain point. Initially, the div will have a position: relative, and once the window is scrolled past a certain point, it will switch to position: fixed.

While this behavior functions correctly on web browsers, it seems to have a delay on IOS devices where the div only becomes fixed after scrolling is complete.

Is there a solution to ensure that the div becomes fixed on scroll, rather than waiting until the scroll is complete?

Answer №1

This method has proven effective for me, even on iOS devices (after the initial scroll from the top). To see a demonstration, check out this JSFiddle: https://jsfiddle.net/fcoxg2kL/

Unfortunately, there isn't a perfect solution that meets the exact requirements of the original poster. The issue lies in how Apple handles the position: fixed property. While my solution works well on iOS after the initial scroll, it also functions correctly on other non-Apple devices like Android. It's essential for developers to consider the broader user base, rather than focusing solely on Apple devices. Despite its limitations on initial scrolling for iOS, my approach represents a step in the right direction.

Regrettably, this problem ultimately rests with Apple for resolution.

The setup I've implemented is fairly straightforward - applying a class when a certain distance (greater than 200px) from the top of the page is reached. The class I've used is madesticky. In the CSS, this class switches the positioning from position: relative; to position: fixed;.

It is my hope that this suggestion will either meet your needs or guide you in the right direction towards a suitable solution.

CSS:

.stickynav {
    position: relative;
    width: 100%;
    background-color: #000;
    }
    .stickynav.madesticky {
        position: fixed;
        top: 0;
        left: 0;
        z-index: 5;
        }

    .stickynav *::before,
    .stickynav *::after,
    .stickynav * {
        -webkit-box-sizing: border-box;
        -moz-box-sizing:    border-box;
        box-sizing:         border-box;
    }

    .stickynav_inner {
        margin: 0 auto;
        width: 100%;
        max-width: 1000px;
    }

    .navlist { margin: 0; padding: 0; }
        .navlist ul { list-style-type: none; }
            .navlist li { display: block; float: left; }
                .navlist a {
                    padding: 10px 15px;
                    display: block;
                    color: #fff;
                    text-decoration: none;
                    }
                    .navlist a:hover {
                        background-color: #454545;
                        }

HTML:

<div style="height: 200px;"></div>

<div class="stickynav">
    <div class="stickynav_inner">
        <ul class="navlist">
            <li><a href="#">Nav Item 1</a></li>
            <li><a href="#">Nav Item 2</a></li>
            <li><a href="#">Nav Item 3</a></li>
        </ul>
        <div style="clear: both;"></div>
    </div>
</div>

<div style="height: 2000px;"></div>

JQuery:

/**
 * Header Scroll with Add/Remove Class Function
 */
function headerScrollResize(){

    var scroll = $(window).scrollTop();

    if (scroll < 200) {
        $(".stickynav").removeClass("madesticky");
    }

    if (scroll >= 200) {
        $(".stickynav").addClass("madesticky");
    }

}
$(window).on('load scroll', function(){
    headerScrollResize();
});

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

What is the appropriate method for passing parameters in PHP and how can you handle returned empty values

I'm looking to pass parameters in the action method because I am unable to do so via the header. <form name="mailinglist1" method="post" action="report1.php" > In this form, I'm using a download button to connect my report (HTML). ...

What is the best way to retrieve and utilize this JSON information with D3?

Understanding how to load JSON in D3 is crucial for working with data visualization. The process involves using the following code snippet without encountering any errors: d3.json("sample_data/unique_items.json", function(json) { // do something }); Af ...

Export nested objects from an AngularJS JSON array to a CSV file

When I download my data into a CSV file, the display setting is showing as "[object Object]". This is not the desired outcome. https://i.stack.imgur.com/ej2UO.png The expected display should look like this: https://i.stack.imgur.com/8JJ88.png This is p ...

What is the reason for needing to export the function when importing a module in an Angular appModule?

I came across the following code snippet @NgModule({ declarations: [ ... ], imports: [ RoutingModule, SharedModule, JwtModule.forRoot({ config: { headerName: 'Authorization', tokenGetter: () => lo ...

When the canvasJS range column chart is below the horizontal axis, modify the color and width of the bars

For each day of the week, there are records of fuel consumed and refilled. I envisioned representing this data in a range column chart using CanvasJS library. The unique aspect is that the color and width of the bars should change dynamically based on thei ...

Getting a pair of `focusin` events upon focusing

My Current Project As I work on my project, I encountered an interesting issue with event listeners and focus handling in different browsers. Specifically, when using jQuery's .on() to register a listener for the focusin event and programmatically se ...

Creating a minimalist metro-inspired menu using HTML and CSS

After researching online for a few days, I've just started learning HTML/CSS and my skills are currently at about a 6 out of 100. I have this code that functions as a blogger HTML widget, but I want to customize it further. Here is the code snippet: ...

What is the reason behind the absence of unwrapping when utilizing a ref as an element within a reactive array or reactive Map?

The Vue documentation states the following: Unlike reactive objects, there is no unwrapping performed when the ref is accessed as an element of a reactive array or a native collection type like Map Here are some examples provided in the documentation: c ...

Learn how to implement Basic Authentication in your Express application using the express-basic-auth package. You can easily trigger login and logout

When it comes to logging out a user who has logged in using basic Auth, I am exploring different options by consulting various sources like: link1 link2 In my application, I have implemented express-basic-auth to secure certain routes. Here is an example ...

Tips for implementing a checkbox (with tick/untick functionality) as a replacement for a plus/minus toggle using HTML

Is it possible to use checkboxes instead of plus and minus signs for expanding and collapsing sections? Can the plus and minus symbols be incorporated into the checkbox itself, so that clicking on the checkbox toggles between plus and minus states? Any hel ...

The align-self-center property in Twitter Bootstrap Flex does not properly center elements vertically

I recently started working with twitter bootstrap 4.4.1 and flex to create a unique layout for my project. My main goal is to have a navbar at the top and the content displayed in the middle of the screen, as shown in the image here: https://i.sstatic.net ...

Tips for Choosing Two Classes Using CSS

I'm currently exploring ways to select two classes so that when one of them is hovered over, a specific animation will occur. Here is my latest attempt: .news1 { -webkit-filter: blur(3px); filter: blur(3px); -webkit-transition: .3s ease-i ...

Encountering a "Text creation error" while trying to run a three.js demo on Microsoft Edge using the WebGL context

When attempting to run three.js on Edge, an error message appears stating 'text could not be created. Reason: Could not create a WebGL context.' Even after trying to execute the official three.js example on Edge, the same error persisted, while ...

Ways to avoid the cascading of CSS styles onto multiple Three.JS scenes

It seems like I might have to take the longer route to solve this issue, but let's give it a shot... I'm facing a challenge when applying CSS to control the opacity of a specific HTML element that acts as a container for a Three.JS scene. In thi ...

Transferring parameters via URL - When passing single quotes, they are converted to &#39;

In my current ASP.NET project, we encounter an issue with passing parameters through the URL. Whenever a single quote is passed, the URL automatically changes all single quotes to %27 and the actual JavaScript value reads them as &#39; I need assistan ...

What is the process of programmatically sorting a column in a Material UI DataGrid?

Hey there! I'm currently working on a DataGrid that has a column with a custom header, specifically a Select option. My goal is to have the column sorted in descending order every time a user selects an option from the dropdown menu. renderHeader: (pa ...

"Learn the process of integrating Javascript files from the Angular assets folder into a specific Angular component or module (such as Angular 2, 4,

I have custom1.js, custom2.js, and custom3.js JavaScript files that I need to load into Angular components component1, component2, and component3 respectively. Instead of adding these files to the index.html globally, I want to load them specifically for e ...

Displaying 'N/A' in the chart if the data is missing

I have a chart that displays data, but when data does not exist it shows "undefined%". https://i.sstatic.net/Fm3Tl.png Is there a way to remove the "undefined%" and simply display nothing on the graph if no data exists? Here is the code snippet: import { ...

jQuery animation not executing as expected due to transition issues

I'm looking to create a cool effect where the previous quote disappears and a new one appears when I click on a button, all with a smooth transition effect. In my attempt below using jQuery .animate and opacity property, I'm struggling to make i ...

Create a JavaScript button that increases a progress bar value by 1 and adjusts its width style by 10 units

Here is the code for managing a progress bar using JavaScript: function getProgress() { return document.getElementById("progressbar").getAttribute("aria-valuenow"); } function setProgress(value) { document.getElementById("progressbar").setAttri ...