javascript - 使用淡入淡出效果更改背景图像的最佳方法是什么?

标签 javascript jquery html css

     #compareDiv{
    position: fixed; 
    bottom: 0px; 
    right: 220px; 
    background:url(images/compare-V1.png) top left no-repeat; 
    width:80px;
    height:16px; 
    padding:2px 6px;  
    color:#FFF; 
    font-size:12px; 
    font-weight:bold; 
    cursor:pointer;
    }
    #compareDiv:hover{
    background:url(images/compare-V1.png) bottom left no-repeat;
    }

这是我的代码,我需要的是在它正常悬停时使用 css 更改此快速效果并提供缓慢的效果更改。 有什么想法吗?

最佳答案

您需要使用图像标签而不是 CSS 背景属性。您可以通过将图像标签设置为 z-index:0

使图像标签充当背景图像

CSS

    body, html{
        margin:0;
        padding:0;
        background:black;
    }
    img{
        position:absolute;
        top:0;
        display:none;
        width:100%;
        height:100%;
    }

HTML

<img src="http://...1.jpg" />
<img src="http://...2.jpg" />
<img src="http://...3.jpg" />

jQuery

function test() {
    $("img").each(function(index) {
        $(this).hide();
        $(this).delay(3000 * index).fadeIn(3000).fadeOut();
    });
}
test();

http://jsfiddle.net/RyGKV/ 查看工作示例

关于javascript - 使用淡入淡出效果更改背景图像的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7748723/

相关文章:

javascript - 如何在 JavaScript 中访问父元素的子元素

javascript - 在 fullcalendar 中如何禁止在我的自定义 View 中选择

javascript - 字符串替换在 jQuery 中的复选框取消选中事件中不起作用

html - 调整窗口大小时 div 定位错误

javascript - 无法将组件传递给组件

php - 允许用户选择页面上的任意元素

jquery - 如何通过ajax重新加载数据

jquery - 可展开列表仅在第二次点击后展开

php - 使用行中的 xpath 提取表格单元格文本内容以供使用?

html - css3 旋转和拉伸(stretch)