"Gone in a Scroll: Div vanishes as page moves

Fiddle: http://jsfiddle.net/ud6hejm6/

I was tasked with creating a website for a video game tournament. By opening the fiddle link provided, you can preview the page layout. A central div is featured in the design:

<div class="middle teko" id="mezzo">
 <span style="color: purple">EndGame</span><span style="color: yellow">TV</span> &nbsp; + &nbsp; World Cup
</div>

One key CSS property set was:

html, body {
    height:100%;
    overflow: hidden;
  }

This prevented users from scrolling down the page. However, clicking on the div with id="mezzo" results in a scroll effect thanks to the following JavaScript code snippet.

$(document).ready(function() {

    $('#mezzo').click(function(){
        $('html, body').animate({scrollTop:$(document).height()}, 'slow');
        return false;
    });

    $('#back').click(function(){
        $('html, body').animate({scrollTop:0}, 'slow');
        return false;
    });

  });

The id="back" refers to the div displayed when the page is scrolled down.

The Puzzle

When running the jsfiddle demo, everything functions correctly except for one anomaly - as the page scrolls down, the central div with EndGameTV + World Cup (id="mezzo") stays fixed at the center of the screen instead of staying in its original position.

Despite being coded with position: absolute;, it behaves more like position: fixed;. Why does this occur?

Illustration of homepage view:

View upon scrolling down:

To see the live page, visit:

Answer №1

To solve this issue, you need to include a parent div around the entire content of your page:

$(document).ready(function() {
   
    $('#mezzo').click(function(){
        $('html, body').animate({scrollTop:$(document).height()}, 'slow');
        return false;
    });

    $('#back').click(function(){
        $('html, body').animate({scrollTop:0}, 'slow');
        return false;
    });
    
  });
(CSS code here)
(HTML code here)

Link to example on jsFiddle

Answer №2

If you're experiencing an issue where the position of your div is based on the position of <body> and remains static when scrolling, here's a solution:

To fix this, reposition div#mezzo to come after div.content-b in the code. By doing this, it will be placed correctly in the DOM tree without needing an additional parent div.

Your updated code should look like this:

-snip-

<div class="content-b">
    <img alt="Mario Kart 8 - World Cup" src="/images/mk8.png">
</div>
<div id="mezzo" class="middle teko" onclick="document.getElementById('mezzo').visibility = 'hidden';">
    <span style="color: purple">EndGame</span>
    <span style="color: yellow">TV</span>
      +   World Cup
</div>

-snip-

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

Stunning CRUD tables in AngularJS featuring dynamic jQuery pop-up windows

Hello, I have developed a small CRUD application using AngularJS and encountered a minor issue. The application is functioning correctly overall, but there is a problem: when adding a new record and then attempting to edit it, the pop-up box I created does ...

Blurry font rendering occurs when an element is scaled using CSS

I want to add a scale animation to a bootstrap button, but the text appears blurry. I've tried all the solutions mentioned here. How can I resolve this issue? Below is the code snippet (text is slightly less blurry in the snippet compared to my pr ...

Use MySQL and PHP to selectively filter records by choosing a specific option from a drop-down menu

Within my HTML webpage, I have a drop-down menu featuring various company names as options. When selecting a particular company, I can retrieve the associated records successfully. However, I would like to include an "All" option at the top of the list, ...

Retrieving an array using the $.post() method

I am utilizing the following code: $.post('fromDB.php', function(data) { eval(data); console.log(data); updateTimer(); }); In order to retrieve arrays from PHP. This is what PHP returns: var todayTimeMinutes = [0, 45, 35, 25, 40, 0 ...

How can I make the Struts2 iterator function correctly?

Using the Struts2 <iterator> tag in a JSP to display values, the method is being called but no results are appearing on the page. All the necessary data is printing in the console, but not in the JSP itself. The call made is localhost\listAddCo ...

Implement HTML code within WordPress labels

Seeking assistance with customizing certain menu items in a WordPress website. Wondering if it's feasible to include HTML/CSS in the title label? My current situation is as follows: I only want one result to be highlighted in blue. I attempted to ...

Extracting data from multiple web pages with R - An extensive collection of tables

Looking for assistance in extracting data from a website with multiple pages and importing it into R. Can anyone help me with this task? Website: ...

Transforming a Bootstrap 4 HTML project into Angular 9

After stumbling upon a beautiful HTML template that caught my eye, I realized it was built using Bootstrap. Luckily, I came across tutorials on how to convert such templates into Angular 6 projects, making the process seem quite straightforward (like this ...

The save feature becomes dysfunctional after switching to the Material-UI dependency in a React project

After integrating the Material-UI dependency into my ReactJS code, I encountered an issue where the "save" functionality no longer works properly. Previously, when editing a task in my simple Todo list app, the new name would be saved successfully. However ...

How come my Calendar is not showing up on the browser?

I came across a helpful guide on setting up a Calendar in HTML using JavaScript You can find it here: Unfortunately, the code I tried to use based on that guide isn't functioning as expected. <div class="row"> <div class="col-lg-4 text ...

Using jQuery to create a blinking effect on a specific letter in a string

I'm looking to create a text adventure using Canvas, and I want the parser to blink continuously, similar to the one in a Dos Console. The parser is stored as a global variable. How can I use jQuery to permanently change the character of this global v ...

I have tried using justify-content: center; and align-items: center; to center this div, but the container div is still not centered. How can I successfully center

I am having trouble centering the container in my HTML code HTML code:- <body> <div class="container"> <div class="image"> <img src="./images/image-qr-code.png" alt="qrcode"> & ...

Is there a way to alter the text color using JavaScript on the client side?

Is there a way to change the color of a list item in a ul based on whether it is a palindrome or not? Here is the JavaScript file I am working with: function isPalindrome(text){ return text == text.split('').reverse().join(''); } c ...

Looking for assistance in implementing specific styling techniques with React

Trying to implement a slider on my React page sourced from a Github repository. The challenge lies in adding the CSS as it is in JS format. Even after incorporating webpack and an npm compiler, the styling seems unrecognized. Any suggestions or solutions ...

Creating a visually striking layout with Bootstrap card columns masonry effect by seamlessly adjusting card heights to prevent any

When using the bootstrap card columns masonry and a user clicks on a button inside a card, the height of the card changes dynamically by adding a card-footer. However, in certain situations, the cards change position causing a jumpy effect. To see this i ...

Combine all div elements to create a unified image and then proceed to save it as a jpg file before uploading to the server

These are the divs below: <div style="width:800px; height:420px; position:absolute; top:0px; left:0px; z-index:10; background-image: url('https://3t27ch3qjjn74dw66o1as187-wpengine.netdna-ssl.com/wp-content/uploads/2016/05/052516-800x420-vege-Wallp ...

What is the best way to utilize localStorage along with AJAX in php in order to update my database effectively?

I am facing a challenge where I need to utilize a value stored in the localStorage of my browser within a PHP file in order to update data in my database. Despite knowing that I require ajax for this task, I have been unable to make it work as intended. T ...

CORS policy blocked Deepl API request

For the past few months, I successfully integrated Deepl API into our Web CRM. However, things took a turn a few days ago. I can't pinpoint exactly when, but it might have been since the start of the new year. Now, every request is being blocked by s ...

The functionality of a JQuery post within a PHP loop is not functioning optimally

I am encountering an issue with my list.php file, which generates a list with items and a button (Erledigt!) that triggers a jQuery Post to wishdelete2.php. The problem is that the jQuery post sometimes requires multiple clicks (3x or 5x) before it process ...

What methods can I use to accomplish the transformation of superimposed images?

Struggling with transforming a content box on my webpage. I have a div containing three overlapping images positioned using Grid, and I want to scale one of them to fill the entire div when scrolled over. While I managed to achieve this effect, I'm un ...