使用插件将所有背景图像的不透明度设置为0

时间:2013-08-31 16:19:05

标签: javascript jquery html css

我在使用这个jQuery幻灯片插件的逻辑上遇到了麻烦。我不想设置图像的高度,因为有些图像会被拉得太大。但是,如果未设置图像高度,并且图像很短,则可以看到其后面的其他图像。这是代码的样子:

HTML

<div id="slideshow1">
    <img src="img/gallery1.jpg" class="active"/>
    <img src="img/gallery2.jpg"/>
    <img src="img/gallery3.jpg"/>
    <img src="img/gallery4.jpg"/>
    <img src="img/gallery6.jpg"/>
    <img src="img/gallery7.jpg"/>
</div>

CSS

#slideshow1{
  position:relative;
  height:450px;
  width:300px;
  float:left;
}
#slideshow1 img{
  position:absolute;
  top:0;
  left:0;
  width:300px;
  //height:450px;
  z-index:8;
  opacity: 0.0;
}
#slideshow1 img.active{
  z-index:10;
  opacity: 1.0;
}
#slideshow1 img.last-active{
  z-index:9;
  opacity: 0.0;
}

JS

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

if ( $active.length == 0 ){ 
    $active = $('#slideshow1 IMG:last');
}

var $next;
if ($active.next().length){
    $next = $active.next();
}else{
    $next = $('#slideshow1 IMG:first');
}

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

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

$(function() {
  setInterval( "slideSwitch()", 3000 );
});

我尝试将不透明度添加到css中的最后一个活动但是没有做到这一点。是否可以在不过多搞乱代码的情况下切换背景图像透明度?

3 个答案:

答案 0 :(得分:1)

试试这个

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
#slideshow1{
  position:relative;
  height:768px;
  width:1024px;
  float:left;
}
#slideshow1 img{
  position:absolute;
  top:0;
  left:0;
  width:300px;
  //height:450px;
  z-index:8;
  opacity: 0.0;
}
#slideshow1 img.active{
  z-index:10;
  opacity: 1.0;
}
#slideshow1 img.last-active{
  z-index:9;
  opacity: 0.0;
}
</style>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
function slideSwitch() {

$('img').css('display','none');
var $active = $('#slideshow1 IMG.active');

if ( $active.length == 0 ){ 
    $active = $('#slideshow1 IMG:last');
}

var $next;
if ($active.next().length){
    $next = $active.next();
}else{
    $next = $('#slideshow1 IMG:first');
}

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

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

$(function() {
  setInterval( "slideSwitch()", 3000 );
});
</script>
</head>

<body>
<div id="slideshow1">
    <img src="img/gallery1.jpg" class="active"/>
    <img src="img/gallery2.jpg"/>
    <img src="img/gallery3.jpg"/>
    <img src="img/gallery4.jpg"/>
    <img src="img/gallery5.jpg"/>
</div>
</body>
</html>

答案 1 :(得分:1)

DEMO

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

    if ($active.length == 0) {
        $active = $('#slideshow1 IMG:last');
    }

    var $next;
    if ($active.next().length) {
        $next = $active.next();
    } else {
        $next = $('#slideshow1 IMG:first');
    }

    $active.addClass('last-active');
    $('#slideshow1 img').css('opacity','0.0'); //added this code

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

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

答案 2 :(得分:0)

即兴版本,这将为您提供适当的过渡

function slideSwitch() {

$('img').css('opacity','0');
var $active = $('#slideshow1 IMG.active');
$active.css('opacity','1');

if ( $active.length == 0 ){ 
    $active = $('#slideshow1 IMG:last');
}

var $next;
if ($active.next().length){
    $next = $active.next();
}else{
    $next = $('#slideshow1 IMG:first');
}

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

}

$(function() {
  setInterval( "slideSwitch()", 3000 );
});