Home > Web Front-end > JS Tutorial > Simple sample code sharing for JS to achieve dithering effect

Simple sample code sharing for JS to achieve dithering effect

黄舟
Release: 2017-06-01 09:20:40
Original
1435 people have browsed it

This article ends for everyone to achieve the dithering effect through js, which is very good and has reference value. Friends who are interested can refer to it

No more nonsense, just go directly I’ve posted the code for everyone. The specific code is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#p1 {
width: 100px;
height: 100px;
position: absolute;
left: 400px;
top: 200px;
background: red;
}
</style>
</head>
<body>
<p id="p1"></p>
<script>
var p1 = document.querySelector(&#39;#p1&#39;);
document.onclick = function () {
/*
* 抖动:
* 1. 每次改变一下元素的位置
* 按照一个中心点进行偏移,假设中心点left原始是400,那么每次就以left:400为中心做位置的移动
* 380 -> 420
* */
// p1.style.left = &#39;380px&#39;;
// p1.style.left = &#39;420px&#39;;
var a = true;
setInterval(function() {
/*
* 根据a的值,做不同的设置
* */
p1.style.left = (a ? 380 : 420) + &#39;px&#39;;
a = !a;
}, 30);
}
</script>
</body>
</html>
Copy after login

The above is the detailed content of Simple sample code sharing for JS to achieve dithering effect. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template