jQuery - 我可以为 CSS 属性设置动画吗?特别是背景图像
我知道我可以淡入淡出 div 和元素,但我也可以为属性设置动画吗? 我刚才在更改某个div的background-image属性时想到了这一点。并且认为如果背景图像也淡入的话会很酷;)
这是我用来替换背景图像的代码。但它可以动画化吗?
var originalBG = $('#wrapper').css('background-image');
$('.bggallery_images img').hover(function () {
var newBG = "url('" + $(this).attr('src');
$('#wrapper').css('background-image', newBG);
}, function () {
$('#wrapper').css('background-image', originalBG);
});
I know I can fade in and out divs and elements, but can I animate properties as well?
I thought about this just now when changing background-image property of a certain div. And thought it'd be cool if the background image was faded in as well ;)
Here's the code I'm using to replace background image. But can it be animated?
var originalBG = $('#wrapper').css('background-image');
$('.bggallery_images img').hover(function () {
var newBG = "url('" + $(this).attr('src');
$('#wrapper').css('background-image', newBG);
}, function () {
$('#wrapper').css('background-image', originalBG);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
jQuery 有 animate() 方法来完成此任务。
查看手册中的示例,它们应该向您展示您需要的一切。
jQuery has the animate() method that does exactly that.
Check out the examples in the manual, they should show you everything you need.
JQuery UI 是一个插件,可以将动画功能扩展到动画颜色(如果您想要这样做)背景。基本上,如果 JQuery 不这样做,很可能有一个库可以扩展它以具有该功能。
JQuery UI is a plug in that extends the animation function to animate color if that's what you're looking to do with the background. Basically if JQuery doesn't do it, there's most likely a library that extends it to have that functionality.