欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js轮播图透明度切换(带上下页和底部圆点切换)

程序员文章站 2023-08-13 18:50:40
效果图: 代码如下:

效果图:

js轮播图透明度切换(带上下页和底部圆点切换)

代码如下:

<!doctype html>
<html>
 <head>
 <meta charset="utf-8">
 <title></title>
 <style type="text/css">
 *{
 margin:0;
 padding:0;
 border:none;
 }
 li{
 list-style: none;
 }
 #box{
 height:340px;
 width:790px;
 position: relative;
 margin:100px auto;

 }
 #box #list1{
 height:340px;
 width:790px;

 }
 #box #list1 li{
 font-size: 80px;
 line-height: 340px;
 text-align: center;
 height:340px;
 width:790px;
 position: absolute;
 left:0;
 top:0;
 opacity: 0;
 filter: alpha(opacity=0);
 }
 #box #list1 li img{
 height:340px;
 width:790px;
 }
 #shang,#xia{
 height:80px;
 width:50px;
 color:#212121;
 background: #ccc;
 font-size: 60px;
 font-weight: bold;
 line-height: 80px;
 text-align: center;
 position: absolute;
 top:130px;
 opacity: 0.8;
 filter: alpha(opacity=80);
 cursor: pointer;

 }
 #shang{
 left:0;
 }
 #xia{
 right:0;
 }
 #box #list2{
 height:20px;
 width:195px;
 position: absolute;
 left:297px;
 bottom:25px;
 opacity: 0.8;
 filter: alpha(opacity=80);

 }
 #box #list2 li{
 height:20px;
 width:20px;
 background: #ccc;
 border-radius: 50%;
 float: left;
 margin-right:5px;
 cursor: pointer;

 }

 #box #list2 li.active{
 background: black;
 }
 </style>

 <script type="text/javascript">
 onload = function(){
 var obox = document.getelementbyid('box');
 var olist1 = document.getelementbyid('list1');
 var ali1 = olist1.getelementsbytagname('li');
 var olist2 = document.getelementbyid('list2');
 var ali2 = olist2.getelementsbytagname('li');
 var oshang = document.getelementbyid('shang');
 var oxia = document.getelementbyid('xia');
 ali1[0].style.opacity = 1;
 ali1[0].style.filter = 'alpha(opacity=100)';
 var size = ali1.length;
 var i = 0;
 var timer = setinterval(function(){
 i ++;
 move();
 },2000);
 function move(){
 if(i >= size){
 i = 0
 }
 if(i < 0){
 i = size-1;
 }
 for(var j = 0; j < ali1.length; j ++){
 if(j == i){
 animate(ali1[j],{opacity:100});
 ali2[j].classname = 'active';
 }else{
 animate(ali1[j],{opacity:0});
 ali2[j].classname = '';
 }
 }

 }
 //前一张
 oshang.onclick = function(e){
 var evt = e || event;
 evt.preventdefault();
 i --;
 move();
 }
 //后一张
 oxia.onclick = function(e){
 var evt = e || event;
 evt.preventdefault();
 i ++;
 move();
 }
 //下面的圆点
 for(var k = 0;k < ali2.length; k ++){
 ali2[k].index = k;
 ali2[k].onmouseenter = function(){
 i = this.index;
 move();
 }
 }
 obox.onmouseenter = function(){
 clearinterval(timer);
 }
 obox.onmouseleave = function(){
 timer = setinterval(function(){
 i ++;
 move();
 },2000);
 }
 /*************************缓冲运动 可封装留着以后备用^_^*************************/

 function getstyleattr(obj, attr){
 if (window.getcomputedstyle){
 return getcomputedstyle(obj, null)[attr]; 
 }
 else {
 return obj.currentstyle[attr]; 
 }
 }
 function animate(obj, json, fn){
 clearinterval(obj.timer); 
 obj.timer = setinterval(function(){
 var bstop = true; 
 for (var attr in json){
 var itarget = json[attr]; 
 var current;
 if (attr == "opacity"){ 
 current = parsefloat(getstyleattr(obj, attr))*100;
 current = math.round(current);
 }
 else { 
 current = parsefloat(getstyleattr(obj, attr));
 current = math.round(current);
 }
 var speed = (itarget-current)/8; (400-393)/8
 speed = speed>0 ? math.ceil(speed) : math.floor(speed);
 if (current != itarget){
 bstop = false; 
 }
 if (attr == "opacity"){ 
 obj.style[attr] = (current+speed)/100;
 obj.style.filter = "alpha(opacity=" + (current+speed) + ")";
 }
 else { 
 obj.style[attr] = current+speed + "px";
 }
 }
 if (bstop){
 console.log("停止运动");
 clearinterval(obj.timer); 
 if (fn) {
 fn(); 
 }
 }
 }, 30);
 }
 }
 </script>
 </head>
 <body>
 <div id="box">
 <ul id="list1">
 <li style="background: red;">1</li>
 <li style="background: yellow;">2</li>
 <li style="background: green;">3</li>
 <li style="background: blue;">4</li>
 <li style="background: blueviolet;">5</li>
 <li style="background: brown;">6</li>
 <li style="background: orangered;">7</li>
 <li style="background: palevioletred;">8</li> 
 </ul>
 <ul id="list2">
 <li class="active"></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li></li>
 <li style="margin-right:0px;"></li>
 </ul>
 <div id="shang">
 <
 </div>
 <div id="xia">
 >
 </div>
 </div>
 </body>
</html>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!