Home > Web Front-end > CSS Tutorial > Why Does My Full-Screen Background Image Jitter on Mobile Chrome, and How Can I Fix It?

Why Does My Full-Screen Background Image Jitter on Mobile Chrome, and How Can I Fix It?

Linda Hamilton
Release: 2024-12-09 08:34:07
Original
826 people have browsed it

Why Does My Full-Screen Background Image Jitter on Mobile Chrome, and How Can I Fix It?

Background Image Jittering on Mobile Chrome

In developing a responsive website with a full-screen background image that transitions across mobile, tablet, and desktop, it has been observed that the background jumps slightly when the address bar hides on iOS/Android browsers or Chrome on Android.

The issue stems from the background image's css properties, which include "fixed" and "cover" settings. When the address bar disappears, the height of the background div changes since it's set to 100% height. This in turn affects the background image, causing it to adjust its size and position to fit the new container size.

To resolve this, two solutions are proposed:

Solution 1: Use CSS vh Unit (Not Recommended)

Setting the background div's height to 100vh maintains the desired behavior; however, there is a known vh bug in iOS that causes inconsistencies in viewport unit measurements. Attempts to mitigate this issue with max-height or other workarounds have been unsuccessful.

Solution 2: Use Javascript to Set Height Based on Viewport Size

Javascript presents a more reliable approach, as it is unaffected by the URL bar's resizing. By determining the viewport size and setting the background div's height accordingly, the image remains static regardless of the address bar's visibility.

Here's the Javascript script:

var bg = $("#bg1, #bg2");

function resizeBackground() {
    bg.height($(window).height());
}

$(window).resize(resizeBackground);
resizeBackground();
Copy after login

Additional Adjustment:

While the Javascript script addresses the background resizing issue, a noticeable gap may appear when scrolling down. This can be resolved by adding a small offset to the background height calculation, effectively hiding the potential gap.

function resizeBackground() {
    bg.height( $(window).height() + 60); // Add 60px to compensate for gap
}
Copy after login

The above is the detailed content of Why Does My Full-Screen Background Image Jitter on Mobile Chrome, and How Can I Fix It?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template