【问题标题】:Animate background replacement动画背景替换
【发布时间】:2011-03-08 19:17:46
【问题描述】:

如何动画背景图片替换?

我有两个图像,works.png 和works-hover.png。第一个是暗的,第二个是亮的。想要在悬停时用淡入/淡出等动画替换深色。两张图片都是透明的png。

谢谢。

【问题讨论】:

标签: javascript jquery html css background


【解决方案1】:

在 CSS 中这很容易,只需使用另一个 CSS 类和 jQuery 更改背景图像:

.myElement
{
    background-image: url(path/to/flie/works.png);
}

.roll
{
    background-image: url(path/to/flie/works-hover.png);
}

对于淡入淡出,使用jQuery:

$(function() {
    //fade the element, load new bg, bring back the element
    $(".myElement").hover(function() {
        $(this).animate({ 'opacity': 0}, 100), 
            $(this).toggleClass('roll'),
                $(this).animate({'opacity': 1}, 100);
    });
});

I have made an example for you here.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 2011-06-09
    • 2013-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多