What is the best way to enlarge an image and have it perfectly fill the screen while scrolling?

Having a background in photography and using SVG graphics as a mask, I have implemented an image as the background-image and the svg mask as a mask-image on the same element. The result is displayed below:

My query pertains to expanding this image to fit the screen upon mouse scroll. Are there any third-party plugins required to achieve this?

While I have utilized JavaScript, the desired outcome has not been attained.

window.addEventListener('scroll', function() {
        const el = document.querySelector('.el');
        const scrollPercent = window.scrollY / (document.documentElement.scrollHeight - window.innerHeight);
        const newSize = (100 + scrollPercent * 100) + 'vw';
        el.style.width = newSize;
        el.style.height = newSize;
});
* {
  box-sizing: border-box;
}
body {
  margin: 0;
  background: #8f7a66;
}

.el {
  width: 100vw;
  height: 100vh;
  padding: 1rem;

  background-image: url(https://images.unsplash.com/photo-1528287942171-fbe365d1d9ac?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&w=1200&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ);
  background-size: contain;
  background-position: center;
  background-repeat: no-repeat;

  mask-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/sun.svg);
  mask-size: 50vmin;
  mask-repeat: no-repeat;
  mask-position: center;
}
<div class="el"></div>

Answer №1

Update:

You have successfully implemented the changes in the provided codepen

A few additional pointers:

  • mask-* properties need a vendor prefix
  • Utilize css variables for better flexibility
  • Suggest adjusting the upper limit (100) to a different value

The specified value represents the total percentage required to accommodate the largest dimension within the inner part of the svg.

For instance, if the inner part equals 50% of the svg size, set the limit to 200%

const el = document.querySelector('.el');
let maskSize = 100;
el.style.setProperty("--mask-size", `${maskSize}%`);
const limit = 200;



window.addEventListener('wheel', function(event) {
  event.preventDefault();

  const scrollAmount = event.deltaY;
  maskSize += scrollAmount * -0.1;

  maskSize = maskSize < 0 ? 0 : maskSize > limit ? limit : maskSize;



  el.style.setProperty("--mask-size", `${maskSize}%`);
});
* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: #8f7a66;
}

.el {
  width: 100vw;
  height: 100vh;
  padding: 1rem;
  background-size: cover;
  background-position: top center;
  background-repeat: no-repeat;
  background-image: url(https://images.unsplash.com/photo-1528287942171-fbe365d1d9ac?ixlib=rb-1.2.1&q=85&fm=jpg&crop=entropy&w=1200&cs=srgb&ixid=eyJhcHBfaWQiOjE0NTg5fQ);
  -webkit-mask-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/sun.svg);
  -webkit-mask-size: var(--mask-size);
  -webkit-mask-repeat: no-repeat;
  -webkit-mask-position: center;
  mask-image: url(https://s3-us-west-2.amazonaws.com/s.cdpn.io/3/sun.svg);
  mask-size: var(--mask-size);
  mask-repeat: no-repeat;
  mask-position: center;
}
<div class="el"></div>

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

How to achieve a reverse slideToggle effect with jQuery when refreshing the page

After creating a custom menu on Wordpress using jQuery slideToggle to toggle dropdown on hover, everything seemed to be working perfectly. However, I noticed that when I refreshed the page while moving my cursor between two menu items with dropdown menus, ...

The carousel indicators in Bootstrap 4 are not responding to the CSS styles

I'm having trouble customizing the bootstrap 4 carousel indicators to look like the example image below. https://i.sstatic.net/Xdmim.png .carousel-indicators { width: auto; padding: 8px 18px; background-color: rgba(96,107,114,.8); ...

Concerning geoext

I am currently working on creating a framework for overlaying layers on the web. I am using geoserver to publish the layers and the geoext tree.js example to display all the layers in a tree-like panel. However, I'm encountering an issue with zooming ...

Obtaining a document through an Anchor Element

I am currently implementing a feature to allow users to download a zip file using an anchor tag. Here's how it's set up: <a href="download/sample.zip">sample.zip</a> When the file exists in the download directory, everything works s ...

Is there a way to adjust the text color of a label for a disabled input HTML element?

If the custom-switch is active: the label text color will be green. If the custom-switch is inactive: the label text color will be red. A JavaScript (JQuery) method call can be used to activate one button while deactivating another. The Issue It appe ...

Element remains hidden until the developer console is activated

On my website, I've noticed that certain LayerSlider elements are remaining invisible until: The window is resized I disable the Bookmarks bar in Chrome (quite strange, right?) I switch on the Chrome debugger tools This issue is not exclusive to Ch ...

Soundtrack featured on the main page

Currently, I am trying to incorporate an audio file into my homepage without interrupting its playback when the user navigates to another page or title. Essentially, I want the audio .mp3 file to continue playing in the background seamlessly. I am currentl ...

How can I evenly align each <td> in a Bootstrap table?

Looking at the image below, you can observe that the red lines indicate the uneven width of each <td>. My objective is to align each <td> evenly. How can I achieve this? CSS .stickyFooter { background: #c1c1c1; positio ...

Obtain the controller name from the current scope

I am trying to assign a controller named in the scope of another controller JavaScript file: .controller('PageCtrl', [ '$http', '$scope', '$routeParams', '$location', function($http, $scope, $ro ...

What would be the best way to display a label once the zoom level exceeds a certain threshold in Leaflet?

As someone new to the Leaflet library and JavaScript in general, I'm currently facing a challenge with showing/hiding a leaflet label based on the zoom level. The markers are within a 'cluster' layer loaded via AJAX callback, and I bind the ...

Troubleshooting common issues with PHP's simple HTML DOM parser

I've encountered an issue while working on a scraper for a website that involves crawling through links. The error message I'm receiving is as follows: PHP Fatal error: Uncaught Error: Call to a member function find() on null in D:\Projekti ...

Position the Navbar above the Logo and ensure it stays in place when resized

How can I position the navbar over the logo so that it aligns perfectly under the letter "g"? I want to ensure that this positioning remains consistent even when resizing the browser or viewing on different screen sizes, without impacting the content belo ...

What other option can be used in place of white-space:nowrap?

When using a textarea with the CSS property white-space:nowrap, it successfully removes spaces but adds a horizontal scrollbar to the text. I want to avoid the horizontal scrollbar while also preventing scrolling using arrow keys when using overflow-x:hi ...

What is the precise description of the CSS property font-size when measured in pixels?

When defining the CSS property font-size, it can be set to a specified length in pixels. What exactly does this measurement describe? Is it the width or height of a particular character (such as 'm'), is it an attribute within the font file desc ...

Issue with horizontal scrolling functionality in DataTables

I have a table with over 10 columns, and I am using datatables to display the data. I have enabled horizontal scrolling due to the large number of columns, but the scroll is not appearing. Can someone please assist me with this issue? Here is a screenshot ...

Controlling HTML5 Video Playback with JavaScript: Pausing at Custom Frames

I need to implement a feature in my web application where I can pause an HTML5 video at specific frames using pure JavaScript, not based on minutes. <video id="media-video" controls> <source id="media-source" src="media/video.mp4" typ ...

Modify The Source Code of an Image

HTML Markup <img class="blog-picture ul-normal-classic" src="https://example.pk/wp-content/uploads/2020/12/example300-300x203.jpg" alt="Beintehaa300"> What I'm Looking For: <img class="blog-picture ul-norma ...

Why Isn't This JPG Image Functioning Properly in HTML/CSS?

I am facing an issue with a JPG image that I am attempting to use in CSS as a background-image for a div container. For some reason, this particular image refuses to render in the viewport. Interestingly, when I inspect the element, I can see its thumbnail ...

Incorporating Cascading Style Sheets (CSS) to

I'm looking to style my form with CSS but I'm unsure of the process. Currently, the form is generated using PHP and MySQL, and in the browser it appears like this: http://gyazo.com/5d099ead9bd6ea83859a5114b2438748 I want to align the text and dr ...

Customizing CSS for Internet Explorer browsers

I am looking to target specific CSS styles for Internet Explorer only, without affecting other browsers. Initially, I tried using conditional comments: <!--[if lt IE 7 ]><html class="ie6 ie"> <![endif]--> <!--[if IE 7 ]><html cl ...