Tips for incorporating symmetrical values using jquery

I am facing a challenge with the following markup:

<div class="container">
   <figure></figure>   
   <figure></figure>
   <figure></figure>
</div>

My task is to add a symmetrical element for each of the figures, but with different height and width values. The size of each new element should be reduced by about 10% in both width and height compared to its corresponding original image. Specifically, the first element should have 90%, the second 80%, and the third 70% of the initial size. I attempted to achieve this using the code below, but it seems to not work as expected. Can someone provide guidance?

var inside_element = $(figure);
var indx = 10;
inside_element.each(function(indx){
    $(this).css({
        width: '90%' - indx,
        height: '90%' - indx
    });
});

Thank you!

Answer №1

If you are working with the string '90%' and attempting to perform a mathematical operation that will decrease, you can use the following code:

var inside_element = $('figure');
inside_element.each(function(indx){
    $(this).css({
        width: (90 - (10*indx)) + '%' ,
        height: (90 - (10*indx)) + '%'
    });
});

It is worth noting that the declaration var indx = 10; is not necessary as it will be overridden within the function.

UPDATE: If there are multiple containers, the code should be adjusted accordingly:

var inside_container = $('.inside_container');
inside_container.each( function(i) {
    var inside_element = $(this).find('figure');
    var step = 10;
    inside_element.each(function(indx){
        $(this).css({
            width: (90 - (step*indx)) + '%' ,
            height: (90 - (step*indx)) + '%'
        });
    });
});

Answer №2

Calculations for adjusting width and height are done by subtracting the value of indx from a string.

To achieve this, follow the code snippet below:

var width = 100;
var height = 100;
inside_element.each(function(indx){
    width = width - 10;
    height = height - 10;
    $(this).css({
        width: width + '%',
        height: height + '%'
    });
});

Answer №3

If you want to target the figure element, make sure to use $('figure'); instead of just 'figure'.

var figureElements = $('figure');
figureElements.each(function(index){
    $(this).css({
        width: (90 - index * 10) +"%",
        height:(90 - index * 10) +"%"
    });
});

Answer №4

Give this a shot:

let selected_element = $('.image');
let width = '90',
    height = '90';
    
selected_element.each(function(index){
    $(this).css({
        width: width + '%',
        height: height + '%'
    });
    width = width - 10;
    height = height - 10;
});

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

Tips for inserting SVG images into PDF documents

Is there a way to generate a PDF upon clicking the "generate PDF" button? The PDF should display an image containing highlighted squares corresponding to the user's selections on the webpage. I've been struggling to include the SVG in the PDF as ...

Bring a div box to life using AngularJS

Struggling to animate a div-Box in AngularJS? Despite trying multiple examples, the animation just won't cooperate. I'm aiming to implement a feature where clicking on a button triggers a transition animation to display the search form. I under ...

Changing the texture on a material in three.js

I have successfully set up a texture on a mesh using three.js, and it initially loads exactly as I want it to: texture = THREE.ImageUtils.loadTexture("textures/hash.png"); texture.needsUpdate = true; uniforms = { colo ...

The RefreshIndicator from Material-UI fails to display during the first page load

I am facing an issue with displaying a loading icon during the initial page load. The code appears to be correct to me but for some reason, the loading icon is not showing up. I am utilizing the RefreshIndicator component from material-ui for the loading i ...

Urgent dependency alert: calling for a necessity (sequelize) in next.js is a vital element

I'm encountering a challenge with integrating sequelize into my Next.js 13 project to connect my API routes with the database. I keep receiving errors that say "Critical dependency: the request of a dependency is an expression." import * as pg from &a ...

Go through each item in the array and change its properties

When retrieving data from the database, I receive the following information: "Data": [{ "mainData": [{ "_id": ObjectId("5ab63b22d012ea2bc0bb7e9b"), "date": "2018-03-24" }], "files": [ { "_id": ObjectId("5ab63b22d012ea2bc0bb7e9d"), ...

Steps to clear the form in vue after the ajax request has been successfully submitted

Exploring Vue.js Web Form Validation with a Scenario If you are interested in the vue-form validator library, it can be found at https://github.com/fergaldoyle/vue-form For a demonstration of this scenario, check out the following jsfiddle link: https:/ ...

Tips for handling notifications that have been read and unread in a React application

In my current project using React JS, I am tasked with creating a notifications component that will display account-related activities. The notifications need to be sorted into read and unread categories. My main question is how should I manage the read a ...

What could be the reason for the date locale 'en-CA' displaying the format M/d/yyyy instead of yyyy-MM-dd?

My JavaScript code is set up to show the current date in en-CA format. date = new Date(); console.log(date.toLocaleDateString('en-CA')); In the past, dates in the en-CA locale were displayed as yyyy-MM-dd (2023-02-24). However, suddenly today ...

Does Webkit reserve the class name ".ad-space" for a specific purpose?

Can you believe this? I'm working with 960.gs and for some reason my ads class labeled ".ad-space" is vanishing in Webkit browsers. It functions properly in Firefox, but not in Chrome or Safari. Could it be a bug in a Webkit addon, like Adblock Plus? ...

Sending form data field in a request using the POST method

I am faced with the task of making a POST call to a URL with parameters a="x", b="y", c="z", and d, which is a form data field for file upload. When initiating an AJAX call, the code I use is as follows: $.ajax({ contentType: false, pro ...

How can I reposition an image diagonally to a specific location using JavaScript in p5.js? Is there a method to display an image and then conceal it at a chosen location in p5.js?

Is there a way to move the third image diagonally until it intersects with the two images? var pic1; var pic2; var pic3; let posX=0 let posY=0 const rightwall=350; function preload(){ pic1=loadImage("5.png") pic2=loadImage("iron.jpg&qu ...

Create a reusable React widget in a JavaScript file and integrate it into an ASP.NET MVC project

Is there a way to create a widget with the React library and compile it into a single JavaScript file for reuse in any non-React application (particularly in ASP .NET)? ...

Selenium IDE seems to be having trouble capturing mousedown events during recording

I recently created a basic div element that changes its background color on mousedown and mouseup events. I decided to test it using Selenium IDE in Firefox, but encountered an issue where none of the mouse actions were being recorded. If you'd like t ...

Adjust the size and color of text in Chart.js using Angular 5

Why does the font color in chartjs appear as light gray and not print when you want to do so from the page? I tried changing the font color of chartjs in the options attribute, but it didn't work. How can I change the font color in chartjs angular? ...

Using ASP.NET MVC to transmit JSON information to a Controller method

Even after multiple attempts, I am unable to send JSON data to my ASP.NET MVC3 controller action method successfully. Below is the ajax call I am using (it utilizes the JSON.stringify method from json2.js): $.ajax({ url: '/Home/GetData', ...

Achieve uniform height for two elements using Material UI

Here is the code snippet I have: <div className="center"> <TextField variant="outlined" className="manualUserFollowTxt" required id="manualUserFollowTxt" label="Username" name="username" autoComplete="username" a ...

Difficulty in using a mouse click to transmit user data to the server

I'm struggling with creating an event that activates when the submit button is clicked. My objective is to execute the function Chat.send() using the input provided by the user in the text field at the top of the page when they press the "Send" button ...

Dynamic Bootstrap User Authentication Form

Currently, I am in the process of constructing a login form for my web application and have opted to use Bootstrap for its styling. To ensure that the screen is responsive, I've integrated the following "Meta Tag" into the structure: <meta name="v ...

Is it time to set up your own MySQL Database?

let connection = mysql.createConnection({ user: 'root', password: '1234', database: 'data101', port: 3306 }); While using the MySQL package for NodeJS to create a database, I have a question. Do I need to manually cr ...