Which is the correct syntax to animate the box-shadow property with jQuery?

$().animate({?:"0 0 5px #666"});

推荐答案

Direct answer

使用Edwin Martin的100,它扩展了.animate方法,你可以简单地使用"boxShadow"的正常语法,它的每一个方面——colorx- and y-offsetblur-radiusspread-radius——都会被动画化.它包括多个阴影支持.

$(element).animate({ 
  boxShadow: "0px 0px 5px 3px hsla(100, 70%, 60%, 0.8)"
}); 

Using CSS animations instead

jQuery animates by changing the style property of DOM elements, which can cause surprises with specificity and moves style information out of your stylesheets.

I can't find browser support stats for CSS shadow animation, but you might consider using jQuery to apply an animation-based class instead of handling the animation directly. For example, you can define a box-shadow animation in your stylesheet:

@keyframes shadowPulse {
    0% {
        box-shadow: 0px 0px 10px 0px hsla(0, 0%, 0%, 1);
    }

    100% {
        box-shadow: 0px 0px 5px 0px hsla(0, 0%, 0%, 0);
    }
}

.shadow-pulse {
    animation-name: shadowPulse;
    animation-duration: 1.5s;
    animation-iteration-count: 1;
    animation-timing-function: linear;
}

然后,您可以使用本机animationend事件将动画的结尾与您在JS代码中执行的操作同步:

$(element).addClass('shadow-pulse');
$(element).on('animationend', function(){    
    $(element).removeClass('shadow-pulse');
    // do something else...
});

Jquery相关问答推荐

将搜索面板和服务器端与POST AJAX请求一起使用时出现DataTables错误

如何在Jquery和Laravel中使用请求传递两个参数给Datatable

JQuery AJAX 成功事件未触发

递归循环遍历对象(树)

在 jQuery 事件中控制this的值

用按钮切换显示/隐藏div?

为什么 jquery fadeIn() 不能与 .html() 一起使用?

jQuery用另一个类替换一个类

如何判断元素是否在屏幕外

jQuery Force 为 iframe 设置 src 属性

通过 AJAX 和 jQuery 使用 HTML5 文件上传

获取跨域 iframe 的 DOM 内容

在jQuery中添加ID?

消除移动 Safari 中点击事件的 300 毫秒延迟

jQuery - 确定输入元素是文本框还是 Select 列表

使用 jQuery 比较两个 Javascript 对象数组

在 jQuery 中 Select 后代元素的最快方法是什么?

如何在 JavaScript / jQuery 中获取对象的属性?

如何确定滚动高度?

JS - 从base64代码中获取图片的宽高