


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 AMCreate 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

Undress AI Tool
Undress images for free

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

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

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

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

CSS blocks page rendering because browsers view inline and external CSS as key resources by default, especially with imported stylesheets, header large amounts of inline CSS, and unoptimized media query styles. 1. Extract critical CSS and embed it into HTML; 2. Delay loading non-critical CSS through JavaScript; 3. Use media attributes to optimize loading such as print styles; 4. Compress and merge CSS to reduce requests. It is recommended to use tools to extract key CSS, combine rel="preload" asynchronous loading, and use media delayed loading reasonably to avoid excessive splitting and complex script control.

ToimplementdarkmodeinCSSeffectively,useCSSvariablesforthemecolors,detectsystempreferenceswithprefers-color-scheme,addamanualtogglebutton,andhandleimagesandbackgroundsthoughtfully.1.DefineCSSvariablesforlightanddarkthemestomanagecolorsefficiently.2.Us

The topic differencebetweenem, Rem, PX, andViewportunits (VH, VW) LiesintheirreFerencepoint: PXISFixedandbasedonpixelvalues, emissrelative EtothefontsizeFheelementoritsparent, Remisrelelatotherootfontsize, AndVH/VwarebaseDontheviewporttimensions.1.PXoffersprecis

ThebestapproachforCSSdependsontheproject'sspecificneeds.Forlargerprojects,externalCSSisbetterduetomaintainabilityandreusability;forsmallerprojectsorsingle-pageapplications,internalCSSmightbemoresuitable.It'scrucialtobalanceprojectsize,performanceneed

Choosing the correct display value in CSS is crucial because it controls the behavior of elements in the layout. 1.inline: Make elements flow like text, without occupying a single line, and cannot directly set width and height, suitable for elements in text, such as; 2.block: Make elements exclusively occupy one line and occupy all width, can set width and height and inner and outer margins, suitable for structured elements, such as; 3.inline-block: has both block characteristics and inline layout, can set size but still display in the same line, suitable for horizontal layouts that require consistent spacing; 4.flex: Modern layout mode, suitable for containers, easy to achieve alignment and distribution through justify-content, align-items and other attributes, yes

Cryptocurrencies that are expected to become "100 times coin" in 2026 include Bitcoin, Ethereum, Solana, Chainlink, Polkadot, Ripple and emerging track projects such as AI blockchain, MEME coins, and GameFi. 1. Bitcoin (BTC) may reach US$150,000-200,000 due to the halving effect, institutional increase and regulatory clarification; 2. Ethereum (ETH) benefited from ecological upgrades, pledge returns and possible ETF approvals, with a target price of US$20,000; 3. Solana (SOL) may hit US$1,000 with high performance and ecological expansion; 4. Chainlink (LINK) is the leader in oracle, driven by the integration of AI and RWA, and the price may be sudden.

AnimatingSVGwithCSSispossibleusingkeyframesforbasicanimationsandtransitionsforinteractiveeffects.1.Use@keyframestodefineanimationstagesforpropertieslikescale,opacity,andcolor.2.ApplytheanimationtoSVGelementssuchas,,orviaCSSclasses.3.Forhoverorstate-b

Autoprefixer is a tool that automatically adds vendor prefixes to CSS attributes based on the target browser scope. 1. It solves the problem of manually maintaining prefixes with errors; 2. Work through the PostCSS plug-in form, parse CSS, analyze attributes that need to be prefixed, and generate code according to configuration; 3. The usage steps include installing plug-ins, setting browserslist, and enabling them in the build process; 4. Notes include not manually adding prefixes, keeping configuration updates, prefixes not all attributes, and it is recommended to use them with the preprocessor.
