What is the best way to zoom in on an image and make it move off the screen as I scroll through my webpage?

I am working on a website design where I would like to implement an image zoom effect as the user scrolls down. Additionally, I want the image to move to either the left or right side of the screen as it goes out of view when scrolling to the bottom. While I have successfully implemented the zoom in on scroll feature, I am struggling with moving the image to the sides. Any guidance on how to achieve this would be highly appreciated. Thank you.

EDIT:

Here is my progress so far in realizing the desired behavior for the image:

HTML:

<html>
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>

<body>
     <img src="http://www.freepngimg.com/download/plants/6-2-plants-free-download-png.png" class = "zoom">

    <script>
        $(window).on("scroll", function() {
            var s = Math.min(400, $(document).scrollTop()) + 200;
            $(".zoom").width(s).height(s);
        });
    </script>
</body>

CSS:

body{

width: 100%;
height: 1200px;
}

img {
    position: fixed;
    width: 200px;
    left: 0;
    bottom: 0;
}

Answer №1

I have created a sample for demonstration purposes. Please incorporate this HTML code:

<div class="wrapper">
<img class="image" src="https://www.mozilla.org/media/img/firefox/home/switch-
to-firefox.fb1c114dfd84.png">
</div>

This is the CSS:

body{
height:2000px;
}
.wrapper{
}
.image{
 height:150px;
 width:150px;
 position:relative;
 left:0; 
 }

And here is the JavaScript code snippet:

var oldScrollTop=0;
var newScrollTop;
$(window).scroll(function(e){

if($(window).scrollTop() <=150){ // You can adjust the viewport image height here

newScrollTop = $(window).scrollTop()
var step = newScrollTop - oldScrollTop


var imageHeight = $('.image').css('height');
var imageWidth = $('.image').css('width');
 var imageLeft = $('.image').css('left');

var NewimageHeight = parseInt($('.image').css('height')) - step;
 var NewimageWidth = parseInt($('.image').css('width')) - step;
 var NewimageLeft =  parseInt($('.image').css('left')) -step;

$('.image').css({"height":NewimageHeight ,"width":NewimageWidth,  
"left":NewimageLeft});
oldScrollTop = newScrollTop;
}
})

Lastly, you can access the implemented feature through this provided link:

https://jsfiddle.net/mustkeom/xy3dy4nL/32/

Answer №2

If I'm understanding correctly what you're attempting to achieve, maybe this solution could be helpful?

$(window).on("scroll", function() {
  var topPosition = $(document).scrollTop()
  var scaleValue = Math.min(400, topPosition) + 200

  $(".element-to-zoom").css({
    width: scaleValue,
    height: scaleValue,
    left: -topPosition * 2,
  })
})

I've made some adjustments to your code for better readability ;)

Please let me know if this aligns with your intended goal.

Answer №3

CSS Transition can be used to achieve this effect:

setTimeout(resizeAfterDelay, 1500)

function resizeAfterDelay() {
  $('div').addClass('move-left')
  
  setTimeout(moveAwayAfterDelay, 1500)
}

function moveAwayAfterDelay() {
  $('div').addClass('move-away')
}
div {
  width: 100%;
  height: 250px;
  background: lightcoral;
  transition: all .5s ease-in-out;
  transition-property: width, height, transform;
  transform: translateX(0);
}

div.move-left {
  width: 20%;
  height: 100px;
}

div.move-away {
  transform: translateX(-150px);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div></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

If you invoke revokeObjectURL, Chrome will fail to display Blob images

-----update------ After some investigation, I found that commenting out window.URL.revokeObjectURL( imgSrc ); fixed the issue in all browsers. It seems like Chrome was revoking the URL too early. I am curious to understand why this behavior occurs, and if ...

altering the directory for bower installations on specific repositories exclusively

Recently, I've been experimenting with Bower and at the same time exploring Polymer. If you want to download polymer elements using bower, you can use the following command: bower install --save PolymerElements/iron-image I assume there's a sp ...

What is the best way to open a link contained within a div by clicking on it

I am currently in the process of developing a website that is able to parse a Spanish dictionary. When looking up the word HOLA, the site provides the definition. However, for other words like CASA, users receive suggestions with links such as My goal is ...

How to parse JSON in JavaScript/jQuery while preserving the original order

Below is a json object that I have. var json1 = {"00" : "00", "15" : "15", "30" : "30", "45" : "45"}; I am trying to populate a select element using the above json in the following way. var selElem = $('<select>', {'name' : nam ...

Creating an HTML document with unique characters

I am currently facing an issue with writing special characters in HTML. My program is designed to edit an HTML file and allow users to replace content using a text box. However, I am struggling to add characters like "ç", "á", "ö", etc. These character ...

JavaScript module declarations in TypeScript

Recently, I delved into the world of a Node library known as bpmn-js (npmjs.com). This library is coded in JavaScript and I wanted to incorporate typings, which led me to explore d.ts files. My folder structure looks like this webapp @types bpmn ...

Mongodb failing to recognize the concat function

I have a field within my collection that looks like this: uniqueId: 123 inTarefa: true exclude: "ab,cd," orderId: 987 I am attempting to update all of the values using a "FindOneAndUpdate" query like so: collection.findOneAndUpdate({ 'uniqu ...

Create an object using a combination of different promises

Creating an object from multiple promise results can be done in a few different ways. One common method is using Promise.all like so: const allPromises = await Promise.all(asyncResult1, asyncResult2); allPromises.then([result1, result2] => { return { ...

Updating a class within an AngularJS directive: A step-by-step guide

Is there a way to change the class (inside directive) upon clicking the directive element? The current code I have updates scope.myattr in the console but not reflected in the template or view: <test order="A">Test</test> .directive("test", ...

Populate a dropdown list with array elements using Javascript in ReactJS

I am encountering an issue while trying to populate a dropdown with data from the 'options' array. The error message states that it cannot read property appendChild of null. Using 'document.getElementByClassName' instead of document.ge ...

Looking to enclose a search query in JavaScript using quotes, and then remove the quotes on the Python backend

Let's start with my code snippet. This is the HTML section: <form action= "/" onSubmit= "return validate(this);" method= "post"> <!--irrelevant from this point--> Below is the Javascript portion, located later in the file: <scri ...

Initiate an AJAX request to the server upon beforeunload event

It seems that starting with Firefox version 4, binding the window jQuery object to beforeunload is no longer effective. I want to send an AJAX post request to delete data from my server's memcache. When I refresh the only open tab, I can observe tha ...

Ways to utilize jQuery for selecting IDs that are not part of a specific class

I am currently working with a jQuery script that is designed to target all IDs containing 'Phone'. However, I am facing an issue where I need the selection to ignore those IDs if they are part of a specific class. The current code I have, from m ...

Unable to substitute a value using the useState hook in React

Whenever I click a key, I attempt to update the value of a variable but it appears not to be working as intended. ↓ The current implementation is not effective and needs improvement import React, { useState, useEffect } from 'react'; const Li ...

Expanding divs automatically depending on the content they contain

Currently, I have been developing a template for a database-driven page that is supposed to contain four divs for content arranged like this: ----- ----- | 1 | | 2 | ----- ----- ----- ----- | 3 | | 4 | ----- ----- The information in each box will always ...

Switch the background color when a radio button is chosen

For my quiz on test practice with an array of questions, each question includes five radio buttons, but only one is correct. Initially, all five options have a gray background. Upon selecting a wrong option, it changes to red. If another incorrect option i ...

Fill out the form field using an AJAX request

Whenever a specific business is selected from a dropdown list, I want to automatically populate a Django form field. For example: I have a list of businesses (business A, business B, ...) and corresponding countries where each business is located. Busin ...

best way to eliminate empty p tags and non-breaking spaces from html code in react-native

How can I clean up dynamic HTML content by removing empty <p> tags and &nbsp? The whitespace they create is unwanted and I want to get rid of it. Here's the HTML content retrieved from an API response. I'm using the react-native-render ...

Exploring asynchronous data handling in AngularJS using promises

Currently, I am working on a single page application using angularJS and encountering some difficulties in storing asynchronous data. In simple terms, I have a service that contains my data models which are returned as promises (as they can be updated asy ...

The magical form component in React using TypeScript with the powerful react-final-form

My goal is to develop a 3-step form using react-final-form with TypeScript in React.js. I found inspiration from codesandbox, but I am encountering an issue with the const static Page. I am struggling to convert it to TypeScript and honestly, I don't ...