【问题标题】:Add Fade Effect in Slideshow (Javascript)在幻灯片中添加淡入淡出效果 (Javascript)
【发布时间】:2014-10-10 11:02:12
【问题描述】:

我创建了一个 JavaScript 幻灯片,但我不知道如何添加淡入淡出效果。请告诉我怎么做,请只用 JavaScript 告诉我,不要用 jQuery!

代码:

var imgArray = [
    'img/slider1.jpg',
    'img/slider2.jpg',
    'img/slider3.jpg'],
    curIndex = 0;
    imgDuration = 3000;

function slideShow() {
    document.getElementById('slider').src = imgArray[curIndex];
    curIndex++;
    if (curIndex == imgArray.length) { curIndex = 0; }
    setTimeout("slideShow()", imgDuration);
}
slideShow();

【问题讨论】:

标签: javascript html slideshow


【解决方案1】:

更短的答案:

HTML:

<div class="js-slideshow">
    <img src="[your/image/path]">
    <img src="[your/image/path]" class="is-shown">
    <img src="[your/image/path]">
</div>

Javascript:

setInterval(function(){
    var $container = $('.js-slideshow'),
        $currentImage = $container.find('.is-shown'),
        currentImageIndex = $currentImage.index() + 1,
        imagesLength = $container.find('img').length;

    $currentImage.removeClass('is-shown');
    $currentImage.next('img').addClass('is-shown');

    if ( currentImageIndex == imagesLength ) {
        $container.find('img').first().addClass('is-shown');
    }
}, 5000)

SCSS

.promo-banner {
   height: 300px;
   width: 100%;
   overflow: hidden;
   position: relative;

   img {
       width: 100%;
       height: 100%;

       position: absolute;
       top: 0;
       left: 0;
       right: 0;
       bottom: 0;

       opacity: 0;
       z-index: -10;
       transition: all 800ms;

       &.is-shown {
           transition: all 800ms;
           opacity: 1;
           z-index: 10;
       }
  }

}

【讨论】:

    【解决方案2】:

    作为替代方案。如果您正在尝试制作滑块。

    通常的方法是动画出帧并动画入帧。

    这就是幻灯片效果和淡入淡出效果起作用的原因。您的示例逐渐消失。这很好,但一旦您看到它工作,可能不是您真正想要的。

    如果你真正想要的是动画图像进出……你需要一些更复杂的东西。

    要使图像进出动画,您必须为每个图像元素使用一个图像元素,然后翻转一个并翻转一个。如果要滑动,图像需要在淡入淡出的情况下彼此叠放你把它们放在一起。

    然后您的幻灯片功能就会发挥作用,但在您执行此操作之前,您需要将数组中的所有图像添加到 dom 中,这称为动态 dom 注入,它真的很酷。

    确保您检查小提琴以获取完整的工作演示和底部链接的代码。

    HTML

    <div id="slider">
    // ...we will dynamically add your images here, we need element for each image
    </div>
    

    JS

    var curIndex = 0,
        imgDuration = 3000,
        slider = document.getElementById("slider"),
        slides = slider.childNodes; //get a hook on all child elements, this is live so anything we add will get listed
        imgArray = [
            'http://placehold.it/300x200',
            'http://placehold.it/200x100',
            'http://placehold.it/400x300'];
    
    
    //
    // Dynamically add each image frame into the dom;
    //
    function buildSlideShow(arr) {
        for (i = 0; i < arr.length; i++) {
            var img = document.createElement('img');
            img.src = arr[i];
            slider.appendChild(img);
        }
        // note the slides reference will now contain the images so we can access them
    }
    
    //
    // Our slideshow function, we can call this and it flips the image instantly, once it is called it will roll
    // our images at given interval [imgDuration];
    //
    function slideShow() {
    
        function fadeIn(e) {
            e.className = "fadeIn";
        };
    
        function fadeOut(e) {
            e.className = "";
        };
    
    
            // first we start the existing image fading out;
    
            fadeOut(slides[curIndex]);
    
            // then we start the next image fading in, making sure if we are at the end we restart!
            curIndex++;
            if (curIndex == slides.length) {
                curIndex = 0;
            }
    
            fadeIn(slides[curIndex]);
    
            // now we are done we recall this function with a timer, simple.
    
            setTimeout(function () {
                slideShow();
            }, imgDuration);
        };
    
    
        // first build the slider, then start it rolling!
    
        buildSlideShow(imgArray);
        slideShow();
    

    小提琴: http://jsfiddle.net/f8d1js04/2/

    【讨论】:

    • 如果你想要一个真正的交叉淡入淡出,这是要走的路。其他答案只是给出了顺序淡出/淡入效果,但这个解决方案提供了同时交叉淡入淡出,这更好。谢谢你的小提琴!
    【解决方案3】:

    比 Ninja 的解决方案短得多,并且具有硬件加速的 CSS3 动画。 http://jsfiddle.net/pdb4kb1a/2/只要确保过渡时间(1s)与第一个超时函数(1000(ms))相同即可。

    纯 JS

    var imgArray = [
        'http://placehold.it/300x200',
        'http://placehold.it/200x100',
        'http://placehold.it/400x300'],
        curIndex = 0;
        imgDuration = 3000;
    
    function slideShow() {
        document.getElementById('slider').className += "fadeOut";
        setTimeout(function() {
            document.getElementById('slider').src = imgArray[curIndex];
            document.getElementById('slider').className = "";
        },1000);
        curIndex++;
        if (curIndex == imgArray.length) { curIndex = 0; }
        setTimeout(slideShow, imgDuration);
    }
    slideShow();
    

    CSS

    #slider {
        opacity:1;
        transition: opacity 1s; 
    }
    
    #slider.fadeOut {
        opacity:0;
    }
    

    【讨论】:

    • 谢谢你!这正是我想要的。感谢您的帮助。 :) (Y)
    • 简单易用 - 谢谢!我对其进行了一些更改,所以它会随着 onClick 淡出和淡入 - http://jsfiddle.net/7p0kbxv8/ Cheers, Stephan
    【解决方案4】:

    您可以使用此代码

    var fadeEffect=function(){
    
        return{
    
            init:function(id, flag, target){
    
                this.elem = document.getElementById(id);
    
                clearInterval(this.elem.si);
    
                this.target = target ? target : flag ? 100 : 0;
    
                this.flag = flag || -1;
    
                this.alpha = this.elem.style.opacity ? parseFloat(this.elem.style.opacity) * 100 : 0;
    
                this.elem.si = setInterval(function(){fadeEffect.tween()}, 20);
    
            },
    
            tween:function(){
    
                if(this.alpha == this.target){
    
                    clearInterval(this.elem.si);
    
                }else{
    
                    var value = Math.round(this.alpha + ((this.target - this.alpha) * .05)) + (1 * this.flag);
    
                    this.elem.style.opacity = value / 100;
    
                    this.elem.style.filter = 'alpha(opacity=' + value + ')';
    
                    this.alpha = value
    
                }
    
            }
    
        }
    
    }();
    

    这是如何使用它

    fadeEffect.init('fade', 1, 50) // fade in the "fade" element to 50% transparency
    
    fadeEffect.init('fade', 1) // fade out the "fade" element
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      • 1970-01-01
      • 1970-01-01
      • 2017-12-26
      • 1970-01-01
      • 1970-01-01
      • 2011-09-17
      相关资源
      最近更新 更多