简体   繁体   中英

Javascript/CSS slideshow: Using transparencies highlights flaws I don't know how to fix

As much as I'd love to have this be as broad as possible to help future coders, this is a rather specific problem.

As you can see in this jsfiddle , this slideshow seems to have an issue with pictures with transparencies. With transparencies, there is a fairly jarring transition. If possible, I'd like to have the old picture fade out as the new picture is fading in.

And a note: the slideshadow div is there for a reason, mainly for box-shadow uses with greater control over z-index.

HTML:

<div id="slideshow">
   <img class="active" src="http://i.imgur.com/tqMy9wn.png" alt="" />
   <img src="http://i.imgur.com/vNyEFB0.png" alt="" />
   <div id="slideshadow">&nbsp;</div>
</div>

CSS:

#slideshow {
    position:relative;
    float:left;
    height:245px;
    width:200px;
}

#slideshow IMG {
    position:absolute;
    top:0;
    left:0;
    z-index:8;
    opacity:0.0;
}

#slideshow IMG.active {
    z-index:10;
    opacity:1.0;
}

#slideshow IMG.last-active {
    z-index:9;
}

#slideshadow {
    position:absolute;
    height:245px;
    width:200px;
    box-shadow:inset 0px 0px 80px 0px rgba(0,0,0,0.75);
    z-index:15;
}

JS:

  function slideSwitch() {
    var $active = $('#slideshow IMG.active');

    if ($active.length == 0) $active = $('#slideshow IMG:last');

    var $next = $active.next('img').length ? $active.next('img') : $('#slideshow IMG:first');

    $active.addClass('last-active');

    $next.css({
        opacity: 0.0
    })
        .addClass('active')
        .animate({
        opacity: 1.0
    }, 2000, function () {
        $active.removeClass('active last-active');
    });
}

$(function () {
    setInterval(slideSwitch, 7000);
});

Adding an explicit fade to transparent seems to work:

$active.animate({
    opacity: 0.0
}, 2000);

http://jsfiddle.net/tbyccesq/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM