用animate实现jQuery的摇动效果

发布于 2024-10-07 04:04:31 字数 327 浏览 3 评论 0原文

我得到了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

悲念泪 2024-10-14 04:04:31

到目前为止我有这样的事情..

jQuery.fn.shake = function(intShakes, intDistance, intDuration) {
    this.each(function() {
        $(this).css("position","relative"); 
        for (var x=1; x<=intShakes; x++) {
        $(this).animate({left:(intDistance*-1)}, (((intDuration/intShakes)/4)))
    .animate({left:intDistance}, ((intDuration/intShakes)/2))
    .animate({left:0}, (((intDuration/intShakes)/4)));
    }
  });
return this;
};

So far I have something like this ..

jQuery.fn.shake = function(intShakes, intDistance, intDuration) {
    this.each(function() {
        $(this).css("position","relative"); 
        for (var x=1; x<=intShakes; x++) {
        $(this).animate({left:(intDistance*-1)}, (((intDuration/intShakes)/4)))
    .animate({left:intDistance}, ((intDuration/intShakes)/2))
    .animate({left:0}, (((intDuration/intShakes)/4)));
    }
  });
return this;
};
一曲琵琶半遮面シ 2024-10-14 04:04:31

我非常喜欢@phpslightly 解决方案,我一直在使用它。所以这里它被更新为基本的 jquery 插件形式,它将返回你的元素

jQuery.fn.shake = function(interval,distance,times){
   interval = typeof interval == "undefined" ? 100 : interval;
   distance = typeof distance == "undefined" ? 10 : distance;
   times = typeof times == "undefined" ? 3 : times;
   var jTarget = $(this);
   jTarget.css('position','relative');
   for(var iter=0;iter<(times+1);iter++){
      jTarget.animate({ left: ((iter%2==0 ? distance : distance*-1))}, interval);
   }
   return jTarget.animate({ left: 0},interval);
}

然后你可以像常规插件一样使用它:

$("#your-element").shake(100,10,3);

或者使用默认值(100,10,3):

$("#your-element").shake();

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

jQuery.fn.shake = function(interval,distance,times){
   interval = typeof interval == "undefined" ? 100 : interval;
   distance = typeof distance == "undefined" ? 10 : distance;
   times = typeof times == "undefined" ? 3 : times;
   var jTarget = $(this);
   jTarget.css('position','relative');
   for(var iter=0;iter<(times+1);iter++){
      jTarget.animate({ left: ((iter%2==0 ? distance : distance*-1))}, interval);
   }
   return jTarget.animate({ left: 0},interval);
}

You would then use it like a regular plugin:

$("#your-element").shake(100,10,3);

Or use the default values (100, 10, 3):

$("#your-element").shake();
━╋う一瞬間旳綻放 2024-10-14 04:04:31

它实际上已经在幕后以这种方式实现了,您可以在 jquery.effects.shake.js,如果您只想复制该功能,则可以。

另一种考虑方法:如果您使用多种效果,我建议仅下载您想要的效果的 jQuery UI< /a>.对于这种效果,无需自己复制功能,您只需要 jquery.effects.core.jsjquery.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 and jquery.effects.shake.js.

只涨不跌 2024-10-14 04:04:31

现在这可能无关紧要,但我已将 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.

无风消散 2024-10-14 04:04:31

这是一种更干净、更流畅的动画制作方式。

jQuery.fn.shake = function(shakes, distance, duration) {
    if(shakes > 0) {
        this.each(function() {
            var $el = $(this);
            var left = $el.css('left');
            $el.animate({left: "-=" + distance}, duration, function(){
                $el.animate({left: "+=" + distance * 2}, duration, function() {
                    $el.animate({left: left}, duration, function() {
                        $el.shake(shakes-1, distance, duration); });});
            });
        });
    }
    return this;
};

This is a more clean and smooth way to do the animation.

jQuery.fn.shake = function(shakes, distance, duration) {
    if(shakes > 0) {
        this.each(function() {
            var $el = $(this);
            var left = $el.css('left');
            $el.animate({left: "-=" + distance}, duration, function(){
                $el.animate({left: "+=" + distance * 2}, duration, function() {
                    $el.animate({left: left}, duration, function() {
                        $el.shake(shakes-1, distance, duration); });});
            });
        });
    }
    return this;
};
执笔绘流年 2024-10-14 04:04:31

我不明白仅使用 animate 再现抖动效果会带来如此复杂的情况。这是我的解决方案,只需几行。

function shake(div,interval=100,distance=10,times=4){
    $(div).css('position','relative');
    for(var iter=0;iter<(times+1);iter++){
        $(div).animate({ left: ((iter%2==0 ? distance : distance*-1))}, interval);
    }//for
    $(div).animate({ left: 0},interval);
}//shake

编辑:更新代码以将元素返回到原始位置。仍然相信这是解决问题的最轻、最好的解决方案。

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.

function shake(div,interval=100,distance=10,times=4){
    $(div).css('position','relative');
    for(var iter=0;iter<(times+1);iter++){
        $(div).animate({ left: ((iter%2==0 ? distance : distance*-1))}, interval);
    }//for
    $(div).animate({ left: 0},interval);
}//shake

EDIT: Updated code to return element to original position. Still believe this is the lightest and best solution to the problem.

停滞 2024-10-14 04:04:31

我前段时间写了一些简单的 jquery 动画:

https://github.com/yckart/jquery-自定义动画

/**
 * @param {number} times - The number of shakes
 * @param {number} duration - The speed amount
 * @param {string} easing - The easing method
 * @param {function} complete - A callback function
 */
jQuery.fn.shake =
jQuery.fn.wiggle = function (times, duration, easing, complete) {
    var self = this;

    if (times > 0) {
        this.animate({
            marginLeft: times-- % 2 === 0 ? -15 : 15
        }, duration, easing, function () {
            self.wiggle(times, duration, easing, complete);
        });
    } else {
        this.animate({
            marginLeft: 0
        }, duration, easing, function () {
            if (jQuery.isFunction(complete)) {
                complete();
            }
        });
    }
    return this;
};

I wrote some time ago a few simple jquery animations:

https://github.com/yckart/jquery-custom-animations

/**
 * @param {number} times - The number of shakes
 * @param {number} duration - The speed amount
 * @param {string} easing - The easing method
 * @param {function} complete - A callback function
 */
jQuery.fn.shake =
jQuery.fn.wiggle = function (times, duration, easing, complete) {
    var self = this;

    if (times > 0) {
        this.animate({
            marginLeft: times-- % 2 === 0 ? -15 : 15
        }, duration, easing, function () {
            self.wiggle(times, duration, easing, complete);
        });
    } else {
        this.animate({
            marginLeft: 0
        }, duration, easing, function () {
            if (jQuery.isFunction(complete)) {
                complete();
            }
        });
    }
    return this;
};
冧九 2024-10-14 04:04:31

这并不完美,但很实用

    // Example: $('#<% =ButtonTest.ClientID %>').myshake(3, 120, 3, false);
    jQuery.fn.myshake = function (steps, duration, amount, vertical) {
        var s = steps || 3;
        var d = duration || 120;
        var a = amount || 3;
        var v = vertical || false;
        this.css('position', 'relative');
        var cur = parseInt(this.css(v ? "top" : "left"), 10);
        if (isNaN(cur))
            cur = 0;

        var ds = d / s;

        if (v) {
            for (i = 0; i < s; i++)
                this.animate({ "top": cur + a + "px" }, ds).animate({ "top": cur - a + "px" }, ds);
            this.animate({ "top": cur }, 20);
        }
        else {
            for (i = 0; i < s; i++)
                this.animate({ "left": cur + a }, ds).animate({ "left": cur - a + "px" }, ds);
            this.animate({ "left": cur }, 20);
        }

        return this;
    }

This is not perfect, but functional

    // Example: $('#<% =ButtonTest.ClientID %>').myshake(3, 120, 3, false);
    jQuery.fn.myshake = function (steps, duration, amount, vertical) {
        var s = steps || 3;
        var d = duration || 120;
        var a = amount || 3;
        var v = vertical || false;
        this.css('position', 'relative');
        var cur = parseInt(this.css(v ? "top" : "left"), 10);
        if (isNaN(cur))
            cur = 0;

        var ds = d / s;

        if (v) {
            for (i = 0; i < s; i++)
                this.animate({ "top": cur + a + "px" }, ds).animate({ "top": cur - a + "px" }, ds);
            this.animate({ "top": cur }, 20);
        }
        else {
            for (i = 0; i < s; i++)
                this.animate({ "left": cur + a }, ds).animate({ "left": cur - a + "px" }, ds);
            this.animate({ "left": cur }, 20);
        }

        return this;
    }
相守太难 2024-10-14 04:04:31

基于@el生产者解决方案,我添加了一些乘法逻辑,并使其看起来像随机摇动。

jQuery.fn.shake = function (interval, distance, times) {
    interval = typeof interval == "undefined" ? 100 : interval;
    distance = typeof distance == "undefined" ? 10 : distance;
    times = typeof times == "undefined" ? 3 : times;
    var jTarget = $(this);
    jTarget.css('position', 'relative');
    for (var iter = 0; iter < (times + 1) ; iter++) {
        jTarget.animate({ top: ((iter % 2 == 0 ? distance * Math.random() : distance * Math.random() * -1)), left: ((iter % 2 == 0 ? distance * Math.random() : distance * Math.random() * -1)) }, interval);
    }
    return jTarget.animate({ top: 0 , left: 0 }, interval);
}

Based on @el producer solution, I added some multiply logic and make it look like a random shake.

jQuery.fn.shake = function (interval, distance, times) {
    interval = typeof interval == "undefined" ? 100 : interval;
    distance = typeof distance == "undefined" ? 10 : distance;
    times = typeof times == "undefined" ? 3 : times;
    var jTarget = $(this);
    jTarget.css('position', 'relative');
    for (var iter = 0; iter < (times + 1) ; iter++) {
        jTarget.animate({ top: ((iter % 2 == 0 ? distance * Math.random() : distance * Math.random() * -1)), left: ((iter % 2 == 0 ? distance * Math.random() : distance * Math.random() * -1)) }, interval);
    }
    return jTarget.animate({ top: 0 , left: 0 }, interval);
}
探春 2024-10-14 04:04:31

立场必须绝对站在我这边,所以我将其更改为:

jQuery.fn.shake = function(interval, distance, times) {
  interval = typeof interval == "undefined" ? 100 : interval;
  distance = typeof distance == "undefined" ? 10 : distance;
  times = typeof times == "undefined" ? 3 : times;
  var jTarget = $(this);
  for (var iter=0;iter<(times+1);iter++) {
    jTarget.animate({ 'padding-left': ((iter%2==0 ? distance : distance*-1))}, interval);
  }
  return jTarget.animate({ 'padding-left': 0 } ,interval);
}

Position had to be absolute on my side, so I changed it to:

jQuery.fn.shake = function(interval, distance, times) {
  interval = typeof interval == "undefined" ? 100 : interval;
  distance = typeof distance == "undefined" ? 10 : distance;
  times = typeof times == "undefined" ? 3 : times;
  var jTarget = $(this);
  for (var iter=0;iter<(times+1);iter++) {
    jTarget.animate({ 'padding-left': ((iter%2==0 ? distance : distance*-1))}, interval);
  }
  return jTarget.animate({ 'padding-left': 0 } ,interval);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文