How to achieve image jitter effect
Requirement: When you click on the picture, the picture will shake left and right, and it can also shake up and down.
The specific code is as follows:
(Recommended tutorial: CSS tutorial)
html:
<img src="./img/19.jpg" alt="" id="img1">
css:
img { position: absolute; top: 200px; left: 300px; }
js:
<script src="doMove.js"></script> <script> window.onload = function(){ var img1 = document.getElementById('img1'); img1.onclick = function(){ // 先获得img1的原来left值,在此基础上进行抖动;parseInt()是把300px的单位去掉只留数字 var pos = parseInt(getStyle(img1,'left')); var arr = [];//20,-20,18,-18,16,-16,...存放抖动的频率 var timer = null;//开启定时器 var num = 0;//一般只要有数组,那么必定有数字跟着变化;数组的第0个,第1个... for(var i =20;i>0;i-=2){//左右抖动的幅度第一次为20px,然后减2 arr.push(i,-i); } arr.push(0); // alert(arr);//测试一下 // 有抖动的频率之后,要开启定时器让它进行抖动 clearInterval(timer);//开启定时器之前要先把定时器关闭,防止连续多次点击 timer = setInterval(function(){ img1.style.left = pos + arr[num] + 'px';//原来的位置pos,加上数组中每一个数字 num++; if(num === arr.length){//如果数字等于数组的长度(就是全部抖完了),关闭定时器 clearInterval(timer); } },50); } } </script>
Instructions:
1. We must first introduce our original encapsulated animation function doMove.js, and the getStyle() function is the function in this function;
2. parseInt() removes the 300px unit and leaves only the number 300;
3. To jitter up and down, just change left to top.
The above is the detailed content of How to achieve image jitter effect. 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

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

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.

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.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

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

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.

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

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-
