参考文档: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时,自身抖动。
