js制作轮播图 css html js

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            list-style: none;
            text-decoration: none;
        }
        #banner{
            width: 800px;
            height: 400px;
            border: 2px solid #999;
            overflow: hidden;
            position: relative;  
        }
        #imglist{
            width: 4000px;
            height: 400px;

        }
        #imglist img{
            width: 800px;
            height: 400px; 
        }
        #imglist li{
            float: left;  
        }
        .prev{
            background-color: #666;
            width: 30px;
            height: 40px;
            color: #fff;
            text-align: center;
            line-height: 40px;
            position: absolute;
            left: 5px;
            top: 45%; 
            cursor: pointer;
        }  
        .next{
            background-color: #666;
            width: 30px;
            height: 40px;
            color: #fff;
            text-align: center;
            line-height: 40px;
            position: absolute;
            right: 5px;
            top: 45%; 
            cursor: pointer;
        } 
        #icolist{
            position: absolute;
            right: 10px;
            bottom: 10px;
        }
        #icolist li{
            width: 30px;
            height: 30px;
            border-radius: 50%;
            background-color: #666;
            text-align: center;
            line-height: 30px;
            color: #fff;
            float: left;
            margin-left: 5px;
        } 
    </style>
</head>
<body>
    <div id="banner">
        <ul id="imglist">
            <li><img src="./imgs/tu1.webp" alt=""></li>
            <li><img src="./imgs/tu2.webp" alt=""></li>
            <li><img src="./imgs/tu3.webp" alt=""></li>
            <li><img src="./imgs/tu4.webp" alt=""></li>
            <li><img src="./imgs/tu1.webp" alt=""></li>
        </ul>
        <ul id="icolist">
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
        </ul>
         <div class="prev"> 》</div>
         <div class="next">《</div>
    </div>
    <script> 
    var eprve = document.querySelector(".prev");
    var enext = document.querySelector(".next");
    var esico = document.getElementById("icolist").getElementsByTagName("li");
    var eicolist = document.querySelector("#icolist");
    var eimglist = document.querySelector("#imglist");
        var left = 0;
         var timer;
        run(); 
        function run(){
            if (left <= -3000) {
                left=0; 
            }
          var m = Math.floor(-left / 800);
          imglist.style.marginLeft = left + "px";
          var n = (left % 800 ==0) ? n=1200 : n = 10;
          left-= 10;
          timer = setTimeout(run,n);
        icochange(m);
        } 
        //做一个图片定位函数
        function imgchange(n) {
            let lt = -(n * 800);
            imglist.style.marginLeft = lt   + "px";
            left = lt;
        }
        eprve.onclick = function(){
         //试着定位第一张
         let prevgo = Math.floor(-left /800) - 1;
         if (prevgo == -1) {
            prevgo = 3;
         }
         imgchange(prevgo);
        }
        enext.onclick = function(){
         //定位做后一张
         let nextgo = Math.floor(-left /800) + 1;
         if (nextgo == 4) {
            nextgo = 0;
         } 
         imgchange(nextgo);
        } 
        function icochange(m) {
            for (let index = 0; index < esico.length; index++) {
                esico[index].style.backgroundColor = "";
                if (m <esico.length) {
                   esico[m].style.backgroundColor = "red"; 
                }
                
            }
        } 
        eicolist.onclick = function(){
            var tg = event.target;
            let ico = tg.innerHTML -1;
            imgchange(ico);
            icochange(ico);
        }
        eimglist.onmouseover = function(){
            clearTimeout(timer);
        }
        eimglist.onmouseout = function(){
            run();
        }
    </script>
</body> 
</html>

 拿走别客气 图片换一下就行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值