Home > Article > Web Front-end > Simple sample code sharing for JS to achieve dithering effect
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('#p1'); document.onclick = function () { /* * 抖动: * 1. 每次改变一下元素的位置 * 按照一个中心点进行偏移,假设中心点left原始是400,那么每次就以left:400为中心做位置的移动 * 380 -> 420 * */ // p1.style.left = '380px'; // p1.style.left = '420px'; var a = true; setInterval(function() { /* * 根据a的值,做不同的设置 * */ p1.style.left = (a ? 380 : 420) + 'px'; 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!