js uses setInterval timer method to develop native carousel chart

Keywords: Javascript PHP

In the past, when I went for an interview, the interviewer asked me to write a rotation chart directly. It was just coming out, so js is not powerful, so I can't write directly. If I want to get a high salary js, I still need to be able to learn. Now I use setInterval timer to develop a native rotation chart;

The code is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style type="text/css">
        *{margin:0;padding: 0;}
        #main{width:490px;margin:100px auto;}
        #main img{width:100%;height:300px}
        #main li{list-style: none;float: left;width:47px;border:1px solid orange;text-align: center;
            padding:6px;
        }
        .orange{background-color: orange}
    </style>
</head>
<body>
<div id="main">
    <img src="img/1.jpg">
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
        <li>6</li>
        <li>7</li>
        <li>8</li>
    </ul>
</div>
<script type="text/javascript">
    var oDiv = document.getElementsByTagName("img")[0];
    var oLi = document.getElementsByTagName("li");
    var count =1;
    var timer = setInterval(function () {
        oDiv.src = "img/"+count+".jpg";
        for ( var i=0;i<oLi.length;i++ ){
            oLi[i].className = '';
        }
        oLi[count-1].className = "orange";
        count++;
        if (count>8){
            count=1;
        }
    },1000)
</script>
</body>
</html>

Example:

Group leader personal blog: http://www.dongdang.xin/

 

Recommended articles: php cloud communication, send SMS, WeChat H5 payment

Posted by scottrad on Wed, 01 Jan 2020 00:27:20 -0800