web前端 用js实现轮播图效果

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style>
            * {
                margin: 0;
                padding: 0;
            }

            ul img {
                width: 550px;
                height: 350px;
                display: none;

            }

            ul {
                list-style: none;
            }

            ol {
                width: 150px;
                height: 20px;
                background-color: rgba(0, 0, 0, .6);
                border-radius: 10px;
                position: absolute;
                left: 200px;
                bottom: 10px;
                list-style: none;
            }

            ol>li {
                width: 13px;
                height: 13px;
                border-radius: 50%;
                background-color: rgba(255, 255, 255, 0.5);
                float: left;
                margin-top: 3px;
                margin-left: 14px;
            }

            #container {
                width: 550px;
                height: 350px;
                position: relative;
            }

            #arr_left,#arr_right {
                width: 50px;
                height: 50px;
                font-size: 50px;
                font-weight: bold;
                position: absolute;
                top: 45%;
                background-color: rgba(255, 255, 255, 0.6);
                color: black;
                text-align: center;
                line-height: 50px;
                border-radius: 50%;
                display: none;
            }
            
            #arr_right{
                right: 0px;
            }
            
        </style>
    </head>
    <body>
        <div id="container">
            <ul>
                <li><img style="display: block;" src="https://tse1-mm.cn.bing.net/th/id/OIP-C.My4zZWM57V4-6ocv3xnmGAHaEK?w=301&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7" alt=""></li>
                <li><img src="https://tse3-mm.cn.bing.net/th/id/OIP-C.UvMnxGQs885rpCslxF6VTAHaEK?w=287&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7" alt=""></li>
                <li><img src="https://tse4-mm.cn.bing.net/th/id/OIP-C.tFhdMxajYZ13cIZJ_MZ8RAHaEK?w=299&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7" alt=""></li>
                <li><img src="https://tse2-mm.cn.bing.net/th/id/OIP-C.eWcZN25l4l5IE-COLLmH1QHaDq?w=350&h=173&c=7&r=0&o=5&dpr=1.5&pid=1.7" alt=""></li>
                <li><img src="https://tse2-mm.cn.bing.net/th/id/OIP-C.Go47HuDLJYKU7RlnXKU4EgHaEo?w=273&h=180&c=7&r=0&o=5&dpr=1.5&pid=1.7" alt=""></li>
            </ul>
            <ol>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
            </ol>
            <div id="arr_left">&lt;</div>
            <div id="arr_right">&gt;</div>
            
        </div>


        <script>
            let arrow_left = document.getElementById("arr_left")
            let arrow_right = document.getElementById("arr_right")
            let grey_color = "rgba(255,255,255,0.5)"
            let current_index = 1
            let img_arr = document.querySelectorAll('ul img')
            let div = document.querySelector('#container')
            let time_id = setInterval(lunbo, 1000)
            let cirle_arr = document.querySelectorAll("ol > li")

            for (let i = 0; i < cirle_arr.length; i++) {
                
                cirle_arr[i].index = i
                cirle_arr[i].onclick = function(event) {
                    
                    this.style.backgroundColor = "#fff"
                    
                    cirle_arr[current_index].style.backgroundColor = grey_color
                    
                    img_arr[this.index].style.display = "block"
                
                    img_arr[current_index].style.display = "none"
                
                    current_index = this.index
                }
            }

            function lunbo() {
                current_index++
                if (current_index >= img_arr.length) {
                    current_index = 0
                }
                for (let i = 0; i < img_arr.length; i++) {
                    if (current_index == i) {
                        img_arr[i].style.display = "block"
                        cirle_arr[i].style.backgroundColor = "#fff"
                    } else {
                        img_arr[i].style.display = "none"
                        cirle_arr[i].style.backgroundColor = grey_color
                    }
                }
            }

            div.onmousemove = function() {
                clearInterval(time_id)
                arrow_left.style.display = "block"
                arrow_right.style.display = "block"
            }
            div.onmouseout = function() {
                time_id = setInterval(lunbo, 1000)
                arrow_left.style.display = "none"
                arrow_right.style.display = "none"
            }
            arrow_left.onclick = function(){
                cirle_arr[current_index].style.backgroundColor = grey_color
                img_arr[current_index].style.display = "none"
                if(current_index >= 1){
                    current_index--
                }
                else{
                    current_index = 4
                }
                cirle_arr[current_index].style.backgroundColor = "white"
                img_arr[current_index].style.display = "block"
            }
            arrow_right.onclick = function(){
                cirle_arr[current_index].style.backgroundColor = grey_color
                img_arr[current_index].style.display = "none"
                if(current_index < 4){
                    current_index++
                }
                else{
                    current_index = 0
                }
                cirle_arr[current_index].style.backgroundColor = "white"
                img_arr[current_index].style.display = "block"
            }
        </script>
    </body>
</html>

  • 17
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值