Jquery实现抖动效果


参考文档:jQuery 的扩展,实现抖动效果

背景:在项目中,登录页验证用户名和密码是否匹配,如果不匹配,则抖动输入框,提示用户输入错误。

将如下代码写到JS文件中:

 1 jQuery.fn.shake = function (intShakes /*Amount of shakes*/, intDistance /*Shake distance*/, intDuration /*Time duration*/) {
 2     this.each(function () {
 3         var jqNode = $(this);
 4         jqNode.css({ position: 'relative' });
 5         for (var x = 1; x <= intShakes; x++) {
 6             jqNode.animate({ left: (intDistance * -1) }, (((intDuration / intShakes) / 4)))
 7             .animate({ left: intDistance }, ((intDuration / intShakes) / 2))
 8             .animate({ left: 0 }, (((intDuration / intShakes) / 4)));
 9         }
10     });
11     return this;
12 }

在View中引入以上JS文件以及Jquery文件,代码如下:

1 <script src="~/Scripts/jquery-1.8.2.js"></script>
2 <script src="~/Scripts/shakeYou.js"></script>
3 <script>
4     $(function () {
5         $('#btn1').click(function () {
6             $(this).shake(2, 10, 400);
7         });
8     });
9 </script>

以上代码的效果是:当点击btn时,自身抖动。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM