ITeye博客

JavaScript控制多张图片循环播放(淡入淡出效果)

dhj · 2008-09-27 14:18 · web页面技术 · Markdown

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

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

    2.建立测试HTML页面,代码如下:

view plain copy to clipboard print ?

  1. < style >
  2. .imgShow{
  3. font-size: 12px;
  4. background-color: #B0C6CE;
  5. text-align: center;
  6. vertical-align: middle;
  7. display: table-caption;
  8. border-top-width: thin;
  9. border-right-width: thin;
  10. border-bottom-width: thin;
  11. border-left-width: thin;
  12. border-top-style: solid;
  13. border-right-style: solid;
  14. border-bottom-style: solid;
  15. border-left-style: solid;
  16. position:absolute;
  17. width:400px;
  18. height:300px;
  19. }
  20. .imgHide{
  21. font-size: 12px;
  22. background-color: #B0C6CE;
  23. text-align: center;
  24. vertical-align: middle;
  25. display: table-caption;
  26. border-top-width: thin;
  27. border-right-width: thin;
  28. border-bottom-width: thin;
  29. border-left-width: thin;
  30. border-top-style: solid;
  31. border-right-style: solid;
  32. border-bottom-style: solid;
  33. border-left-style: solid;
  34. position:absolute;
  35. filter:alpha(opacity = 0 );
  36. opacity:0.00;
  37. width:400px;
  38. height:300px;
  39. }
  40. </ style >
  41. < script >
  42. var hideTime = null ;
  43. var hide = 100 ;//过滤镜初始值,IE
  44. var hideFF = 1 .0;//过滤镜初始值,FF
  45. var show = 0 ;
  46. var showFF = 0 .00;
  47. var imgShow = 1 ;
  48. 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");
  49. function hideObject(hideImage,showImage){
  50. try{
  51. hide hide =hide-1;
  52. hideFF hideFF =hideFF-0.01;
  53. show show =show+1;
  54. showFF showFF =showFF+0.01;
  55. hideImage.style.filter = "alpha(opacity=" +hide+")";
  56. hideImage.style.opacity = hideFF +"";/*FF兼容*/
  57. //hideImage.style.-moz-opacity = hideFF +"";
  58. showImage.style.filter = "alpha(opacity=" +show+")";
  59. showImage.style.opacity = showFF +"";/*FF兼容*/
  60. if(hide< =0){
  61. showImage.style.filter = "alpha(opacity=100)" ;
  62. showImage.style.opacity = 1 .0;/*FF兼容*/
  63. window.clearInterval(hideTime);
  64. hideImage.style.display = "none" ;
  65. window.setTimeout(changeImage,1000);
  66. }
  67. }catch(e){
  68. window.clearInterval(hideTime);
  69. }
  70. }
  71. function changeImage(){
  72. hide = 100 ;
  73. hideFF = 1 .0;
  74. show = 0 ;
  75. showFF = 0 .0;
  76. imgShow++;
  77. if(imgShow> imgSrcArray.length){
  78. imgShow = 1 ;
  79. }
  80. var img1 = document .getElementById("img1");
  81. var img2 = document .getElementById("img2");
  82. var imageShow,imageHide;
  83. if(img1.style.display =="none"){
  84. img1.className = "imgHide" ;
  85. img2.className = "imgShow" ;
  86. img1.style.display = "" ;
  87. img1.src = imgSrcArray [imgShow-1];
  88. imageShow = img1 ;
  89. imageHide = img2 ;
  90. }else{
  91. img2.className = "imgHide" ;
  92. img1.className = "imgShow" ;
  93. img2.style.display = "" ;
  94. img2.src = imgSrcArray [imgShow-1];
  95. imageShow = img2 ;
  96. imageHide = img1 ;
  97. }
  98. hideTime = window .setInterval(function(){hideObject(imageHide,imageShow);},20);
  99. }
  100. </ script >
  101. < img   class = "imgShow"   id = "img1"   src = "/image/1.jpg" >
  102. < img   class = "imgHide"   id = "img2"   src = "/image/2.jpg" >
  103. < pre > </ pre >






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








评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。