Scroll and Zoom effect in CSS

For basic styles adding bootstrap UI cdn.

And for DOM manipulation, adding jquery cdn in the header of html document.

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">


<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.slim.min.js"></script>

Adding main image

Adding some high quality image from google search results.

  <div class="header" id="parallex">
    <img class="header-image" src="https://handluggageonly.co.uk/wp-content/uploads/2015/12/London-1.jpg" alt="">
  </div>

Adding some random content

Adding some lorem ipsum content to make the page looks longer. So that we can scroll and see the effect.

<p class="container">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Recusandae similique perferendis quaerat porro, facilis quidem itaque qui excepturi autem ab iure ea quae vero dolorum fugiat alias accusantium, unde laborum facere pariatur soluta quibusdam
    numquam! Rerum iure similique, magnam tempore voluptatem molestias adipisci velit eaque ipsam eum sed consequatur ducimus magni enim cumque! Modi quia quibusdam praesentium ab quis quasi magni enim velit, rem numquam accusantium expedita labore suscipit.
    Dolores repellendus itaque, facere aliquid at nihil obcaecati voluptatibus laborum excepturi sapiente molestiae eius non sequi architecto delectus, repellat, iste ducimus iusto cum. Fuga animi soluta dolorem, ratione dolorum repellat voluptatum eaque
    esse aperiam impedit ex, blanditiis dignissimos ab nostrum. Quo adipisci nostrum repellat accusamus voluptatibus eius error sed! Architecto sed ut obcaecati porro ipsum alias eos at neque quae, veritatis, soluta! Harum tenetur quae maxime laborum
    ipsum nostrum laudantium repellendus quas cum adipisci est maiores, tempore tempora enim autem consequatur qui aperiam. Explicabo odit dolorem, assumenda veritatis dolor rerum maiores nobis impedit delectus eos quidem alias necessitatibus hic repellat
    aut a obcaecati, ullam distinctio, nisi, dolore facere tenetur. Illo sequi ab consequuntur et corrupti id dicta impedit qui quo, quasi sapiente cumque nulla adipisci ipsum voluptatem est atque architecto voluptas recusandae accusantium. Accusamus
    cupiditate, doloremque quae magni. Earum enim, quidem eveniet expedita! Labore aliquam, animi cum architecto repellendus possimus eos temporibus vitae sapiente optio dolorum dolorem qui alias, ratione quas voluptatibus incidunt recusandae amet praesentium
    accusantium enim esse sit magnam. Tempore iure, facilis placeat ipsa, laborum mollitia. Ipsam quos nostrum temporibus ex ipsum velit dignissimos maiores, facere, architecto dolorem mollitia repellendus necessitatibus officia accusantium expedita assumenda
    ipsa laborum nesciunt quam numquam consectetur. Id vero impedit, assumenda architecto eaque laborum deserunt commodi laboriosam itaque adipisci, omnis aperiam. Nostrum obcaecati est, dicta numquam ea cumque nemo sint, maiores mollitia hic. Quod at
    fugit iste. Obcaecati debitis quos facere delectus nemo beatae numquam. Vero excepturi obcaecati amet, consequatur velit eveniet sunt optio sint consequuntur alias eligendi voluptas voluptates, facere? Consequuntur corrupti a quis, enim? Officiis
    ea incidunt consequatur maiores, esse reiciendis magni, distinctio aut neque facere odio eius dicta natus provident consequuntur voluptatum ducimus tenetur, totam dolorum corporis animi expedita repellat! Dolorem inventore molestias aliquid, distinctio
    libero quisquam, facere iste earum delectus assumenda, quis nobis totam unde vero nostrum quas corrupti! Adipisci omnis sequi nemo odio iusto cupiditate dolorum voluptatibus nesciunt pariatur amet ipsam dignissimos eum facilis est iste ea delectus,
    itaque eius! Reiciendis facilis voluptas, expedita optio. Ducimus omnis facere illo commodi ut sequi veritatis quae nam et quasi delectus similique, ex voluptatibus odio recusandae officia consectetur voluptate nesciunt qui soluta ipsam. Aliquid rerum
    accusamus esse suscipit error nihil maxime sequi, illo illum aspernatur officiis ad, culpa dignissimos ratione, veniam. Ea dolore aperiam, qui. Sit aspernatur repudiandae atque rerum accusamus eum quos illum aliquid mollitia quis ratione pariatur
    praesentium totam beatae labore expedita, quidem provident hic veritatis officia corrupti magni facilis dolor incidunt voluptatibus! Obcaecati incidunt aspernatur corporis asperiores quaerat iste, ratione necessitatibus quibusdam ipsa nobis accusamus
    laborum est, rerum similique sapiente.</p>

So far we have added the HTML content. Lets add some CSS magic.

CSS magic

body: {
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}

img {
  width: 100%;
}

.header {
  height: 500px;
  overflow: hidden;
}

p {
  font-family: tahoma;
  font-size: 20px;
  padding: 50px;
}

Basically, this CSS sets the document height and width, and keeping the image stretch to 100%.

So far we have added some HTML text content and image in header. To add some scroll and zoom effect, lets add some javascript/jQuery code.

jQuery code

$(window).scroll(function() {
  var scroll = $(window).scrollTop();
  console.log(scroll);
  $("#parallex img").css({
    width: 100 + scroll / 5 + "%"
  });
});

The above code, adds the width of image based on the windows scroll position.

Final Output

You can checkout the final output in the below codepen.

Thanks for reading. Please share your feedback in comment section below.

Parallax & Zoom effect

Parallax & Zoom effect is one of the popular effect to add in the homepage or single blog page.

In this post, we will see how we can achieve a simple Parallax & Zoom effect.

For this, we need a wide image which can serve in the top of page. I’m using jQuery for custom scripts and bootstrap for simple layout view in html.

<pre class="wp-block-syntaxhighlighter-code"><head>
  <link href="<https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css>" rel="stylesheet" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
  <a href="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.slim.min.js">https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.slim.min.js</a>
</head>
</pre>

The image used in the code snippet is grabbed from google image search. And we need tons of text or words to present in the page. So that it will looks like page full of content and also we will get vertical scroll bar.

<div class="header" id="parallex">
    <img class="header-image" src="<https://handluggageonly.co.uk/wp-content/uploads/2015/12/London-1.jpg>" alt="">
  </div>

<p class="container">
		Lorem ipsum dolor sit amet, consectetur adipisicing elit. Recusandae similique perferendis quaerat porro, facilis quidem itaque qui excepturi autem ab iure ea quae vero dolorum fugiat alias accusantium, unde laborum facere pariatur soluta quibusdam
</p>

I’m getting that bunch of text from Lorem Ipsum generator from internet. (there is plenty site to generate random text). If you are familiar with emmet, you can generate using that as well.

Next thing, we need to add some CSS magic to make our html looks decent. I’m adding width of image as 100% and if it leads or overflows the screen – we are hiding it.

body: {
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}
img {
  width: 100%;
}
.header {
  height: 500px;
  overflow: hidden;
}
p {
  font-family: tahoma;
  font-size: 20px;
  padding: 50px;
}

Next big thing, lets capture the scrolling happening in website using jQuery and zoom the header image to apply Parallax effect.

$(window).scroll(function() {
  var scroll = $(window).scrollTop();
  $("#parallex img").css({
    width: 100 + scroll / 5 + "%"
  });
});

Here, we are adjusting the CSS width property of Parallax image based on scroll happens by 5%. So the image will start scaling or zooming whenever the scroll down or scroll up happens.

Here is the full demo with code.

Happy Coding!