---
title: " JavaScript控制多张图片循环播放（淡入淡出效果）"
author: "dhj"
date: "2008-09-27T14:18:57Z"
tags: []
slug: "javascript"
draft: false
lastmod: "2008-09-27T14:18:57Z"
id: 247486
url: "https://dang.iteye.com/blog/247486"
---

没事写着玩，呵呵，支持IE和FF。

    1.首先找一组图片，放到你的对应目录下。

    2.建立测试HTML页面，代码如下：

[view plain](http://blog.csdn.net/lip009/archive/2008/07/26/2712344.aspx) [copy to clipboard](http://blog.csdn.net/lip009/archive/2008/07/26/2712344.aspx) [print](http://blog.csdn.net/lip009/archive/2008/07/26/2712344.aspx) [?](http://blog.csdn.net/lip009/archive/2008/07/26/2712344.aspx)

001. &lt; style &gt;
002. .imgShow{
003. font-size: 12px;
004. background-color: #B0C6CE;
005. text-align: center;
006. vertical-align: middle;
007. display: table-caption;
008. border-top-width: thin;
009. border-right-width: thin;
010. border-bottom-width: thin;
011. border-left-width: thin;
012. border-top-style: solid;
013. border-right-style: solid;
014. border-bottom-style: solid;
015. border-left-style: solid;
016. position:absolute;
017. width:400px;
018. height:300px;
019. }
020. .imgHide{
021. font-size: 12px;
022. background-color: #B0C6CE;
023. text-align: center;
024. vertical-align: middle;
025. display: table-caption;
026. border-top-width: thin;
027. border-right-width: thin;
028. border-bottom-width: thin;
029. border-left-width: thin;
030. border-top-style: solid;
031. border-right-style: solid;
032. border-bottom-style: solid;
033. border-left-style: solid;
034. position:absolute;
035. filter:alpha(opacity = 0 );
036. opacity:0.00;
037. width:400px;
038. height:300px;
039. }
040. &lt;/ style &gt;
041. &lt; script &gt;
042. var hideTime = null ;
043. var hide = 100 ;//过滤镜初始值，IE
044. var hideFF = 1 .0;//过滤镜初始值，FF
045. var show = 0 ;
046. var showFF = 0 .00;
047. var imgShow = 1 ;
048. var imgSrcArray = new  Array("http://p.blog.csdn.net/images/p\_blog\_csdn\_net/lip009/EntryImages/20080726/0001.jpg","http://p.blog.csdn.net/images/p\_blog\_csdn\_net/lip009/EntryImages/20080726/0004.jpg","http://p.blog.csdn.net/images/p\_blog\_csdn\_net/lip009/EntryImages/20080726/0005.jpg");
049. function hideObject(hideImage,showImage){
050. try{
051. hide hide =hide-1;
052. hideFF hideFF =hideFF-0.01;
053. show show =show+1;
054. showFF showFF =showFF+0.01;
055. hideImage.style.filter = "alpha(opacity=" +hide+")";
056. hideImage.style.opacity = hideFF +"";/\*FF兼容\*/
057. //hideImage.style.-moz-opacity = hideFF +"";
058. showImage.style.filter = "alpha(opacity=" +show+")";
059. showImage.style.opacity = showFF +"";/\*FF兼容\*/
060. if(hide&lt; =0){
061. showImage.style.filter = "alpha(opacity=100)" ;
062. showImage.style.opacity = 1 .0;/\*FF兼容\*/
063. window.clearInterval(hideTime);
064. hideImage.style.display = "none" ;
065. window.setTimeout(changeImage,1000);
066. }
067. }catch(e){
068. window.clearInterval(hideTime);
069. }
070. }
071. function changeImage(){
072. hide = 100 ;
073. hideFF = 1 .0;
074. show = 0 ;
075. showFF = 0 .0;
076. imgShow++;
077. if(imgShow&gt; imgSrcArray.length){
078. imgShow = 1 ;
079. }
080. var img1 = document .getElementById("img1");
081. var img2 = document .getElementById("img2");
082. var imageShow,imageHide;
083. if(img1.style.display =="none"){
084. img1.className = "imgHide" ;
085. img2.className = "imgShow" ;
086. img1.style.display = "" ;
087. img1.src = imgSrcArray \[imgShow-1];
088. imageShow = img1 ;
089. imageHide = img2 ;
090. }else{
091. img2.className = "imgHide" ;
092. img1.className = "imgShow" ;
093. img2.style.display = "" ;
094. img2.src = imgSrcArray \[imgShow-1];
095. imageShow = img2 ;
096. imageHide = img1 ;
097. }
098. hideTime = window .setInterval(function(){hideObject(imageHide,imageShow);},20);
099. }
100. &lt;/ script &gt;
101. &lt; img   class = "imgShow"   id = "img1"   src = "/image/1.jpg" &gt;
102. &lt; img   class = "imgHide"   id = "img2"   src = "/image/2.jpg" &gt;
103. &lt; pre &gt; &lt;/ pre &gt;

```






.imgShow{
	font-size: 12px;
	background-color: #B0C6CE;
	text-align: center;
	vertical-align: middle;
	display: table-caption;
	border-top-width: thin;
	border-right-width: thin;
	border-bottom-width: thin;
	border-left-width: thin;
	border-top-style: solid;
	border-right-style: solid;
	border-bottom-style: solid;
	border-left-style: solid;
	position:absolute;
	width:400px;
	height:300px;
}
.imgHide{
	font-size: 12px;
	background-color: #B0C6CE;
	text-align: center;
	vertical-align: middle;
	display: table-caption;
	border-top-width: thin;
	border-right-width: thin;
	border-bottom-width: thin;
	border-left-width: thin;
	border-top-style: solid;
	border-right-style: solid;
	border-bottom-style: solid;
	border-left-style: solid;
	position:absolute;
	filter:alpha(opacity=0);
	opacity:0.00;
	width:400px;
	height:300px;
}

<script></script>








```