Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Content shifts slightly on page reload with Chrome

I have a div that has it's left and top offsets chosen randomly from an array of values on page load. It works for the most part, but once in a while, if you refresh, the window gets scrolled down slightly, exposing the next section, and all the content gets cut pushed very slightly to the left. Here are some screenshots of this undesired behavior. This is what the window looks like after a refresh.

Left offset

Window scrolls down

Here's the code:

<div class="about-section section">

    <div class="about-section__container">
        <h2 class="section-header about-section__header">About me.</h2>

        <p class="about-section__paragraph"></p>
    </div>
...
</div>
.section {
    width: 100vw;
    height: 100vh;
    z-index: 1;
}

.about-section {
    background-image: url("../../../mountain-background.png.jpg");
    background-size: cover;
}

.about-section__container {
    position: absolute;
    display: block;
    visibility: hidden;
    left: 0;
    top: 0;
}

.about-section__header {
    color: white;
    font-size: 3.5em;
    font-family: 'Abril Fatface';
}
var title = $('.about-section__container');

var xIndex = Math.floor(Math.random() * 4);
var yIndex = Math.floor(Math.random() * 4);

var leftMargs = ['20%', '25%', '30%', '35%'];
var topMargs = ['25%', '30%', '35%', '40%'];

title.css({
    'left': leftMargs[xIndex],
    'top': topMargs[yIndex],
    'visibility': 'visible'
    });

Another thing to note is that this works perfectly in Edge and Firefox, but only Chrome produces this behavior. It seems like the offset of the text container is somehow affecting the rest of the content, as if you refresh and the text moves to a random position to the left of the previous position, everything gets shifted left, and if it moves down on refresh it causes the browser to scroll down.

Also, this only occurs if you refresh, but never on a first visit.

Is this a problem with my Javascript or is the styling of my section containers or background wrong? Or is it something with the way Chrome caches the CSS? I have no clue as to what else I could possibly try, so any help is appreciated.

like image 678
jkofskie Avatar asked Dec 29 '19 22:12

jkofskie


People also ask

How do I stop page reload/refresh on hit back button?

How do I stop page reload/refresh on hit back button? You have to detect browser back button event and pass as an input of the page you want do prevent URL reload that indicates you if you came from a back button click. this code: $(window). on('popstate', function(event) { alert(“pop”); });

What happens when a page is reloaded?

Once a request is made to the server from the browser the page is processed. Even if the user cancels or stops the request, the server continues to process the request. If the user reloads/refreshes the page, it's another request that will be executed in parallel with the first request.

How do I make HTML refresh automatically?

Approach 1: One can auto refresh the webpage using the meta tag within the head element of your HTML using the http-equiv property. It is an inbuilt property with HTML 5. One can further add the time period of the refresh using the content attribute within the Meta tag.

How to force browser to reload page?

Chrome and Windows:Hold down Ctrl and click the Reload button. Or Hold down Ctrl and press F5.


1 Answers

add this in your css

.section { 
    width: 100vw; 
    height: 100vh !important; 
    z-index: 1; 
    max-width: 100%; 
}

Or You can add overflow-anchor: none; It works fine as well. I just found this link with the same issue Chrome Browser automatically scrolling

like image 77
Akbar Ali Avatar answered Sep 18 '22 16:09

Akbar Ali