


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/imghwm/default1.png" data-src="https://img.php.cn/upload/article/001/246/273/174312337946553.jpg?x-oss-process=image/resize,p_40" 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/imghwm/default1.png" data-src="https://img.php.cn/upload/article/001/246/273/174312337917621.jpg?x-oss-process=image/resize,p_40" 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/imghwm/default1.png" data-src="https://img.php.cn/upload/article/001/246/273/174312338030325.jpg?x-oss-process=image/resize,p_40" 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!

A consistent HTML encoding style is important because it improves the readability, maintainability and efficiency of the code. 1) Use lowercase tags and attributes, 2) Keep consistent indentation, 3) Select and stick to single or double quotes, 4) Avoid mixing different styles in projects, 5) Use automation tools such as Prettier or ESLint to ensure consistency in styles.

Solution to implement multi-project carousel in Bootstrap4 Implementing multi-project carousel in Bootstrap4 is not an easy task. Although Bootstrap...

How to achieve the effect of mouse scrolling event penetration? When we browse the web, we often encounter some special interaction designs. For example, on deepseek official website, �...

The default playback control style of HTML video cannot be modified directly through CSS. 1. Create custom controls using JavaScript. 2. Beautify these controls through CSS. 3. Consider compatibility, user experience and performance, using libraries such as Video.js or Plyr can simplify the process.

Potential problems with using native select on mobile phones When developing mobile applications, we often encounter the need for selecting boxes. Normally, developers...

What are the disadvantages of using native select on your phone? When developing applications on mobile devices, it is very important to choose the right UI components. Many developers...

Use Three.js and Octree to optimize collision handling of third-person roaming in the room. Use Octree in Three.js to implement third-person roaming in the room and add collisions...

Issues with native select on mobile phones When developing applications on mobile devices, we often encounter scenarios where users need to make choices. Although native sel...


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

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

SublimeText3 English version
Recommended: Win version, supports code prompts!
