Home Web Front-end JS Tutorial Detailed example code explanation of javascript implementation of window jitter and qq window jitter_javascript skills

Detailed example code explanation of javascript implementation of window jitter and qq window jitter_javascript skills

May 16, 2016 pm 03:22 PM

The window jitter effect is used in many places. For example, NetEase's login window has this effect. When the login fails, a jitter effect will appear. This is not only dynamic, but also makes people feel novel. Here is a paragraph like this I would like to share the code example with you.

The code is as follows:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8"/>
<meta name="author" content="http://www.softwhy.com/" />
<title>窗口登陆效果</title>
<style type="text/css">
#win
{
 position:relative;
 width:100px;
 height:100px;
 background-color:#666;
}
</style>
<script type="text/javascript"> 
var a=['top','left'];
var b=0; 
var u;
function fudu()
{ 
 win.style[a[b%2]]=(b++)%4<2&#63;"0px":"4px"; 
 if(b>15)
 {
 clearInterval(u);
 b=0
 } 
}
function zd()
{
 clearInterval(u); 
 u=setInterval(fudu,32) 
} 
window.onload=function()
{
 var bt=document.getElementById("bt");
 var win=document.getElementById("win");
 bt.onclick=zd;
}
</script> 
</head> 
<body > 
<button id="bt">点击振动</button> 
<div id="win"></div> 
</body> 
</html> 
Copy after login

In the above code, when the button is clicked, the div will have a jitter effect. Of course, this effect is relatively simple. This is just for demonstration. It can be expanded in actual applications. Here is a brief introduction to the implementation process.

1. Implementation principle:

The code is simple and the principle is also very simple. The div adopts relative positioning. When the button is clicked, the fudu() function will be continuously called through the timer function setInterval(). This function can continuously set the attribute value of left or top through the modulo method, that is, continuously Randomly adjust the position of the div to achieve a jittering effect. When the value of b is greater than 15, the jittering will stop.

2. Code comments:

1.var a=['top','left'], declares an array that stores top and left strings.
2.var b=0, declare a variable b and assign it a value of 0.
3.var u, declare a variable as the return value of the timer function setInterval().
4.function fudu(){}, declare a function.
5.win.style[a[b%2]]=(b++)%4<2?"0px":"4px", this code is the core part, the value of b%2 modulo operation is 0 or 1, This becomes the index value of array a and is used to obtain the value in the array. The effect of style[a[b%] and style.top is the same. ]]=(b++)%4<2?"0px":"4px", in this way, the top and left attributes of the div can be assigned values ​​by taking the modulo to determine whether the value is less than 2.
6.if(b>15) {clearInterval(u); b=0}, if the value of b is greater than 15, then stop dithering and reset the value of b to 0.
7.function zd(){}, declare a function.
8. clearInterval(u), stops the running of the timer function. This code is to prevent the problem of jitter that may not stop when the button is continuously clicked, because the two jitters affect each other.
9.u=setInterval(fudu,30), use the timer function to continuously call the fudu function.
10.window.onload=function(){}, execute the code in the function when the document content is completely loaded.
11.var bt=document.getElementById("bt"), get the button object.
12.var win=document.getElementById("win"), get the div object.
13.bt.onclick=zd, register the event handler function for the button.

The following is a JavaScript code that imitates QQ window shaking

A very unique shaking effect, imitating the shaking effect of QQ chat window. It is implemented with JavaScript code. With this fake chat window, I didn’t expect that it is really similar to the shaking effect of QQ. It’s quite funny.

<title>仿QQ窗口抖动</title>
<img id="win" style='position:relative' src="/jscss/demoimg/qqwinows.jpg">
<br /><br />
<button onclick="zd()">让我抖一下!</button>
<script >
function zd(u){ 
 var a=['top','left'],b=0;
 u=setInterval(function(){
 document.getElementById('win').style[a[b%2]]=(b++)%4<2&#63;0:4;
 if(b>15){clearInterval(u);b=0}
 },32)
}
</script>
Copy after login

Through the above example code, I will introduce to you the implementation of window jitter and QQ window jitter using JavaScript. I hope this code can help you.

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)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

How to implement panel drag and drop adjustment function similar to VSCode in front-end development? How to implement panel drag and drop adjustment function similar to VSCode in front-end development? Apr 04, 2025 pm 02:06 PM

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...

See all articles