【问题标题】:How to use transition for background image slide show without fading the contents?如何在不褪色内容的情况下对背景图像幻灯片使用过渡?
【发布时间】:2014-06-09 18:40:56
【问题描述】:

我想使用 jQuery 为背景图像幻灯片放映使用 css 过渡。 是否可以在不淡化 div 内容的情况下这样做? 如果是这样,如何?请帮忙。 这是我到目前为止没有转换的代码:

jQuery(window).load(function(){
var images = ['images/slide1.jpg','images/slide3.jpg','images/slide4.jpg','images/slide5.jpg','images/slide7.jpg',];
var i = 0;
var timeoutVar;

function changeBackground() {
    clearTimeout(timeoutVar); // just to be sure it will run only once at a time

    jQuery('#page').css('background-image', function() {
        if (i >= images.length) {
            i=0;
        }
        return 'url(' + images[i++] + ')';      
    });

    // call the setTimeout every time to repeat the function
    timeoutVar = setTimeout(changeBackground, 5000);
}

// Call it on the first time and it will repeat
changeBackground();        
});

【问题讨论】:

    标签: jquery slideshow css-transitions


    【解决方案1】:

    当然有可能;您必须使用 CSS transition 属性。

    只需添加这个 CSS:

    #page {
        transition: background-image .7s ease-in-out;
       -moz-transition: background-image .7s ease-in-out;
       -webkit-transition: background-image .7s ease-in-out; 
       -o-transition: background-image .7s ease-in-out; 
    }
    

    每次更改 #page div 的 background-image 属性时,都会触发一个持续时间为 0.7 秒的动画(您也可以更改该值以适应您的需要)。

    这是fiddle。

    编辑:这仅适用于基于 webkit 的浏览器。如果我能找到跨浏览器的解决方案,我会对此进行编辑。

    【讨论】:

    • 谢谢,但奇怪的是它只适用于我的 chrome。而不是firefox和IE。也许我的电脑有问题?
    • 不;我检查了一下,它在我的 Moz 和 IE 中也不起作用。我刚刚在这里(w3.org/TR/css3-transitions/#animation-of-property-types-)读到 background-image 属性不是动画的,这可能是问题所在。对于那个很抱歉!我会尝试找出是否有其他方法可以达到结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    • 2022-01-25
    • 2013-07-05
    相关资源
    最近更新 更多