简体   繁体   English

如何为CSS框阴影深度设置动画(使用jQuery或CSS3过渡)?

[英]How do I animate CSS box-shadow depth (with jQuery or CSS3 transitions)?

This does do the delays but doesn't seem to apply the style changes until the end: 这确实会产生延迟,但似乎直到最后才应用样式更改:

for (i=20;i>=0;i--) {            
    var boxShadow = i+"px "+i+"px "+i+"px #888";
    $('article').css("box-shadow", boxShadow);
    sleep(20);
}
function sleep(ms)
{
    var dt = new Date();
    dt.setTime(dt.getTime() + ms);
    while (new Date().getTime() < dt.getTime());
}

This doesn't apply the delays at all: 这根本不适用延迟:

for (i=20;i>=0;i--) {            
    var boxShadow = i+"px "+i+"px "+i+"px #888";
    $('article').delay(500).css("box-shadow", boxShadow);
}

Can this be done more easily with css3 transitions? 可以使用CSS3转换更轻松地完成此操作吗? Am I just making some small jquery error in the delay sample? 我只是在延迟样本中犯了一些小的jquery错误吗?

Thank you to anyone who can help. 谢谢任何可以帮助的人。

You can use classes and setTimeout to utilize CSS3 transitions for your animation effect: 您可以使用类和setTimeout为动画效果利用CSS3过渡:

CSS -- CSS-

#some-element {
    -webkit-transition : all 0.5s linear;
       -moz-transition : all 0.5s linear;
        -ms-transition : all 0.5s linear;
         -o-transition : all 0.5s linear;
            transition : all 0.5s linear;
}
#some-element.ani-state {
    -webkit-box-shadow : 0 0 24px #000;
       -moz-box-shadow : 0 0 24px #000;
            box-shadow : 0 0 24px #000;
}

I used all for the transition declarations because of Chrome... some versions of Chrome use -webkit-box-shadow and newer versions use box-shadow . 由于Chrome,我将all用于过渡声明。Chrome的某些版本使用-webkit-box-shadow而较新的版本使用box-shadow all isn't a big deal if you aren't changing any other properties of the element (or if you want to animate those property changes). all是不是,如果你不改变元素的任何其他属性一个大问题(或者,如果要动画这些属性的更改)。

JS -- JS-

$(function () {

    var $someElement = $('#some-element');

    $someElement.on('click', function () {
        $someElement.addClass('ani-state');
        setTimeout(function () {
            $someElement.removeClass('ani-state');
        }, 500);
    });
});

Here is a demo: http://jsfiddle.net/jasper/tvfPq/1/ 这是一个演示: http : //jsfiddle.net/jasper/tvfPq/1/

Note that in the demo I used two box-shadows : box-shadow : 0 0 24px #000, inset 0 0 24px #999; 请注意,在演示中,我使用了两个box-shadowsbox-shadow : 0 0 24px #000, inset 0 0 24px #999;

A jquery plugin was recently released that does this. 最近发布了一个jquery插件来执行此操作。 Take a peak: Shadowmation 登顶: 暗影之歌

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM