


How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking?
Create cool web interaction: click on the picture, and the surrounding pictures spread and enlarge!
Many websites use this compelling interaction: click on an image and it will enlarge, while the surrounding images will spread out. This article will explain in detail how to achieve this effect using CSS3 and JavaScript.
First, we need to add a click event listener to each image. When a user clicks on an image, the JavaScript code executes, resizing the position and size of all images. The key is to use the transform
attribute of CSS3 to achieve the displacement and scaling of the picture.
The steps are as follows:
1. HTML structure: for each <img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/001/246/273/174312337946553.jpg" class="lazy" alt="How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? "> <img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/001/246/273/174312337917621.jpg" class="lazy" alt="How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? "> <img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/001/246/273/174312338030325.jpg" class="lazy" alt="How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking? ">
2. JavaScript code: The spread()
function receives the clicked image element as a parameter and calculates the displacement and scaling ratio of other images:
function spread(img) { const images = document.getElementsByTagName('img'); const index = Array.prototype.indexOf.call(images, img); for (let i = 0; i <p> In this code, <code>Math.sin</code> and <code>Math.cos</code> functions calculate the image displacement, <code>100</code> controls the scattering radius, and <code>scale</code> controls the scaling ratio. The clicked image has a scale of 2, and the other images remain as they are.</p><p> <strong>3. CSS style:</strong> Set the initial position, size and transition effect of the picture:</p><pre class="brush:php;toolbar:false"> img { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%) scale(1); transition: transform 0.5s ease-in-out; /* Add other styles, such as image size*/ width: 100px; height: 100px; }
position: absolute
enables the image to be absolutely positioned, translate(-50%, -50%)
centers the image, and the transition
attribute allows the image transformation process to smoothly transition.
Through the above steps, you can achieve the effect of scattering the surrounding pictures and enlarging them by themselves after clicking on them. You can fine-tune the effects by adjusting parameters in your code (such as spreading radius, scaling, transition time) and add more animation or interaction details to enhance the user experience. Remember to replace "image1.jpg"
, "image2.jpg"
and "image3.jpg"
as your actual image path.
The above is the detailed content of How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text
