在JavaScript幻灯片中简化图像之间的转换

时间:2016-01-03 20:57:36

标签: javascript jquery css3 css-transitions slideshow

我正在使用JavaScript随机显示具有定时间隔的图像数组。

这是剧本:

var NumberOfImages = 5

var img = new Array(NumberOfImages)

    img[0] = "[image here]"
    img[1] = "[image here]"
    img[2] = "[image here]"
    img[3] = "[image here]"
    img[4] = "[image here]"

Array.prototype.shuffle = function () {
    var len = this.length;
    var i = len;
    while (i--) {
        var p = parseInt(Math.random()*len);
        var t = this[i];
    this[i] = this[p];
    this[p] = t;
    }
};

img.shuffle ();

var imgNumber = 0

function NextImage() {
    document.images["VCRImage"].src = img[imgNumber++]

        if (imgNumber == img.length) {
               img.shuffle ();
               imgNumber = 0;
        }
}

window.setInterval (NextImage, 3000);

和html:

<div class="item">
    <script type="text/javascript">document.writeln('<img src="'+img[0]+'" name="VCRImage">');</script>
</div>

可在此处找到一个工作示例:https://jsfiddle.net/admiringtheorchid/esm3u0xg/

我想简化每张图片之间的过渡。如何实现这一目标?

1 个答案:

答案 0 :(得分:1)

使用jQuery尝试此方法:https://jsfiddle.net/joe_young/qxf69hsL/

//create a jQuery object of the image
    var $image = $(document.images["VCRImage"]);

//fade out the image, and once it has finished fading out... 
    $image.fadeOut(function() {  

//change the image source
        $image.attr("src", img[imgNumber++]);                    
    });

//fade the image back in
    $image.fadeIn();