Introduction to the principle
Jitter is actually a special form of reciprocating motion, except that reciprocating motion is an infinite motion without friction, and is based on speed; while jitter uses position as a reference, and finally stops at the starting point.
The most common jitter effect in web pages should be the window jitter prompt
The jitter element starts from the starting point, first moves to the right by the maximum distance len, then moves to the symmetrical left position; and then moves to the right Move a slightly smaller distance, and then move to the symmetrical left position; in this loop, the final element stops at the starting point
Code implementation
Jittering in code implementation is nothing more than using a timer to let left or The top value changes
In the implementation of motion, there are two ideas for distance change
Idea 1
div.style.left = div.offsetLeft + value;
Get the current style of the element each time, and then perform calculations with the changed value
Idea 2
left = div.offsetLeft; ...... div.style.left = left + value;
Before the timer starts, get the initial style of the element, and then calculate it with the changed value. From the perspective of jitter implementation, it is simpler to use the second method and completely leave the distance change to the value change. So, The key to code implementation is to understand how the value changes. Assume that the farthest distance is the target, and the distance interval in the same direction is the step. If expressed more intuitively with numbers, the value of value is similar to 4, -4, 2, -2, 0. Therefore, a variable dir is needed to control the starting shaking direction. The dir must be changed every time the timer moves
//定时器开启前的变量声明 dir = 1; step = 0; left = div.offsetLeft //定时器里面的代码 value = dir*(target - step); if(step >= target){ step = target } div.style.left = left + value + 'px'; if(dir === -1){ step++; } dir = -dir;
Encapsulate the shaking effect as shakeMove.js
function getCSS(obj,style){ if(window.getComputedStyle){ return getComputedStyle(obj)[style]; } return obj.currentStyle[style]; } function shakeMove(json){ //声明要进行抖动的元素 var obj = json.obj; //声明元素抖动的最远距离 var target = json.target; //默认值为20 target = Number(target) || 20; //声明元素的变化样式 var attr = json.attr; //默认为'left' attr = attr || 'left'; //声明元素的起始抖动方向 var dir = json.dir; //默认为'1',表示开始先向右抖动 dir = Number(dir) || '1'; //声明元素每次抖动的变化幅度 var stepValue = json.stepValue; stepValue = Number(stepValue) || 2; //声明回调函数 var fn = json.fn; //声明步长step var step = 0; //保存样式初始值 var attrValue = parseFloat(getCSS(obj,attr)); //声明参照值value var value; //清除定时器 if(obj.timer){return;} //开启定时器 obj.timer = setInterval(function(){ //抖动核心代码 value = dir*(target - step); //当步长值大于等于最大距离值target时 if(step >= target){ step = target } //更新样式值 obj.style[attr] = attrValue + value + 'px'; //当元素到达起始点时,停止定时器 if(step == target){ clearInterval(obj.timer); obj.timer = 0; //设置回调函数 fn && fn.call(obj); } //如果此时为反向运动,则步长值变化 if(dir === -1){ step = step + stepValue; } //改变方向 dir = -dir; },50); }
Instance application
Let’s use the encapsulated shakeMove to implement some Simple jitter app
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> <style> .test{ height: 50px; width: 50px; position: absolute; top: 50px; } </style> </head> <body> <div id="box"> <div style="left:10px;background:lightblue"></div> <div style="left:70px;background:lightgreen"></div> <div style="left:130px;background:pink"></div> <div style="left:190px;background:lightcoral"></div> <div style="left:250px;background:orange"></div> </div> <script src="http://files.cnblogs.com/files/xiaohuochai/shakeMove.js"></script> <script> var aDiv = box.getElementsByTagName('div'); for(var i = 0; i < aDiv.length; i++){ aDiv[i].onmouseover = function(){ shakeMove({obj:this,attr:'top'}); } } </script> </body> </html>

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.


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

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

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

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
