Developing a user-friendly widget for a website that is not optimized for mobile devices

Here's the backstory:

I'm currently in the process of creating a mobile-friendly widget for my clients. Unfortunately, they have web pages that are not optimized for mobile devices, and it doesn't seem like that will change anytime soon.

The Proposed Solution:

My initial idea was to take the widget out of the regular page flow by using position:fixed, adding a viewport meta tag, and voila! Everything should work smoothly...right? Check out this fiddle for reference.

The Issue at Hand:

However, the proposed solution seems to fail on certain mobile devices. My colleague experienced a situation where they could scroll away from the element marked as position:fixed on their Android 4 or 5 device (so it's not the older 2.1-2.3 bug). It appears that iPhones exhibit similar behavior.

Essentially, it behaves more like position:absolute positioned in the top-left corner of the page.

Further Details on the Proposed Solution:

I begin by injecting the viewport meta tag using JavaScript:

$('head').append('<meta name="viewport" content="width=device-width,initial-scale=1"/>');

Assume we have a simple HTML structure:

<html>
    ...
    <div class="overlay">
        <div class="modal">
            <div class="content">...</div>
        </div>
    </div>
    ...
</html>

and the accompanying CSS:

.hide-overflow {
    overflow: hidden;
}
.overlay {
    position: fixed;
    -webkit-backface-visibility:hidden; /* This may not be effective */
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    display: table;
    overflow: hidden;
    z-index: 1000;
}
.modal {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}
.content {
    display: inline-block;
    width: 800px;
    height: 500px;
}
@media (max-width: 800px) {
    .overlay * {
        max-width: 100%;
        -webkit-box-sizing: border-box;
        -moz-box-sizing: border-box;
        box-sizing: border-box;
    }
    .content {
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
    }
}

Realizing that this might not suffice, I also included JavaScript to prevent scrolling on the <body> and outer-most <div>:

//This code is activated only when the widget is active and removed upon deactivation.
$('body').addClass('hide-overflow'); //Just sets overflow:hidden, in case you missed it ;)
$('body > div').addClass('hide-overflow');

On my Galaxy Note phone's default browser, everything works perfectly without any issues. However, it seems on iPhones, some Android devices, and so on, users can freely scroll away from the element styled with position:fixed as if it were set as position:absolute. How can I ensure that position:fixed actually works as intended?

Answer №1

The solution turned out to be more straightforward than expected. While using javascript, I was simply appending my hide-overflow class to the body and first div element. The original class code looked like this:

.hide-overflow {
    overflow: hidden;
}

The issue was resolved by making the following adjustments:

.hide-overflow {
    overflow: hidden;
    width: 100% !important;
    height: 100% !important;
    -webkit-box-sizing: border-box !important;
    -moz-box-sizing: border-box !important;
    box-sizing: border-box !important;
}

Simply add this modified class to the <body> tag when the widget is displayed, and remove it when the widget is hidden.

You can view the functioning fiddle here.

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

Bring in d3 from unpkg

Uncertain of which file to import, I am seeking guidance on how to import d3 from unpkg.com. Can someone advise me on the equivalent unpkg version of: import * as d3 from "d3"; ...

Guide on waiting for an asynchronous function in the constructor in JavaScript

In the process of creating a JS class, I encounter a situation where I need to call wasmFunc in the constructor and store its output in this.val. However, since wasmFunc comes from a WebAssembly file, it is necessary to execute an async function called loa ...

After deploying DRF, the CSS in Django admin is not displaying

After developing a web application using Django Rest Framework and React, I faced an issue during deployment on IIS. While the deployment is successful, I encountered a problem with the Django Admin where the styles were not displaying properly. This led t ...

What is preventing the items inside the <ul> element from displaying?

Struggling to grasp pagination, I can't figure out why the contents of "li" under the "ul" disappear while the "ul" container continues to display despite specifying in the JavaScript that only 6 should be visible on the page. The "ul" and "li" elemen ...

Showing nested arrays in API data using Angular

I would like to display the data from this API { "results": [ { "name": "Luke Skywalker", "height": "172", "mass": "77", & ...

What is the best way to incorporate a function within a $.click (jquery) event that utilizes an id directly from the clicked element?

It seems that my title may not be very clear, but I have a jQuery code snippet that is causing some issues. The problem stems from the fact that when I click on an image with the class 'slideimg', I expect a function named slideDo to be executed, ...

utilizing vue model attributes with `${}`

I am encountering an issue with my code. Initially, :src="labels[index]" was working fine for me. However, as my codebase expanded, I needed to use ${app.labels[index]}. Simply using ${app.labels} works, but the addition of [index] causes it to b ...

Issues with jQuery validation in Struts2 form verification

My application is built on the struts2 framework, with jquery validation for client-side form input validation. However, I've encountered some compatibility issues between the two. I have a UserBean Class that needs to be included. The following cod ...

What is the method for retrieving the currently selected value in a MultiColumnComboBox within Kendo for Angular?

Check out this live example created by the official Telerik team: I need to extract the id (referenced in contacts.ts) of the currently selected employee when clicking on them. How can I access this information to use in another function? ...

Developing a music playlist using javascript/angular (replicating array elements while linking nested objects)

I am currently working on creating a playlist for a music player within an Angular app that includes a shuffle feature. The actual shuffle function is not the issue, as I am utilizing the Fisher Yates Shuffle method and it is functioning correctly. When th ...

My JavaScript functions are not compliant with the HTML5 required tag

I am currently developing input fields using javascript/jQuery, but I am facing an issue with the required attribute not functioning as expected. The form keeps submitting without displaying any message about the unfilled input field. I also use Bootstrap ...

The issue I am facing is that the map function is not functioning correctly when I click

I am currently working on a project in ReactJs that includes a sidebar with dropdown menu functionality. Desired Outcome When I click on an option in the sidebar that has a submenu, it should display that submenu and close upon another click. Curr ...

When testing, Redux form onSubmit returns an empty object for values

Is it possible to pass values to the onSubmit handler in order to test the append function? Currently, it always seems to be an empty object. Test Example: const store = createStore(combineReducers({ form: formReducer })); const setup = (newProps) => ...

The MUI snackbar element lingers in the DOM even after it has been closed

Having created a global modal with the intention of only utilizing it when necessary, I've encountered an issue where the snackbar div persists in the DOM. This persistence is causing certain elements to become blocked as they appear beneath this div. ...

Managing dynamic input texts in React JS without using name properties with a single onChange function

Dealing with multiple onChange events without a predefined name property has been challenging. Currently, one text input controls all inputs. I have come across examples with static inputs or single input functionality, but nothing specifically addressin ...

Removing a CSS Class Using Tampermonkey: A Step-by-Step Guide

I'm completely new to CSS and javascript, so please bear with me. My goal is to remove the class disable-stream from each of the div elements located under the div with the class "stream-notifications". Below is an image for reference: Even though I ...

Differences between Tags and Elements in HTML5

Could someone provide a detailed explanation of the distinctions between tags and elements in HTML5? I am curious about this topic because I have noticed a lot of confusion regarding the terminology online, with the terms being used interchangeably even o ...

Start running additional JavaScript code only after the previous one has been fully executed

Scenario: I am facing a situation where I have a web form that is submitted through the following event listener: $('#myForm').on('valid', function (e) { ... } Within this function, I have a code snippet that fetches the geo location ...

Choose the "toolbar-title" located within the shadow root of ion-title using CSS

Within Ionic, the ion-title component contains its content wrapped in an additional div inside the shadow-dom. This particular div is designated with the class .toolbar-title. How can I target this div using a SCSS selector to modify its overflow behavior? ...

Utilizing Laravel and Jquery UI for asynchronous communication with the server, passing data from a post request to

I recently constructed a basic Jquery UI slider: $("#sliderNumCh").slider({ range: "min", min: 0, max: 20, step: 1, value: 20, change : function(e, slider){ $('#sliderAppendNumCh'). ...