0

我在使用这个 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中将不透明度添加到last-active,但这并没有奏效。是否可以切换背景图像透明度而不会过多地弄乱代码?

4

3 回答 3

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>
于 2013-08-31T16:42:14.333 回答
1

演示

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);
});
于 2013-08-31T16:43:54.513 回答
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 );
});
于 2013-08-31T17:03:59.817 回答