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

Simple sample code sharing for JS to achieve dithering effect

黄舟
黄舟Original
2017-06-01 09:20:401409browse

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>

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!

Statement:
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