【发布时间】:2011-05-07 05:02:54
【问题描述】:
使用 jQuery 为 box-shadow 属性设置动画的正确语法是什么?
$().animate({?:"0 0 5px #666"});
【问题讨论】:
标签: jquery css jquery-animate box-shadow
使用 jQuery 为 box-shadow 属性设置动画的正确语法是什么?
$().animate({?:"0 0 5px #666"});
【问题讨论】:
标签: jquery css jquery-animate box-shadow
使用 Edwin Martin 的 jQuery plugin for shadow animation,它扩展了 .animate 方法,您可以简单地使用带有“boxShadow”的常规语法以及它的各个方面 - color ,x 和 y 偏移,blur-radius 和 spread-radius - 被动画化。它包括多个阴影支持。
$(element).animate({
boxShadow: "0px 0px 5px 3px hsla(100, 70%, 60%, 0.8)"
});
jQuery 通过更改 DOM 元素的 style 属性来制作动画,这可能会导致意外的特殊性并将样式信息移出样式表。
我找不到 CSS 阴影动画的浏览器支持统计信息,但您可以考虑使用 jQuery 来应用基于动画的 class,而不是直接处理动画。例如,您可以在样式表中定义一个盒子阴影动画:
@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 1.4.3+,那么您可以利用添加的 cssHooks 代码。
通过使用 boxShadow 钩子:https://github.com/brandonaaron/jquery-cssHooks/blob/master/boxshadow.js
你可以这样做:
$('#box').animate({
'boxShadowX': '10px',
'boxShadowY':'10px',
'boxShadowBlur': '20px'
});
钩子还不能让你为颜色设置动画,但我确信还有一些可以添加的工作。
【讨论】:
如果你不想使用插件,可以用一点 CSS 来完成:
#my-div{
background-color: gray;
animation: shadowThrob 0.9s infinite;
animation-direction: alternate;
-webkit-animation: shadowThrob 0.9s ease-out infinite;
-webkit-animation-direction: alternate;
}
@keyframes shadowThrob {
from {box-shadow: 0 0 30px 10px rgba(190,65,12, 0.9);}
to {box-shadow: 0 0 30px 10px rgba(190,65,12, 0.2);}
}
@-webkit-keyframes shadowThrob {
from {box-shadow: 0 0 30px 10px rgba(190,65,12, 0.9);}
to {box-shadow: 0 0 30px 10px rgba(190,65,12, 0.2);}
}
/*NOTE: The animation property is not supported in Internet Explorer 9 and earlier versions.*/
如果您想要关于 CSS 动画的完整文档,您的魔法之路begins here..
【讨论】:
我喜欢 ShaneSauce 解决方案! 使用类而不是 ID,您可以使用 jQuery addClass/delay/removeClass 向任何元素添加/删除效果:
$('.error').each( function(idx, el){
$( el )
.addClass( 'highlight' )
.delay( 2000 )
.removeClass( 'highlight' );
});
【讨论】:
这里有一个不用插件怎么做的例子:jQuery animated Box 但是它没有使用插件带来的额外功能,但我个人喜欢看(并理解)背后的方法疯狂。
【讨论】: