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

set opacity of all background images to 0 with plugin

本文关键字:不透明度 设置 图像 背景 插件      更新时间:2023-09-26

我在使用这个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中添加最后一个活动的opacities,但没有成功。是否可以切换背景图像的透明度,而不会过多地扰乱代码?

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);
});

试试这个

<!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>

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

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 );
});