Adjusting size of a div as you scroll - Java Script

Using the codepen provided at http://codepen.io/anon/pen/fDqdJ, I am looking to enhance the functionality by implementing a feature where, upon reaching a specific distance from the top of the page, an already animated div undergoes a change in scale while moving.

I am facing challenges with the syntax and incrementing the scale of the animated div during scrolling. I believe that leveraging CSS transform is essential, but I require assistance!

Refer to the JavaScript example below:

var $window = $(window);
var $box = $("#box");

$window.scroll(function() {
    var scrollTop = $window.scrollTop();
    console.log(scrollTop);
    if (scrollTop >= 250 && scrollTop < 400) {
      $box.css({top: -250 + scrollTop});      
    } else if(scrollTop >= 400 && scrollTop < 460) {
      $box.css({left: (10+(scrollTop-400)/2)+"%"})
    } else if(scrollTop >= 460 && scrollTop < 580) {
      $box.css({top: (50+(scrollTop-460)/2)+"%"})
    } else if(scrollTop >= 580 && scrollTop < 620) {
      $box.css('transform', 'scale(' + whateverTheScaleShouldBe + ')');
    }
});   

Below is the HTML structure:

<div class="wrapper">
  <div class="row" id="row1">
  </div>
  <div class="row" id="row2">
    <div id="box"></div>
  </div>
  <div class="row" id="row3">
  </div>
  <div class="row" id="row4">
  </div>
</div>

Any assistance would be greatly appreciated! :)

J

Answer №1

The issue with the given code snippet:

$box.css({transform:scale(1.1)})

...lies in the fact that the value provided is incorrect. The value set for the CSS property should be a string. Currently, the code attempts to invoke a non-existent scale function in JavaScript. To utilize the CSS scale function correctly, replace the line of code with either one of these options:

$box.css({transform: 'scale(1.1)'});

Or a simpler version:

$box.css('transform', 'scale(1.1)');

This will update the CSS scale property accordingly. If you wish to make the scale dynamic based on certain calculations, you can modify the code as follows:

$box.css('transform', 'scale(' + whateverTheScaleShouldBe + ')');

Here, whateverTheScaleShouldBe represents your desired calculation for scaling, which was not specified in your question. For instance, if you intend for the scale to increase linearly by 0.1 every 100px scrolled, you could implement this solution:

scaleAmt = 1.0 + (scrollTop / 100);
$box.css('transform', 'scale(' + scaleAmt + ')');

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

Macy.js, a masonry library, experiences compatibility issues with Internet Explorer 11. However, the problem can be resolved by activating "inspect mode"

Having an issue with macy.js on my website. The masonry element doesn't work on IE11 initially, but when I toggle the "inspect element" feature on and off, it starts working suddenly. Is there a way to trigger it automatically right after the website ...

Include vue-videobg in Vuetify using CDN without the need for webpack

I am trying to integrate vue-videobg into vuetify using a CDN without any additional requirements like "magic_videobg_to_install.java.css.js.htm" <!DOCTYPE html> <html> <head> <link href="https://fonts.googleapis.com/css?family=R ...

Is there a way to enable data-add-back-btn using jQuery script?

Currently, I am utilizing jQuery 1.9.1 and jQuery Mobile 1.3.1 to define multiple pages within my project setup: <div id="q1" data-role="page" data-add-back-btn="false" data-back-btn-text="Home"> <div data-role="header" data-position="fixed" ...

Move the box upwards and change it to grayscale when hovering over the image, then reverse the effect when the hover

As I hover over the image, my goal is to make a gray box appear at the bottom and smoothly slide up. While it slides up, everything it covers should turn grayscale. When I move my mouse away from the image, I want the gray box to slide back down and remove ...

Looping through PHP code that generates the same ID twice in a random order

I am currently working on developing a memory game and have encountered an issue. I need to loop through all the images with unique ids (2x id1, 2x id2, 2x id3, etc.) and assign random ids to each image. Here is the PHP code snippet: class Kaart { pu ...

What is the best method to center a div on the screen?

Is there a way to have a div open in the center of the screen? ...

Unable to convert imported JSON file into an array

Looking to create an array from a file, the following code snippet serves the purpose: 'use strict'; const fs = require('fs'); let results = []; fs.readFile('myfile.json', (err, data) => { if (err) throw err; res ...

Exploring Material-UI: Leveraging makeStyles and CSS-In-JS for customizing library component styles

I'm currently utilizing Material-UI to construct a form using makeStyles and CSS-In-JS for styling purposes. I have a form component from the Material-UI library that I want to style according to my needs. My main focus is on how to address classes or ...

Utilizing the .on() method in jQuery within this specific situation

I am interested in learning how to point this to the selected element (event source) within the .on() method without needing to define an anonymous event handling function. For instance, in the code below, the context of my selected element is document, m ...

Using PHP to swap out HTML elements

I need a solution to display text with HTML tags as plain text, without the browser interpreting them. I want the output to be similar to how HTML code viewers render it. For example: replacing <div> with <span><</span><span>d&l ...

Warning from Vue: Steer clear of directly changing a prop as it will be replaced whenever the parent component undergoes re-rendering

I've been diving into Vue.js, but I'm encountering an issue. Every time I try to click on the "edit age" or "change my name" button, I get the following error message. [Vue warn]: Avoid mutating a prop directly because the value will be overwrit ...

Accessing a specific value based on a selected option in Angular 2+

Examining the Angular 2+ code snippet below, there is an Array defined in TypeScript: models = [{idModele: 1, model: "Bianca", price: 500}, {idModele: 2, model: "etc", price: 600} ] The query here is about setting up a mechanism where selecting a specifi ...

Learn how to effectively showcase various components by leveraging the new react-router-dom v6.0.0 alongside react-redux

My issue is that when I click on a link to render different components, the URL updates but the UI remains unchanged. No matter which item I click on to render, the same thing happens. I've tried numerous solutions to fix this problem without success. ...

Express app unable to receive data from Ajax request

I'm currently in the process of developing an application with React and Express. The issue I'm facing is that when I make an Ajax request from a React component to a specific Express route, the data doesn't seem to be reaching the route han ...

Issue arises during initialization with Slick.js

I recently attempted to implement the slick.js function on my webpage by following the guidelines provided on . I copied the "center mode" function as my Jquery function, but unfortunately, the arrows/buttons and nav-docs were not generated in my slideshow ...

Can I rely on setTimeout to guarantee the execution of a task in NodeJS?

I am working on a REST backend using ExpressJS. One of the functionalities of this backend is to allow users to upload file assets, but these files should only exist for 10 minutes. Is it secure to rely on setTimeout to automatically delete the uploaded f ...

Nuxt's axios is encountering difficulties in managing the server's response

Having just started working with Nuxt.js, I encountered an unusual issue. There is an endpoint in my backend API that allows users to reset their password by sending a token along with a new password. Although the request is being sent correctly and the s ...

Tips for retrieving multiple independent response data in Express js app.get callback

What is the optimal way to send two independent MongoDB results in an Express application via HTTP Method? Check out this concise example to clarify: //app.js var express = require('express'); var app = express(); var testController = require(& ...

Struggling to format pictures with instafeed.js

I am currently working on a website section that utilizes instafeed.js to display the three most recent images from an Instagram account. The images are loading correctly, but I need some guidance on how to style them appropriately. Unfortunately, the HTML ...

Solutions for concealing the current div when clicking on a new div that reveals a fresh one

Is there a way to hide the current div once you click on a new div that reveals another one? The code below toggles the display of the div, but what I am attempting to achieve is that when you click on a new item after clicking on the first one, the first ...