简体   繁体   English

在CSS3中转换的延迟模糊滤镜

[英]Delayed blur-filter with transition in CSS3

I need to do first a faded blur on a element with a delay of the transition after loading. 首先,我需要在加载后延迟转换的元素上做一个褪色模糊。 Second another element should be faded in. I'm using animate.css. 第二个元素应该淡入。我正在使用animate.css。

I'm using animate.css. 我正在使用animate.css。

HTML HTML

    <div class="main">
        <div class="wrapper">
            <div id="target" class="blur>This element has a background-image, which should be blured</div>
        </div>
        <div class="content-layer">
            <input id="searchMain" class="animated fadeIn delay">
        </div>
    </div>

CSS CSS

.blur {
    -webkit-filter: blur(5px);
       -moz-filter: blur(5px);
        -ms-filter: blur(5px);
         -o-filter: blur(5px);
            filter: blur(5px);

    -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;

    -webkit-transition-delay: 1s;
            transition-delay: 1s;
}
.delay {
    -webkit-animation-delay: 1s;
       -moz-animation-delay: 1s;
            animation-delay: 1s;
}

With this, the delayed fade-in of the inputField works great. 有了这个,inputField的延迟淡入效果很好。 The target gets also blurred. target也变得模糊。 But this blur is not animated and not delayed. 但这种模糊不是动画而是没有延迟。

As mentioned in comments, the code in question was adding a transition and a transition-delay to the target element but there was no change of state for a transition to happen. 正如评论中所提到的,有问题的代码是向target元素添加transitiontransition-delay ,但转换发生时状态没有变化。

Transition can happen only when there is a change of state either due to user interaction like :hover , :focus etc or due to scripting like addition of a class on click or time out etc. Hence, transitions are not suited for your purpose and you should consider using animation instead. 转换只有在由于用户交互而导致状态发生变化时才会发生:hover:focus等,或者由于脚本在点击或超时等情况下添加类别等等。因此,转换不适合您的目的而且您应考虑使用animation代替。 Animations can start automatically on page load unlike transition . transition不同,动画可以在页面加载时自动启动。

For an element to be blurred after a 1s delay on page load, set the original state of the element to the unblurred state and then animate it to the blurred state like in the below snippet. 对于在页面加载1秒延迟后要模糊的元素,将元素的原始状态设置为不模糊状态,然后将其设置to模糊状态,如下面的代码段所示。

 #target{ height: 100px; width: 500px; background: url(http://lorempixel.com/500/100); } .blur { -webkit-animation: blur 0.5s linear forwards; -moz-animation: blur 0.5s linear forwards; -ms-animation: blur 0.5s linear forwards; -o-animation: blur 0.5s linear forwards; animation: blur 0.5s linear forwards; -webkit-animation-delay: 1s; -moz-animation-delay: 1s; animation-delay: 1s; } .delay { -webkit-animation-delay: 1s; -moz-animation-delay: 1s; animation-delay: 1s; } @-webkit-keyframes blur { to { -webkit-filter: blur(5px); filter: blur(5px); } } @-moz-keyframes blur { to { -moz-filter: blur(5px); filter: blur(5px); } } @keyframes blur { to { -webkit-filter: blur(5px); -moz-filter: blur(5px); filter: blur(5px); } } 
 <div class="main"> <div class="wrapper"> <div id="target" class="blur">This element has a background-image, which should be blured</div> </div> <div class="content-layer"> <input id="searchMain" class="animated fadeIn delay"> </div> </div> 

Since you're not specifically asking for a CSS/HTML only solution, here's a way to get what you want by adding some jquery to the mix. 既然你没有专门要求一个CSS / HTML解决方案,这里有一种方法可以通过添加一些jquery来获得你想要的东西。

<div class="main">
    <div class="wrapper">
        <div id="target" class=">This element has a background-image, which should be blured</div><!-- Start without classes -->
    </div>
    <div class="content-layer">
        <input id="searchMain" class="animated fadeIn delay"> 
    </div>
</div>

<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript" defer>
    $(function() {
        setTimeout(function(){
            console.log('done waiting!'); // Just to verify the delay works
            $('#target').addClass('blur');
        }, 1000); // A delay of 1000ms
    });
</script> 

Needless to say, make sure the animations are working as you want them. 不用说,确保动画按照您的需要运行。 This is just for the delay, after pageload. 这仅适用于页面加载后的延迟。 From here it's pretty easy to build upon that aswell. 从这里开始,它也很容易建立起来。 Like adding the animation when the user has scrolled to the element, etc. etc. 就像在用户滚动到元素等时添加动画一样。

Updated for #target 针对#target进行了更新

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

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