JS计算数组之和

将数组的所有值相加, 代码如下

<script>
    var nums = [1,2,3,4,5];
    //计算数组的总和
    var sum = nums.reduce(function(prev,cur,index,array){
        return prev + cur
    })
    alert(sum);   //15
</script>

CSS制作固定于页面底部的bar

代码如下

<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <meta charset="utf-8" />
    <style>
        body {
            padding-bottom: 50px;
        }

        .footer {
            position: fixed;
            left: 0px;
            bottom: 0px;
            width: 100%;
            height: 50px;
            background-color: #eee;
            z-index: 9999;
        }
    </style>
</head>

<body>
    内容

    <div class="footer">固定在底部</div>
</body>

</html>

原生JS的轮播图

代码如下

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>轮播图</title>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }

        .slide span {
            width: 20px;
            height: 20px;
            background: rgba(0, 0, 0, .6);
            border-radius: 50%;
        }

        .slide a {
            text-decoration: none;
            color: #fff;
        }

        .slide p {
            width: 200px;
            text-align: center;
            line-height: 20px;
            position: absolute;
            top: 260px;
            left: 100px;
            display: flex;
            justify-content: space-around;
        }

        .slide img {
            width: 100%;
        }
    </style>
    <script type="text/javascript">
        let total = 3 //设置轮播的图片数量
        window.onload = function () {
            var aNode = document.getElementsByClassName("point");
            var divNode = document.getElementsByClassName("slide")[0];
            var imgNode = document.createElement("img");
            for (var position in aNode) {
                aNode[position].onclick = function () {
                    var index = this.innerText;
                    carousel(index);
                }
            }
            function carousel(index) {
                imgNode.src = 'static/image/' + index + '.jpg';
                divNode.appendChild(imgNode);
            }
            var i = 1;
            imgNode.src = 'static/image/' + i + '.jpg';
            function change() {
                imgNode.src = 'static/image/' + i + '.jpg';
                i++;
                if (i == total + 1) {
                    i = 1;
                }
            }
            divNode.appendChild(imgNode);
            setInterval(change, 1500);
        }
    </script>
</head>

<body>
    <div class="slide">
        <p>
            <span><a href="#" onclick="carousel(1)" class="point">1</a></span>
            <span><a href="#" onclick="carousel(2)" class="point">2</a></span>
            <span><a href="#" onclick="carousel(3)" class="point">3</a></span>
        </p>
    </div>
</body>

</html>

« Previous entries