Issues with the parallax zooming out effect

Delving into the world of parallax effects, I'm on a quest to achieve a captivating zoom-out effect while scrolling. However, I'm encountering an issue where the image shrinks beyond the browser width and the div's height.

In the example provided, you'll notice that the red background of the wrapper section becomes visible as you scroll down.

Feel free to check out the live example at www.adamkhourydesign.com/test

HTML

<header id="header_container">
    <div class="header_back"></div>
    <div class="logo"></div>
</header>

CSS

#header_container {
  position: relative;
  height: 600px;
  background-color: rgba(255, 100, 85, 0.5);
  background-repeat: no-repeat;
  background-size: auto 800px;
  background-position: top center;
  background-attachment: fixed;
  overflow: hidden;
}

.logo {
  height: 100px;
  width: 100%;
  background-image: url(../img/name.png);
  background-position: center;
  background-repeat: no-repeat;
  position: absolute;
  top: 50%;
  margin-top: -50px;
}

.header_back {
  position: relative;
  height: 600px;
  background-repeat: no-repeat;
  background-size: cover;
  background-position: top center;
  background-attachment: fixed;
  background-image: url(../img/header_bg.jpg);
  overflow: hidden;
}

jQuery

var pContainerHeight = $('#header_container').height();

$(window).scroll(function(){

  var wScroll = $(this).scrollTop();
  var wZoomIn = 1+(wScroll*.0005);
  var wZoomOut = 1-(wScroll*.00005);

  if (wScroll <= pContainerHeight) { 
    $('.header_back').css({
      'transform' : 'scale('+ wZoomOut +')'
    });
    $('.logo').css({
      'transform' : 'translate(0px, '+ wScroll /0.7 +'%)'
    });
  }

Answer №1

By making some adjustments to the code, I achieved a smooth parallax zoom effect while scrolling using the background-size property instead of transform: scale(). The technique involves initially zooming in the background image slightly (e.g. 105%) and then gradually zooming it back out to 100% as the user scrolls down. I ensured that the background-size never goes below 100%, ensuring that it continues to cover the entire header area.

CSS

...
.header__back {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: url("http://www.adamkhourydesign.com/test/img/header_bg.jpg") top center no-repeat fixed;
    background-size: 105%; /** the initial zoom **/
    overflow: hidden;
}
...

Javascript

var height = $('#header_container').height();
var logo = $('.logo');
var background = $('.header__back');

$(window).on('scroll', function() {
    var scroll = $(this).scrollTop();

    logo.css('transform', 'translateY(' + scroll / 0.7 + '%)');

    var backgroundSize = 105 - (5 - (height - scroll) / height * 5);
    backgroundSize = backgroundSize < 100 ? 100 : backgroundSize;
    background.css('background-size', backgroundSize + '%');
});

Experience the parallax zoom effect by visiting this fiddle link: parallax zoom out demo

Answer №2

Shout out to Arnell Balance for your helpful answer that enabled me to make the necessary adjustments to my code and get it up and running smoothly.

I made some modifications by extracting the background image from the Background property and inserting it as an image within the div. Next, in the CSS, I set the image width to 140% and height to auto, then adjusted the left margin to offset half of the amount exceeding 100%, which in this case was -20%.

The revised code now appears like this:

HTML

    <header id="header_container">
            <div class="header_back">
                <img src="img/header_bg.jpg">
            </div>
            <div class="logo"></div>
   </header>

JQUERY

var pContainerHeight = $('#header_container').height();

$(window).scroll(function(){

  var wScroll = $(this).scrollTop();
  var wZoomIn = 1+(wScroll*.0005);
  var wZoomOut = 1-(wScroll*.0005);

  if (wScroll <= pContainerHeight) { 
    $('.header_back img').css({
      'transform' : 'scale('+ wZoomOut +')'
    });
    $('.logo').css({
      'transform' : 'translate(0px, '+ wScroll /0.7 +'%)'
    });
  }

CSS

#header_container {
  position: relative;
  height: 600px;
  width: 100%;
  background-color: rgba(255, 100, 85, 0.5);
  background-repeat: no-repeat;
  background-size: auto 800px;
  background-position: top center;
  background-attachment: fixed;
  overflow: hidden;
}

.logo {
  height: 100px;
  width: 100%;
  background-image: url(../img/name.png);
  background-position: center;
  background-repeat: no-repeat;
  position: absolute;
  top: 50%;
  margin-top: -50px;
}

.header_back {
  position: relative;
  height: 100%;
  width: 100%;
  background-repeat: no-repeat;
  background-size: 100%;
  background-position: top center;
  background-attachment: fixed;
  background-color: rgba(154, 100, 85, 0.5);
  text-align: center;
  overflow: hidden;
}
.header_back img {
  height: auto;
  width: 140%;
  margin-left: -20%;
}

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

Is it possible to manipulate the content inside a div tag using JavaScript in HTML?

What is the most efficient way to dynamically adjust a data offset value within an HTML tag using JavaScript? For example: <div class="box" data-offset="2s"></div> I am looking to update the value within the data-offset attribute based on s ...

Mysterious JQuery attribute

I've come across a piece of code that utilizes an unfamiliar selector that I can't seem to figure out: $("div[tag=" + someVariable + "]").css("display", "block"); From what I gather, the selector is searching for a div element with an attribute ...

Transforming the MUI CircularProgress into a half circle shape

After utilizing CirculaProgress, I was able to achieve the following: Is there a simple method to transform it into a semicircle like shown here? ...

Enhancing jquery datatable functionality with data-* attributes

I successfully added an id to each row of my data table using the rowId property, as outlined in the documentation. $('#myTable').DataTable( { ajax: '/api/staff', rowId: 'staffId' } ); Now I am wondering how I can ad ...

Revolutionary scroll-based navigation fill transition

I'm attempting to achieve an effect where the fill color of the "insticon" SVG changes from black to white based on the scroll position indicated in the jQuery code. I have made the necessary modifications in the if and else statements, but unlike "he ...

Display issue with ul and li elements in Safari browser causing menu to appear incorrectly

Although I am new to CSS, I managed to create a menu on my website that hovers over the background video successfully. It is a basic menu using a ul with li elements and it appears as intended in all browsers except Safari. In Safari, the menu items stack ...

The <hr> element creating a line beneath a connected div

Currently, I am in the process of designing a website with three main divs all on one page. To visually separate these divs, I have included a subtle horizontal line. The horizontal line between the first and second div looks correct as intended. However ...

Does anyone know of a CSS/JavaScript framework that can be used to replicate the look and functionality of the iOS home

I'm wondering if there is a CSS/JavaScript framework that replicates the layout of an iOS home screen. I want to create a kiosk website with a grid layout similar to Android/iOS where apps can be displayed as icons. ...

The boundary extends fully to the right

Recently, I created a calculator to convert kilograms to pounds and encountered an issue when trying to add a border to a p element. The problem was that the border extended all the way to the right side of the page. Here is the corresponding HTML code: &l ...

Tips for generating numerous sub div tags using jQuery

Is there a way to achieve this output with jQuery? I have working code here: . (See image for reference: https://i.stack.imgur.com/eOXiN.png) When trying to replicate the same using jQuery, I encountered an issue where there is no spacing between two imag ...

Insert an HTML page into a div element

I am having an issue with my angular template. I have a div where I am attempting to load an HTML view file (.html) based on a $watch event, but for some reason, the HTML view is not being loaded into the div. Below is a snippet of my controller code that ...

Should position: absolute; be utilized in asp.net development?

I am just starting out in web development Would it be a good idea to utilize position: absolute; for controls? If not, can you explain why? ...

CSS - Combining underline, striikethrough, and overline effects with customized styles and colors within a single element

I am trying to achieve a unique design with only one <span> that has three different text decorations (underline, strikethrough, and overline) like this: (This is just an example, I need it to be changeable) https://i.stack.imgur.com/61ZnQ.png Ho ...

How can I retrieve the value of a <span> element for a MySQL star rating system?

Source: GitHub Dobtco starrr JS plugin Implementing this plugin to allow users to evaluate a company in various categories and store the feedback in a MySQL database. Enhancement: I have customized the javascript file to enable the use of star ratings fo ...

Updating and managing the CSS style for button states

In my asp.net application, I have 5 list items functioning as tabs on a Masterpage. When a user clicks on a tab, I want to redirect them to the corresponding page and visually indicate which tab is active by changing its class. What is the most effective ...

Incorporating Ajax comments into an Ajax-powered status feature

I'm currently in the final phase of implementing ajax in my feed. Originally, I thought it would be a simple matter of pasting the comment input, but it turned out to be more complex than I anticipated. I placed the input below the main ajaxed status ...

Setting a JavaScript variable to null

Using Jquery, I have a variable that is assigned a value on button click. After the code executes successfully, I need to reset the variable to null. $("#btnAdd").click(function () { var myDataVariable= {}; myDataVariable.dataOne="SomeDa ...

Steps on how to trigger an onmouseover event for entire blocks of text:

I'm currently in search of an onmouseover code that seems to be elusive on the vast internet. A CSS box format has been successfully created: .box { float: left; width: 740px; height: 300px; margin-left: 10px; margin-top: 10px; padding: 5px; border: ...

How do I apply a xPage page style using the styleClass attribute instead of directly using the style attribute

Would like to know how to change the background color of an entire page using styleClass instead of directly in the xp:view tag? Here's an example that works: <xp:view xmlns:xp="http://www.ibm.com/xsp/core" style="background-color:#f1f1f1;"> ...

What is the purpose of using overflow:hidden; in our code?

I am curious about the rationale behind using these three properties on this menu. I don't quite understand why they are necessary. I am eager to delve into the technical reasoning behind this. margin: 0; padding: 0; overflow: hidden; Snippet of HTM ...