Vin*_*rez 15 html css jquery css3
是否可以使用jQuery动画CSS3模糊过滤器?
这作为应用CSS规则的静态方式:
item.css({'filter': 'blur('+blur+')','-webkit-filter': 'blur('+blur+')','-moz-filter': 'blur('+blur+')','-o-filter': 'blur('+blur+')','-ms-filter': 'blur('+blur+')'});
Run Code Online (Sandbox Code Playgroud)
但是当我用animate方法替换css方法时,没有任何反应.
item.animate({'filter': 'blur('+blur+')','-webkit-filter': 'blur('+blur+')','-moz-filter': 'blur('+blur+')','-o-filter': 'blur('+blur+')','-ms-filter': 'blur('+blur+')'},500);
Run Code Online (Sandbox Code Playgroud)
有没有我不知道的伎俩?如何设置项目的模糊度?
Ter*_*rry 20
您可以.animate()在具有数值的变量上使用该函数,并相应地设置动画 - 在每个步骤中调用一个函数并将该新数值指定为CSS滤镜模糊半径属性:)
$(function() {
$({blurRadius: 0}).animate({blurRadius: 10}, {
duration: 500,
easing: 'swing', // or "linear"
// use jQuery UI or Easing plugin for more options
step: function() {
console.log(this.blurRadius);
$('.item').css({
"-webkit-filter": "blur("+this.blurRadius+"px)",
"filter": "blur("+this.blurRadius+"px)"
});
}
});
});
Run Code Online (Sandbox Code Playgroud)
次要更新:jQuery .animate()可能无法正确补间到最终值,如下面的另一个答案中所述.在这种情况下,链接一个手动将模糊半径设置为预期最终值的回调总是更安全.我已将这些功能模块化,以便可以重复使用而不会有太多冗余:
$(function() {
// Generic function to set blur radius of $ele
var setBlur = function(ele, radius) {
$(ele).css({
"-webkit-filter": "blur("+radius+"px)",
"filter": "blur("+radius+"px)"
});
},
// Generic function to tween blur radius
tweenBlur = function(ele, startRadius, endRadius) {
$({blurRadius: startRadius}).animate({blurRadius: endRadius}, {
duration: 500,
easing: 'swing', // or "linear"
// use jQuery UI or Easing plugin for more options
step: function() {
setBlur(ele, this.blurRadius);
},
callback: function() {
// Final callback to set the target blur radius
// jQuery might not reach the end value
setBlur(ele, endRadius);
}
});
};
// Start tweening
tweenBlur('.item', 0, 10);
});
Run Code Online (Sandbox Code Playgroud)
您可以在下面附带的代码段中看到此更新后的代码.
需要注意的是很重要的火狐(FF≥35以上支持unprefix CSS滤镜),IE和Opera具有不支持对CSS3的过滤器,所以没有必要使用-moz-,-ms-或-o-前缀:)
请参阅小提琴:http://jsfiddle.net/teddyrised/c72Eb/(更新前)
有关最新示例,请参阅下面的代码段:
$(function() {
// Generic function to set blur radius of $ele
var setBlur = function(ele, radius) {
$(ele).css({
"-webkit-filter": "blur("+radius+"px)",
"filter": "blur("+radius+"px)"
});
},
// Generic function to tween blur radius
tweenBlur = function(ele, startRadius, endRadius) {
$({blurRadius: startRadius}).animate({blurRadius: endRadius}, {
duration: 500,
easing: 'swing', // or "linear"
// use jQuery UI or Easing plugin for more options
step: function() {
setBlur(ele, this.blurRadius);
},
complete: function() {
// Final callback to set the target blur radius
// jQuery might not reach the end value
setBlur(ele, endRadius);
}
});
};
// Start tweening towards blurred image
window.setTimeout(function() {
tweenBlur('.item', 0, 10);
}, 1000);
// Reverse tweening after 3 seconds
window.setTimeout(function() {
tweenBlur('.item', 10, 0);
}, 3000);
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="item">
<p>Sample text that will be blurred.</p>
<img src="http://placehold.it/500x500" />
</div>Run Code Online (Sandbox Code Playgroud)
mea*_*avo 11
一个谷歌搜索查询给了我这个结果你可能想看看.我建议做的只是在JS中切换一个类并处理CSS中的其余类,例如:
var $item = $('.item'); // or any selector you want to use
$item.addClass('item--blur');
Run Code Online (Sandbox Code Playgroud)
处理CSS中的其余部分:
.item {
transition: all 0.25s ease-out;
}
.item--blur {
// all your filters
}
Run Code Online (Sandbox Code Playgroud)