用animate实现jQuery的摇动效果
我得到了 jQuery lib 的一个精简子集,我缺少的关键功能之一是 .effect
函数。不过我有.animate
。我想知道是否有人对我如何复制动画功能有任何想法。
我特别注意只用几行代码,因为我需要缩小代码大小。这就是为什么 jquery 库很小并且没有效果函数的原因。
TLDR - 我正在尝试
$("#"+id_string).effect( "shake", {}, "fast" );
用 jQuery 中使用 .animate
的内容来替换。
I've been given a cut down subset of the jQuery lib one of the key features I'm missing is the .effect
functions. I do however have .animate
. I was wondering if anyone would have any ideas how I could go about reproducing the animation functions.
I am particularly consious of making this only a few lines as I need to keep the code size down. Which is why the jquery lib is as small as it is and doesnt have the effects functions.
TLDR - I'm trying to replace
$("#"+id_string).effect( "shake", {}, "fast" );
With something using .animate
within jQuery.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
到目前为止我有这样的事情..
So far I have something like this ..
我非常喜欢@phpslightly 解决方案,我一直在使用它。所以这里它被更新为基本的 jquery 插件形式,它将返回你的元素
然后你可以像常规插件一样使用它:
或者使用默认值(100,10,3):
I like @phpslightly solution so much, I keep using it. So here it is updated to basic jquery plugin form which will return your element
You would then use it like a regular plugin:
Or use the default values (100, 10, 3):
它实际上已经在幕后以这种方式实现了,您可以在
jquery.effects.shake.js
,如果您只想复制该功能,则可以。另一种考虑方法:如果您使用多种效果,我建议仅下载您想要的效果的 jQuery UI< /a>.对于这种效果,无需自己复制功能,您只需要
jquery.effects.core.js
和jquery.effects.shake.js
。It's actually already implemented this way under the covers, you can see exactly how in
jquery.effects.shake.js
, if you wanted to copy only that functionality you can.Another approach to think about: if you're using multiple effects, I'd recommend downloading jQuery UI with only the effects you want. For this effect, without copying the functionality yourself, you would just need
jquery.effects.core.js
andjquery.effects.shake.js
.现在这可能无关紧要,但我已将 jQ UI 的抖动效果移植为独立的 jQuery 插件。您所需要的只是 jQuery,它的工作方式与 jQ UI 中提供的完全一样。
对于那些想要使用该效果而不用不必要的 jQ UI 核心文件来实际膨胀其项目的人。
$('#element').shake({...});
可以在此处找到说明:https://github.com/ninty9notout/jquery-shake
我想我会把它留在这里以供将来参考。
This is probably irrelevant now but I've ported jQ UI's shake effect as a standalone jQuery plugin. All you need is jQuery and it will work exactly like the one provided in jQ UI.
For those who want to use the effect without actually bloating their project with unnecessary jQ UI core files.
$('#element').shake({...});
It can be found here with instruction: https://github.com/ninty9notout/jquery-shake
Thought I'd leave this here for future reference.
这是一种更干净、更流畅的动画制作方式。
This is a more clean and smooth way to do the animation.
我不明白仅使用
animate
再现抖动效果会带来如此复杂的情况。这是我的解决方案,只需几行。编辑:更新代码以将元素返回到原始位置。仍然相信这是解决问题的最轻、最好的解决方案。
I don't understand all the complexity being thrown into reproducing the shake effect with solely
animate
. Here's my solution in just a couple lines.EDIT: Updated code to return element to original position. Still believe this is the lightest and best solution to the problem.
我前段时间写了一些简单的 jquery 动画:
https://github.com/yckart/jquery-自定义动画
I wrote some time ago a few simple jquery animations:
https://github.com/yckart/jquery-custom-animations
这并不完美,但很实用
This is not perfect, but functional
基于@el生产者解决方案,我添加了一些乘法逻辑,并使其看起来像随机摇动。
Based on @el producer solution, I added some multiply logic and make it look like a random shake.
立场必须绝对站在我这边,所以我将其更改为:
Position had to be absolute on my side, so I changed it to: