js简单实现精美轮播图效果

精美轮播效果

效果图展示:

在这里插入图片描述

是不是觉得还是挺好看的。
1.首先建一个文档简单的html文档
2.引入下面的style样式
  body{
            background-color: rgb(119,115, 110);
        }
        ul{
            width: 800px;
            height: 200px;
            position:absolute;
            padding: 0px;
            top:0;
            left:0;
            right:0;
            bottom:0;
            margin: auto;
            list-style: none;
        }
        ul:hover{
            cursor: pointer;
        }
        li{
            position: absolute;
            left: 0;
            transition: 0.4s;
        }

2.粘贴下面的代码到script里面

 window.onload=function(){
        var cur_ul=document.getElementById('banner');
        var arr=new Array();
        var timer=setInterval(get_next,3000);

        for(var i=1;i<=5;i++){
            var cur_li=document.createElement("li");
            var cur_img=document.createElement('img');
            cur_img.src='img/'+ i +'.jpg';
            cur_img.style.width='400px';
            cur_img.style.height='200px';
            cur_li.appendChild(cur_img);
            cur_ul.appendChild(cur_li);
            cur_ul.onmouseenter=function(){
                  clearInterval(timer);
            }
            cur_ul.onmouseleave=function(){
            timer=setInterval(get_next,3000);
           }
           arr.push(cur_li);

        }
        var len=arr.length-1;
        arr[len-2].style.left='0px';
        arr[len-1].style.left='200px';
        arr[len].style.left='400px';
        arr[len-1].style.zIndex=100;
        arr[len-1].style.transform='scale(1.3)';
        function get_next(){
               var give_up=arr[arr.length-1];
               arr.pop();
               arr.unshift(give_up);
               for(var i=0;i<arr.length;i++){
                   arr[i].style.zIndex=i;
                   arr[i].style.transform='scale(1)';
               }
                arr[len-2].style.left='0px';
                arr[len-1].style.left='200px';
                arr[len].style.left='400px';
                arr[len-1].style.zIndex=100;
                arr[len-1].style.transform='scale(1.3)';
        }
        function  get_pre(){
               var give_up=arr[0];
               arr.shift();
               arr.push(give_up);
               for(var i=0;i<arr.length;i++){
                   arr[i].style.zIndex=i;
                   arr[i].style.transform='scale(1)';
               }
                arr[len-2].style.left='0px';
                arr[len-1].style.left='200px';
                arr[len].style.left='400px';
                arr[len-1].style.zIndex=100;
                arr[len-1].style.transform='scale(1.3)';
        }
        //生成左箭头
        var pre_img=document.createElement('img');
        pre_img.src='img/preImg.png';
        pre_img.style.position='absolute';
        pre_img.style.top=0;
        pre_img.style.bottom=0;
        pre_img.style.left=0;
        pre_img.style.margin='auto';
        pre_img.style.zIndex=100;
        cur_ul.appendChild(pre_img);

        var nex_img=document.createElement('img');
        nex_img.src='img/nexImg.png';
        nex_img.style.position='absolute';
        nex_img.style.top=0;
        nex_img.style.bottom=0;
        nex_img.style.right=0;
        nex_img.style.margin='auto';
        nex_img.style.zIndex=100;
        cur_ul.appendChild(nex_img);
        
        //左箭头
        pre_img.onclick=function(){
            get_pre()
        }
        //右箭头
        nex_img.onclick=function(){
                get_next()
            }

    }
3.把图片下载放在与html相同的目录文件夹

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

这张是左箭头图片透明的,也可以通过下面连接获取

https://img-blog.csdnimg.cn/2dd0da8b0a7b4c2ea2c8cd6cf8784187.png
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

这张是右箭头图片透明的,也可以通过下面连接获取

https://img-blog.csdnimg.cn/9dee8683323b49468cf1ce311cbcabaf.png
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值