The website appears to be loading in a unique way on mobile upon the second loading

While developing my personal website, I encountered a bug that occurs when accessing the site on my Android phone using Firefox or Chrome. The issue arises when the page initially loads correctly, but upon refreshing, the layout is displayed differently.

To address this issue, I utilized jQuery to dynamically determine the viewport size and ensure that the content is positioned accordingly below it. However, I noticed that the function seems to interpret the image height as 0, causing the content to be displayed at the very top of the page instead of beneath the image. Interestingly, if I scroll down and then scroll back up, the image correctly appears.

It's worth noting that this problem does not occur when accessing the site from my desktop, suggesting that it's specific to mobile devices.

Here is the jQuery function I used for dynamic sizing:

// This line ensures that the content is displayed below the image at either the viewport height 
// or the image height, whichever is greater
var height = $(window).outerHeight() < $('.jumbotron-img').innerHeight() ? $(window).outerHeight() : $('.jumbotron-img').innerHeight();
$('#header-img-container').css('height', height);

The corresponding HTML markup is as follows:

</div>
<div id='header-img-container'>
    <img src='images/site_skyline.jpg' class='img-fluid jumbotron-img'>
    <h1 class='d-flex justify-content-center fixed-top' id='main-header'>My Name</h1>
</div>

Answer №1

The reason for this issue is likely due to the height of the container being calculated before the image has fully loaded. In certain browsers, the image may be cached leading to a discrepancy between the desktop and mobile views.

To ensure that everything is loaded properly and the container's height is accurate, you can invoke your function within the window.load() method.

Depending on the version of jQuery you are using, you can achieve this with the following code:

$( window ).load(function() {
  var height = $(window).outerHeight() < $('.jumbotron-img').innerHeight() ? $(window).outerHeight() : $('.jumbotron-img').innerHeight();
  $('#header-img-container').css('height', height);
});

Alternatively, if you prefer to optimize for a specific image, you can trigger the load method specifically for that image like so:

$( ".jumbotron-img" ).load(function() {
  alert('image loaded');
}); 

Consider using an id instead of the class 'jumbotron-img' if you have multiple images and need to be more precise in your targeting.

For further information on the .load() method, refer to the documentation 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

Generating a header each time a table is printed across multiple pages

A table has been created in a webform using C#. The goal is to only print the table when the user clicks the print button on the webform. Javascript has been implemented in ASP.NET to only print the content within a specific div. While this method works, ...

I am unable to comprehend the function definition

I have familiarity with different types of JavaScript function declarations such as expression functions and anonymous functions. However, I am unsure about the syntax used in these two specific functions: "manipulateData: function (input)" and "getDataByI ...

When working with Vue JS, consider utilizing a data or computed property that is dependent on the value of a particular prop. In this case, the prop being altered is named "

While using pagination, I encountered an issue where the nextPage method works fine but the previousPage method throws an error. The error message warns against directly mutating a prop as it gets overwritten whenever the parent component re-renders. It s ...

Retrieving and submitting text in a NodeJS environment

I'm attempting to retrieve text from an API that only provides a string of text ((here)), but I am encountering difficulties in displaying it accurately. When I try to post it, the output shows as [object Response], and the console.log is not showing ...

Ways to retrieve every element inside a specific div container

Is there a way to select all elements inside a div except for specific ones? For example, consider the following structure: <div id="abc"> <div class="def"> sagar patil</div> <div class="pqr"> patil</div& ...

Using jQuery's toggleClass method to implement CSS transformations

I have a question about this situation Each time the button is clicked, jQuery toggles the class: .transition { background-color: #CBA; transform: translateX(100px) scale(0.5) rotate(180deg); } Within the <div class="container2"> And upon ...

Using JavaScript to implement responsive design through media queries

The code seems to be having some issues as it only works when I reload the page. I am looking to display certain code if "size < 700" and other code if "size > 699". I also tried this code from here: <script> function myFunction(x) { if ( ...

Rotate through different image sources using jQuery in a circular pattern

I'm working on a project where I have 3 img tags in my HTML file. My goal is to change the src of all 3 images with a button click, using an array that stores 9 different image src links. When the page initially loads, it should display the first set ...

Ajax fails to send requests to PHP after changing the URL directory

After struggling to find a solution to my query, I have come to the realization that it has not been asked before. Despite enabling rewrite rules on my apache2 server in the .htaccess file located in the root directory, I am facing an issue with my ajax sc ...

How to resolve undefined Axios Authorization headers in Vue.JS and Node.JS interactions?

Encountering an issue while trying to set authorization headers for my axios instance. Here is the code snippet from my Vue.JS application running on http://localhost:8080 : axios.defaults.headers.common['Authorization'] = 'Bearer ' ...

Shopping Directive Coverage Discrepancy

In my application, there is a shop item directive that includes a price attribute. When a user interacts with the directive, the total cost (a variable stored in the main controller) should be updated by the price of the item that was clicked. Unfortunate ...

Obtain all the selection choices in a dropdown list using Selenium

Although I have come across similar questions, this one is distinct in its simplicity. Unlike other queries that involve iterating over options in a loop, my question revolves around the usage of the getOptions() method mentioned in Selenium documentation. ...

Having trouble with your contact form sending messages?

Looking for some assistance with the Contact form on this Codepen page: https://codepen.io/ssbalakumar/pen/uzDIA The form is displaying correctly on my website, but when I try to submit it, the page reloads and I'm unsure where the message is being ...

Transforming an ES6 JSON object into a dictionary: a step-by-step guide

As someone who is new to learning ES6 and JQuery, please forgive me if this question has been asked before (I couldn't find similar ones). Currently, I am working on retrieving data from a JSON file in the following format: { EUR: { code: "EUR" ...

Using Stringify to modify the value of a particular array in Local Storage

Uncertain of the accuracy of my question, but I am attempting to modify a value within a localstorage array. This is what my localstorage currently contains: [{"id":"item-1","href":"google.com","icon":"google.com"}, {"id":"item-2","href":"youtube.com","i ...

Strategies for sending checkbox data to MySQL via AJAX and jQuery

Looking for assistance on passing values of multiple checkboxes to MySQL using arrays with AJAX (jQuery). I've written two separate code snippets, is there someone who can help me merge them? $(document).ready(function(){ var selected = []; $(&apo ...

ScriptSharp - submitting information in a key-value pair format

Currently, I am attempting to send data using Script# by utilizing the jQuery.AjaxRequest method: jQueryAjaxOptions options = new jQueryAjaxOptions(); options.Url = "/Server/Login.ashx"; options.Data = new LoginRequest(username, password); jQuery.AjaxRequ ...

Is there a way to efficiently parse and categorize erroneous JSON data in JavaScript?

Resolved my issue var allKeys = ["key","en","ar"]; for(var i=0;i<allKeys.length;i++) { for(j=0;j<jsonText.Sheet1.length;j++) { console.log(allKeys[i] + ' - ' + jsonText.Sheet1[j][allKeys[i]]); } } Live demonstration Appreciation ...

Guide to sending a specialized SPL token using the libraries '@solana/web3.js' and '@solana/sol-wallet-adapter'

Attempting to transfer a custom SPL token using the solana-wallet adapter is proving to be challenging due to difficulty in obtaining the wallet's secret key for signing the transaction. Although I have reviewed resources on writing the transfer code ...

Developing user registration and authentication using Backbone.js in conjunction with Django Rest Framework

In the process of developing my app, I am utilizing backbone.js for the frontend and django-rest-framework for the backend. My goal is to enable user registration, login, logout functionality, and be able to verify whether a user is logged in using backbon ...