Home Web Front-end HTML Tutorial How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking?

How to use CSS3 and JavaScript to achieve the effect of scattering and enlarging the surrounding pictures after clicking?

Apr 05, 2025 am 06:15 AM
css click event cos absolute positioning

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;
}
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Apr 18, 2025 am 09:24 AM

When developing websites using CraftCMS, you often encounter resource file caching problems, especially when you frequently update CSS and JavaScript files, old versions of files may still be cached by the browser, causing users to not see the latest changes in time. This problem not only affects the user experience, but also increases the difficulty of development and debugging. Recently, I encountered similar troubles in my project, and after some exploration, I found the plugin wiejeben/craft-laravel-mix, which perfectly solved my caching problem.

vscode cannot install extension vscode cannot install extension Apr 15, 2025 pm 07:18 PM

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

Can vscode be used for mac Can vscode be used for mac Apr 15, 2025 pm 07:36 PM

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

How to generate html by sublime How to generate html by sublime Apr 16, 2025 am 09:03 AM

There are two ways to generate HTML code in Sublime Text: Using the Emmet plugin, you can generate HTML elements by entering an abbreviation and pressing the Tab key, or use a predefined HTML file template that provides basic HTML structure and other features such as code snippets, autocomplete functionality, and Emmet Snippets.

How to simplify CMS development with Composer: Practical application of the Lebenlabs/SimpleCMS library How to simplify CMS development with Composer: Practical application of the Lebenlabs/SimpleCMS library Apr 18, 2025 am 09:45 AM

When developing a new content management system (CMS), I encountered a common but difficult problem: how to quickly build a fully functional CMS without adding too much complexity. There are many ready-made CMS solutions available on the market, but they are often too large and complex to configure and can be a burden for small projects. After some exploration, I discovered the lebenlabs/simplecms library, which provides a simple and efficient solution through Composer.

laravel installation code laravel installation code Apr 18, 2025 pm 12:30 PM

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

git software installation git software installation Apr 17, 2025 am 11:57 AM

Installing Git software includes the following steps: Download the installation package and run the installation package to verify the installation configuration Git installation Git Bash (Windows only)

See all articles