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

Consistent dimensions for columns in a table, automatically adjusting based on content

Can we ensure that all rows and columns maintain equal height, whether or not they contain an image? If you need a visual representation of this concept, refer to this example: [] [] [] ...

Is dataType and contentType necessary in an ajax request?

Just started learning about web development and I have a question regarding ajax calls. Specifically, when making an ajax call, do you need to specify the dataType and contentType? I'm slightly confused as my server-side code includes a servicestack e ...

Node.js NPM Google search results showing [ Object object ] instead of actual result

searchOnGoogle: function(searchQuery){ googleSearch.query({ q: searchQuery }, function(error, response) { console.log(response); botChat.send ...

Which UL should be selected next?

I am facing an issue with a trigger that is meant to target the next UL element and apply effects to it. <div id="archives_trigger"><a class="menu_trigger">Monthly Archives</a></div> <ul id="archives_menu"> <?php wp_ ...

Implementing time-limited class addition and removal using jQuery

Upon clicking an icon, a search box should appear. It will then automatically disappear after a certain interval of time (e.g., 5 seconds), even if the user clicks elsewhere on the page. $(".icon").click(function()){ $("search-box").addClass("active ...

I possess 9 captivating visuals that gracefully unveil their larger counterparts upon being clicked. Curiously, this enchanting feature seems to encounter a perplexing issue within the realm of web browsing

<style type="text/javascript" src="jquery-1.11.0.min.js"></style> <style type="text/javascript" src="myCode.js"></style> </body> //jquery is within my site directory on my desktop $(document).ready(function(){ //note: $("#ar ...

Unable to display content in div using jQuery ajax function

I am currently working on a script called support_question.php $('#topic').on('change', function() { var sel = $(this).find('option:selected').val(); $.ajax({ url: "support_process.php", type: "POST" ...

What is the reason behind the non-reversible nature of atob and btoa

Looking for a simple way to obscure answers to quiz questions in Markdown temporarily? The idea is to reveal the answers during the presentation, no need for secure encryption. Considered using atob('message I want to obfuscate') and letting stu ...

The Java.lang.finalize heap became too large during the Hadoop URL parsing task

I'm currently working on analyzing the content of various homepage URLs by utilizing a Hadoop mapper without a reducer. This mapper fetches the URLs and sends them to a parser class for further analysis. The parser leverages Jericho's html parse ...

Customize the CSS for MaterialUI's TablePagination's MenuItem

Having trouble figuring out how to override CSS for the child component MenuItem within TablePagination? Check out this link for more information: https://github.com/mui-org/material-ui/blob/master/packages/material-ui/src/TablePagination/TablePagination.j ...

jQuery performs perfectly in Chrome but encounters issues in IE9/IE8 and other older versions of Internet

I've implemented this lengthy jQuery script to enable dynamic dropdown loading and updating when selections are changed. However, I'm facing issues in Internet Explorer where the script loads the dropdowns initially but doesn't trigger oncha ...

Handle improperly formatted XML in Perl using Perl-XML

Using the perl command line utility xpath, I am extracting data from HTML code in the following manner: #!/bin/bash echo $HTML | xpath -q -e "//h2[1]" The HTML is not well-formed, causing xpath to throw the error below: not well-formed (invalid token) a ...

How can I translate these JQuery functions into vanilla JavaScript?

I am currently in the process of building a configurator using transparent images. The image configurator functions through a basic JQuery function: I have assigned each input element a data attribute called data-Image. This function identifies the data-Im ...

Adjusting the navigation image as it passes through various div elements during scrolling

Is it possible to dynamically change an image in the navigation bar based on the user's scroll position? For example, I want pic1 to be displayed when the page content is at the top, then switch to pic2 once the user reaches the footer, and then back ...

What is the best way to export Class methods as independent functions in TypeScript that can be dynamically assigned?

As I work on rewriting an old NPM module into TypeScript, I encountered an intriguing challenge. The existing module is structured like this - 1.1 my-module.js export function init(options) { //initialize module } export function doStuff(params) { ...

The attribute 'sandwiches' cannot be found within the data type 'string'

In my app, I require an object that can store strings or an array of strings with a string key. This object will serve as a dynamic configuration and the keys will be defined by the user, so I cannot specify types based on key names. That's why I&apos ...

Using an array of objects to set a background image in a Bootstrap carousel using jQuery: a step-by-step guide

I have an array of items, each containing a background property with a URL to an image. Here is my array: https://i.sstatic.net/jfrV0.png Here is the HTML structure: <div id="myCarousel" class="carousel slide" data-ride="carousel"> <ol ...

Require modification of JSON values in React Promise code

Looking to modify the data returned from a promise and wrap a link around one of the fields. Here is the React code: this.state = { medications: [], } queryMeds().then((response) => { this.setState({medications: response}); }); The response c ...

Building a Dynamic Web App with PHP and Vue.js

I developed an API using PHP and you can access it through this link: However, I encountered an issue when trying to display the data on the front-end of my Vue.js (Home.vue) file using axios. Below is the code I used: <ul class="ta-track-list" v-if= ...

What is the technical process behind conducting A/B testing at Optimizely?

I'm currently experimenting with Google Analytics and Content Experiments for A/B testing on my website, but I'm encountering some challenges in making it seamless. To utilize the Google API properly, a few steps need to be taken. Firstly, I nee ...