The enchanting world of CSS background scrolling animations

I am currently developing a website where I have split the hero/header into two sections - one on the left with information and the other on the right with a graphic. I wanted to add a simple parallax effect to the image on the right side, so I used background-attachment: fixed;.

However, I now need to change the background-attachment property once the bottom of the graphic reaches the next section, so the image will scroll up along with the rest of the content from that point onwards.

You can view a Fiddle that demonstrates what I'm trying to achieve here:

https://jsfiddle.net/fg67ho2L/1/

Is there a way to make this work as intended so that the right side (image) behaves correctly? Your help is greatly appreciated. Thank you.

Answer №1

To make it happen, adjust the height value of the right side to be lower than the left side.

Answer №2

Encountering an error with the script led me to make some changes.

var abouttop = document.getElementById("about").getBoundingClientRect().height;

Instead of using background-attachment, I opted for transforms. It's worth noting that you need to add the id="about" to your text-container. Since I don't use JQuery, here is a piece of functional javascript code (please excuse the untidy format):

var pos = 0;

var imageControl = function(e) {
  var scrollposition = document.documentElement.scrollTop;
  var abouttop = document.getElementById("about").getBoundingClientRect().height;
  var scrollBottom = jQuery(window).scrollTop() + jQuery(window).height();
  var image_height = document.getElementById("image").getBoundingClientRect().height;

  if (scrollBottom < abouttop) {
    pos = scrollposition;
  } else if (scrollBottom > abouttop) {
    //pos = abouttop-image_height;
  }
  console.log(pos);
  document.getElementById("image").style.transform = "translateY(" + pos + "px)";
};

jQuery(window).scroll(imageControl);

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

Tips on attaching a click event listener to Nav.Link component in React Bootstrap

return ( <Navbar bg="dark" variant="dark"> <Navbar.Brand href="/">Instagram</Navbar.Brand> <Nav className="mr-auto"> <Nav.Li ...

Resources for ExpressJS

New to ExpressJS and trying to figure out how to reference images stored in the /public/images/ directory in my /public/stylesheets/styles.css The code snippet below doesn't seem to be working for me: .home-bg { background-image:url('../ima ...

Creating a post with an editable div element

I'm facing a challenge with creating a customized textbox using HTML's <input type="text" /> due to its limitations. I am considering using jQuery to build my own textbox, but I am worried about how the data will be posted. I have two optio ...

Issue with Tailwind CSS: The 'overflow-y' property does not scroll all the way to the bottom when using 'top-[63px]'

I'm facing an issue with my Tailwind CSS code that is hindering me from scrolling to the bottom of an aside element. <body> <div> <nav class=" bg-white px-2 py-2.5 dark:bg-gray-800 sm:px-4 fixed w-full z-20 border-b borde ...

Utilize JavaScript to extract and exhibit various SQL records stored within a multidimensional array

How can I use JS, JQuery, PHP, and MySQLi to automatically generate an HTML table within a div on a webpage? The table should display data based on user-input search criteria. For example, the user enters a start date and end date, clicks a button, which t ...

Struggling with calling rerenderEvents in FullCalendar using JQuery after a successful AJAX request

I seem to be encountering an issue where the calendar does not update after a POST request. Everything works smoothly until that point: $('#calendar').fullCalendar({ ... select: function (startDate, endDate) { $.ajax({ ...

One particular page is having trouble loading CSS, whereas it loads perfectly fine on a different page

I have two pages, 403.html and 404.html, both of which need to load the same style.css file. Strangely, when I visit the 403.html page, the CSS loads perfectly fine. However, when I navigate to the 404.html page, even though it has identical code with only ...

Issues with PHP Mail Forms not functioning as intended

I've been struggling for hours trying to figure this out. I'm new to PHP mail form coding and any assistance would be greatly appreciated! Below are two images: one of the HTML code and one of the PHP code. It seems like a simple setup, but even ...

Issues with CSS animation functionality have been detected in the Edge browser

In this particular div, I have two spans enclosed. I've created a loader which features a circle filling up with red color repeatedly. While everything appears to be functioning smoothly in Google Chrome, there's a noticeable glitch when it comes ...

The try and catch block in JavaScript is failing to correctly capture the HTTP status

I have a function that successfully posts JSON to an API endpoint. Here is the code I am using: function sendValuesPageLoad(){ var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () { try { if (xhr.readyState === ...

Creating a mobile-friendly navigation bar with Vuetify for optimal responsiveness

I am attempting to utilize a vuetify tab component as my navigation menu in order to create a responsive navbar using vuetify. The goal is to have a normal navbar that functions like usual, but when viewed on a mobile device, it should hide the menu and di ...

adaptive menu bar with collapsible submenus

I would like to create a right side inline navigation bar with dropdown functionality. When the screen size is small, I want to hide all the menus and display a button. Clicking on the button should reveal a vertical navigation bar. I am facing an issue i ...

What is the best way to connect a relative CSS stylesheet to a master page?

My Java application generates several HTML pages, organized in different directories: html_pages/ | ----> landin_page.html html_pages/details | ----> index1.html html_pages/more_details | ----> index2.html html_pages/css | ...

What is preventing me from using header() to redirect to the page?

There seems to be an issue with the URL redirection. Instead of redirecting to Activated.php, it is currently showing http://localhost/SP/activation.php/Activated.php instead of http://localhost/SP/Activated.php. <?php require 'db.php'; $msg= ...

Browsing through all text on songmeanings.com's HTML files

I am attempting to extract user comments from the HTML of a link using BeautifulSoup. The link in question is: However, I am facing an issue where only the user comments from the first page are being displayed when utilizing a specific code: import url ...

"An unanticipated parameter was encountered while using jQuery AJAX to make

I have a specific request: bindAutoComplete('pro_title'); This command triggers the usage of a jQuery AJAX call within this function: function bindAutoComplete(id) { $("#" + id).keyup(function() { if (this.value.length > 1) { ...

Using jQuery to delete data asynchronously with AJAX technology

I am currently working on a cart page that shows items in a user's shopping cart using ASP while retrieving data from a table. One of the features I have implemented is the ability for users to delete an entry by clicking on an image. I have successfu ...

Using Custom .woff Format Fonts in Sendgrid: A Guide

Having trouble with implementing custom fonts in Sendgrid. While Google fonts work fine, the custom .woff format font doesn't seem to cooperate. I've attempted three solutions below. Solution number 1 shows up in the Preview tab but not in the em ...

Tips for utilizing PrependTo dynamically on every element

I am facing a requirement that involves prepending an element with a specific class to its sibling with another class. While I have managed to accomplish this for the first occurrence, it seems to fail for all other elements within the same div. Below is t ...

Show the ajax response on a separate page

I am looking to showcase the output of an ajax request on a separate page rather than the page where the ajax call originated. The scenario is that I have a membership directory page, and when a user clicks on a member ID cell, an ajax call sends the ID to ...