jQuery模拟窗口抖动效果
效果图:
代码如下:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>jQuery模拟窗口抖动</title> <style type="text/css"> input{margin-top: 20px;} .center{margin-left: 50%;transform: translate(-50%);} .img{display:block;position:absolute;top:100px;} </style> </head> <body> <img src="http://mpic.tiankong.com/60b/994/60b994dc105a7b76b25e116733a142e9/431-0248.jpg" class="img center" /> <input type="button" class="center" value="模拟窗口抖动" /> <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.0.js" type="text/javascript"></script> <script type="text/javascript"> $(":button").click(function () { var len = 4, //晃动的距离,单位像素 c = 16, //晃动次数,4次一圈 step = 0, //计数器 img = $("img"), off = img.offset(); this.step = 0; timer = setInterval(function () { var set = pos(); img.offset({ top: off.top + set.y * len, left: off.left + set.x * len }); if (step++ >= c) { img.offset({ top: off.top, left: off.left }); //抖动结束回归原位 clearInterval(timer); } // console.log(step) }, 45); }); function pos() { this.step = this.step ? this.step : 0; this.step = this.step == 4 ? 0 : this.step; var set = { 0: { x: 0, y: -1 }, 1: { x: -1, y: 0 }, 2: { x: 0, y: 1 }, 3: { x: 1, y: 0 } } return set[this.step++]; } </script> </body> </html>
相关推荐
EdwardSiCong 2020-11-23
85477104 2020-11-17
hhanbj 2020-11-17
81427005 2020-11-11
seoppt 2020-09-13
honeyth 2020-09-13
WRITEFORSHARE 2020-09-13
84483065 2020-09-11
momode 2020-09-11
85477104 2020-08-15
83510998 2020-08-08
82550495 2020-08-03
tthappyer 2020-08-03
84901334 2020-07-28
tthappyer 2020-07-25
TONIYH 2020-07-22
tztzyzyz 2020-07-20
83510998 2020-07-18
81463166 2020-07-17