美文网首页
原生js实现轮播图

原生js实现轮播图

作者: kalrase | 来源:发表于2018-11-15 21:09 被阅读0次

今天练习一下DOM操作,顺便熟悉熟悉函数,做了一个轮播图,经过测试,效果还不错!
直接放上源码,供大家参考!

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        *{
            padding:0;
            margin: 0;
        }
        div{
            position: relative;
            width: 800px;
            height: 400px;
            margin: 100px auto 0 auto;
        }
        #view{
            width: 800px;
            height: 400px;
            margin: 0 auto;
            border: 1px solid #000;

        }
        img{
            width: 800px;
            display: none;

        }
        #dote{
            position: absolute;
            bottom:40px;
            left:350px;
            
        }
        #dote li{
            list-style: none;
            margin: 0 5px;

            width: 15px;
            height: 15px;
            border-radius: 50%;
            background-color: #dedede;
            float: left;
        }
        #left{
            position: absolute;
            background-color: #333333;
            color: white;
            font-size: 60px;
            top:180px;
            left:10px;
            padding: 10px 10px;
            opacity: 0.5;
            border: none;

        }
        #right{
            position: absolute;
            background-color: #333333;
            color: white;
            font-size: 60px;
            top:180px;
            right:10px;
            padding: 10px 10px;
            opacity: 0.5;
            border: none;
        }
        #right:hover{
            background-color: #999999;
        }
        #left:hover{
            background-color: #999999;
        }
    
    </style>
</head>
<body>
   <div>
        <main id="view">
                <img src="0.jpg" alt="#">
                <img src="1.jpg" alt="#">
                <img src="2.jpg" alt="#">
                <img src="3.jpg" alt="#">
                <img src="4.jpg" alt="#">
                <img src="5.jpg" alt="#">
            </main>
            <ul id="dote">
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
            </ul>
            <button id="left" onclick="prev()">&lt;</button>
            <button id="right" onclick="nex()">&gt;</button>
   </div>
</body>
<script>
    var view=document.getElementById('view');
    var dote=document.getElementById('dote');    
    var x=0;    
    view.children[x].style.display='block';
    dote.children[x].style.backgroundColor='green';

   function prev(){
       view.children[x].style.display='none';
       dote.children[x].style.backgroundColor='#dedede';       
       x==0?x=dote.children.length :null;
        x--;
       view.children[x].style.display='block'
       dote.children[x].style.backgroundColor='green';
    }
    function nex(){
       view.children[x].style.display='none';
       dote.children[x].style.backgroundColor='#dedede';    
        x==dote.children.length-1?x=-1:null;                                 
        x++;
       view.children[x].style.display='block';
       dote.children[x].style.backgroundColor='green';
    }

    //tab 切换

    for(var j=0;j<dote.children.length;j++){
        dote.children[j].index=j                //为每个圆点添加索引值,以便获取调用
        dote.children[j].onclick=function (){
            view.children[x].style.display='none';
            dote.children[x].style.backgroundColor='#dedede';         
            x=this.index;                       //当前圆点索引值付给x
            view.children[x].style.display='block';       
            this.style.backgroundColor='green'; 
        }
    }
    setInterval(nex,1000)
</script>
</html>

相关文章

  • 用js原生实现轮播图

    用jquery实现轮播图非常简单的啦!有没有想过用原生js实现轮播图呢???今天琢磨了一下,摸索出来一个,就和大家...

  • JavaScript | 365笔记第87天 | 原生JS做轮播

    用原生JS做一个轮播图

  • 原生Js实现轮播图

    页面结构 原理(通过图片位置的变换来实现轮播,container位置有限,一次只能显示一张,container的宽...

  • 原生JS实现轮播图

    实习刚结束,由于实习期间一直用的React框架,原生js都有些生疏了,所以用原生js写了个简单的轮播图练练手。 方...

  • 原生js实现轮播图

    今天练习一下DOM操作,顺便熟悉熟悉函数,做了一个轮播图,经过测试,效果还不错!直接放上源码,供大家参考!

  • 原生JS实现轮播图

  • 原生js制作轮播图

    原生js 制作的轮播图 今天学习了一个原生js轮播图的方法。效果图如下 通过点击上下页和中间的点进行翻页,通过改变...

  • 轮播图(2)——基于JQ的左右滑动轮播

    上回书我们说到原生js淡入淡出效果的轮播图,这回我们说说左右滑动轮播图,由于需要缓动动画效果,原生js需要封装缓动...

  • 小记setTimeout

    前言:从《原生JS实现轮播(上)》中JS实现渐变效果,引出的setTimeout用法问题。 对于setInterv...

  • js原生—— 无缝轮播

    今天分享前端开发学习中的一个很经典的案例——原生JS实现无缝轮播图。 需求: 1.鼠标移入轮播图时左右两边显示上一...

网友评论

      本文标题:原生js实现轮播图

      本文链接:https://www.haomeiwen.com/subject/pkyufqtx.html