原生JS写一个淡入淡出轮播图

任何网站主页都离不开一个轮播图,这是滚动播放新闻或者广告或者内容的招牌,类型有很多,我们可以做一个淡入淡出的轮播图。

首先分析,淡入淡出指的是没有滑动效果,一张图片消失的时候另一张图片接着显示出来,仔细分析可以知道在一个轮播框内其实利用绝对定位放了好几张图片,

利用他们的透明度的改变依次出现和消失。

效果图如下:

原生JS写一个淡入淡出轮播图_第1张图片

原生JS写一个淡入淡出轮播图_第2张图片

 

布局如下:

  CSS样式:

 *{
	     	margin: 0;
	     	padding: 0;
	     }
	     li{
	     	list-style: none;
	     }
	     a{
	     	text-decoration: none;
	     }
	     
	     
			#banner{
				width: 800px;
				height: 400px;
				margin:50px auto;
				position: relative;
			}
			
			
			
			ul li img{
				width: 800px;
				height: 400px;
				display: block;
				
			}
			ul li{
				position: absolute;
				opacity: 0;
			}
			
			ul li:first-child{
				opacity: 1;
			}
			
			
			#direction>a{
				position: absolute;
				width: 50px;
				height: 30px;
				display: inline-block;
				background: #000;
				
				color: white;
				text-align: center;
				line-height: 30px;
				font-size: 20px;
				top:185px ;
				
			}
			#direction>a:last-child{
				right: 0;
			}
			
			#btn{
				position: absolute;
				top: 370px;
				left: 45%;
			}
			#btn>a{
				display: inline-block;
				width: 20px;
				height: 20px;
				border-radius:50%;
				margin-left: 10px;
				background: #ccc;
				
				
			}
			#btn>.active{
				background: red;
			}

  JS代码及注释:




                    
                    

你可能感兴趣的:(原生JS写一个淡入淡出轮播图)