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

Accessing a remote network via ajax and establishing cookie permissions

I'm currently developing an application that pulls information from another system and embeds it in mine using iframes. It's crucial for me to be able to authenticate against the external system. Initially, I had a working solution where I used ...

What is the best method for caching inline SVG in web browsers?

Over the years, SVGs have remained popular due to their scalability. One key advantage of inline SVG is the ability to manipulate it with CSS and JS. Additionally, using the <use> tag allows for referencing the original element when repeating the sam ...

Utilize Server Side Includes in your JavaScript code

When the query string is parsed, a specific section of code SSI is included in the footer page. Here are some examples of query strings: ?headId=520&genderType=2 ?headId=600&genderType=1 function GetQueryStringParams(sParam){ var sPageURL ...

The SVG image exceeds the boundaries of the parent column div in bootstrap

I am struggling to make a SVG fit inside a column in a bootstrap div with col-lg-4. I tried using img-fluid, but it did not work as expected. The SVG should automatically adjust its size to fit the parent div. However, that is not happening. .star-ratin ...

What is the method for displaying an array in JavaScript?

When a user clicks the value 3 input box, three input boxes will appear where they can enter values. However, when trying to submit the array, no elements are printed and an error message "Element is not defined" appears. var arr = [0]; function get_va ...

Prevent the occurrence of a fixed height problem associated with the Bootstrap Navbar (Mega Menu)

In my project, I have a Custom Mega Menu that dynamically creates code to display all Nav bar entries. Since changing the logic of Mega menu creation is difficult, I am looking for a CSS solution to avoid fixed height in bootstrap columns. My code structu ...

How can I integrate Solr with jQuery AJAX?

Can jQuery Ajax be utilized to send GET parameters to Solr and dynamically update the content of the page without requiring a full page reload by the user? ...

Executing window.open from Html.Actionlink in ASP.NET MVC 4

Here is the code block I am working with: @foreach (System.Data.DataRow drEquipment in Model.EquipmentList.Rows) { <tr> <td valign="top">@drEquipment["ColumnName"]<br /></td> <td valign="to ...

CSS: the max-width property does not constrain an absolute positioned container from overflowing

I've been trying to figure this out for hours, but I can't seem to get it right. My top menu has submenus that include a popup menu. In one of my popups, I need the container to have a maximum width of 170 pixels and all the items inside to wra ...

Having issues with Inline-Block functionality?

I am facing alignment issues with an image and a container placed next to each other. The rest of my website uses inline-block without any problems. If someone could provide guidance on how to resolve this, it would be greatly appreciated. Below is the s ...

Tips on customizing the appearance of React rendering components

<div> <h3>{this.props.product.name}</h3> <h3>{this.props.product.code}</h3> {this.renderColors()} <article> <div da ...

The lack of definition for e.PreventDefault causes an error

In my code, I have a registerAjax(e) function: function registerAjax(e) { e.preventDefault(); let userData = { username: $("#username").val(), password: $("#password").val(), }; $.ajax({ method: "POST", url: kinveyBaseUrl + "user/" + kinve ...

Using Jquery to iterate through a dynamic list of elements

Currently, I am exploring the idea of creating a forEach loop that can handle an unspecified number of elements. The goal is to randomly select one element, perform XYZ actions on it, make it visible, and then eliminate it from consideration. This process ...

Make sure to delete all dynatable records before applying the latest update

I am currently utilizing the dynatable plugin and I am seeking guidance on how to clear (remove all records) from the table before inserting new records. At the moment, new records are being added to the existing ones. Here is my code snippet: response ...

Issue with Bootstrap tab display of content

I'm having trouble with the tabs in my page. When I click on each tab, the content doesn't display correctly. Here is my code: <div class="w470px exam" style="border: 1px solid #ddd; margin-top: 30px;"> <ul id="myTab" class="nav nav ...

what is the process for creating a dynamic display slide in bxslider?

I am trying to create a flexible length display using bxSlider. Here is the code I have so far. JS var duration = $('ul > li > img').data("bekleme"); $(document).ready(function () { $('.bxslider').bxSlider({ ...

Lost navigation hover effect when media size decreases

I followed a tutorial to create a responsive navigation menu that was working perfectly here: After adding a logo and some additional elements, I noticed that the hover effect was missing when the media size changes, which can be seen here: I suspect the ...

I am having trouble getting JavaScript to render properly in my single page application

In my home.js file, I have the following code snippet: define(['services/logger'], function (logger) { var vm = { activate: activate, title: 'Home View' }; return vm; //#region Internal Methods function activate() { ...

Utilizing PHP for a server-side backup in case the CDN is inaccessible

Looking to simulate some server-side functionality: <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script type="text/javascript"> if (typeof jQuery == 'undefined&apo ...

Erase blob_over from the Wordpress menu hover effect

I've created a unique style for the Donate button on this website, but I'm struggling to remove the hover effect. Any suggestions on where to start? Website URL: This is my Custom Class CSS: .donate { background:#4A1383; padding:0px 10px 0px 1 ...