js实现轮播

这篇博客展示了如何使用JavaScript从头实现一个图片轮播功能,包括自动滚动、鼠标悬停暂停、点击箭头切换图片等效果。通过HTML、CSS和JavaScript代码的结合,创建了一个简单的轮播图组件,适用于网页中展示多个图片的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考博文:

https://blog.youkuaiyun.com/lgx1134569285/article/details/80511795

在原博文基础上进行了一些修改,使其实现其功能

实现效果:
1.自动滚动播放
2.鼠标移上去停止播放

3.点击左右箭头滚动播放

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>轮播图</title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        .box{
            width: 500px;
            height: 300px;
            border: 1px solid #ccc;
            margin: 100px auto;
            padding: 5px;
        }
        .inner{
            width: 500px;
            height: 300px;
            position: relative;
            overflow: hidden;
        }
        .inner img{
            width: 500px;
            height: 300px;
            vertical-align: top;
        }
        ul{
            width: 1000%;
            position: absolute;
            list-style: none;
            left: 0;
            top: 0;
        }
        .inner li{
            float: left;
        }
        ol{
            position: absolute;
            height: 20px;
            right: 20px;
            bottom: 20px;
            text-align: center;
            padding: 5px;
        }
        ol li{
            display: inline-block;
            width: 20px;
            height: 20px;
            line-height: 20px;
            background-color: #fff;
            margin:5px;
            cursor: pointer;
        }
        ol .current{
            background-color: red;
        }
        #arr{
            display: none;
        }
        #arr span{
            width: 40px;
            height: 40px;
            position: absolute;
            left: 5px;
            top: 50%;
            margin-top: -20px;
            background: #fff;
            cursor: pointer;
            line-height: 40px;
            text-align: center;
            font-weight: bold;
            font-family: '黑体';
            font-size: 30px;
            color: #000;
            opacity: 0.5;
            border: 1px solid #fff;
        }
        #arr #right{
            right: 5px;
            left: auto;
        }
    </style>
        <body>
    <div id="box" class="box">
        <div class="inner">
            <ul>
                <li><a href="#"><img src="img/001.png" alt=""></a></li>
                <li><a href="#"><img src="img/002.jpg" alt=""></a></li>
                <li><a href="#"><img src="img/003.jpg" alt=""></a></li>
                <li><a href="#"><img src="img/004.jpg" alt=""></a></li>
                <li><a href="#"><img src="img/005.jpg" alt=""></a></li>
            </ul>
            <ol class="bar">
                
            </ol>
            <div id="arr">
                <span id="left"> < </span>
                <span id="right"> > </span>
            </div>
        </div>
    </div>

    <script>
        function myx(id){
            return document.getElementById(id);
        }

        var box=myx("box");
        //alert(document.getElementById("box"));
        var inner=box.children[0];
        var ulObj=inner.children[0];
        var list=ulObj.children;
        var olObj=inner.children[1];
        var arr=myx("arr");
        var imgWidth=inner.offsetWidth;
        var right=myx("right");
        var pic=0;

        for(var i=0;i<list.length;i++){
            var liObj=document.createElement("li");

            olObj.appendChild(liObj);
            liObj.innerText=(i+1);
            liObj.setAttribute("index",i);

            liObj.onmouseover=function(){
                for(var j=0;j<olObj.children.length;j++){
                    olObj.children[j].removeAttribute("class");
                }
                this.className="current";
                pic=this.getAttribute("index");
                animate(ulObj,-pic*imgWidth);
            }
        }

        olObj.children[0].className="current";
        ulObj.appendChild(ulObj.children[0].cloneNode(true));

        var timeId=setInterval(onmouseclickHandle,1000);

        box.onmouseover=function(){
            arr.style.display="block";
            clearInterval(timeId);
        };
        box.onmouseout=function(){
            arr.style.display="none";
            timeId=setInterval(onmouseclickHandle,1000);
        };

        right.onclick=onmouseclickHandle;
        function onmouseclickHandle(){
                if(pic==list.length-1){
                    olObj.children[olObj.children.length-1].className="";
                    olObj.children[0].className="current";
                    animate(ulObj,-pic*imgWidth);
                    pic=0;
                }else if(pic<list.length-1){
                    for(var i=0;i<olObj.children.length;i++){
                        olObj.children[i].removeAttribute("class");
                    }
                    olObj.children[pic].className="current";
                    animate(ulObj,-pic*imgWidth);
                    pic++;
                }
        }
        left.onclick=function(){
            if(pic==0){
                pic=list.length-1;
                ulObj.style.left=-pic*imgWidth+"px";
            }
            pic--;
            animate(ulObj,-pic*imgWidth);
            for(var i=0;i<olObj.children.length;i++){
                olObj.children[i].removeAttribute("class");
            }

            olObj.children[pic].className="current";
        };

        function animate(element,target){
            clearInterval(element.timeId);
            element.timeId=setInterval(function(){
                var current=element.offsetLeft;
                var step=10;
                if(target=="-0"){
                    current="0";
                }
                step=current < target ? step : -step;
                current+=step;
                if(Math.abs(current-target)>Math.abs(step)){
                    element.style.left=current+"px";
                }else{
                    clearInterval(element.timeId);
                    element.style.left=target+"px";
                }
            },10);
        }
    </script>
</body>

</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值