JS循环精灵图背景-遍历背景图片

利用for循环设置一组元素的精灵图背景

如果用css做,需要每个小盒子都设置一遍,才能得到相应的图标

分析:

1、首先精灵图图片排列是有规律的

2、核心:利用for循环,修改背景位置

精灵图来源:卷皮官网

分析:先用盒子将内容进行排版

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .dabox {
            width: 320px;
            height: 400px;
            margin: 0 auto;
            margin-top: 10px;
            border: 1px solid black
        }
        
        .box {
            width: 300px;
            height: 350px;
            background-color: pink;
            margin-top: 10px;
            margin-left: 10px;
        }
        
        .tu {
            width: 24px;
            height: 24px;
            display: inline-block;
            margin-left: 20px;
            background-image: url(ct_icons.png);
            vertical-align: text-top;
        }
        
        dd {
            background-color: white;
            width: 150px;
            height: 50px;
            margin-left: 0px;
            float: left;
            position: relative;
            text-align: center;
            line-height: 50px;
            border-bottom: 1px solid grey;
        }
        
        a {
            font-size: 15px;
            color: black;
            text-decoration: none;
        }
        
        p {
            position: absolute;
            left: 145px;
            top: -10px;
            display: inline-block;
            text-align: center;
            line-height: 30px;
            color: grey;
        }
    </style>
</head>

<body>
    <div class="dabox">
        <div class='box'>
            <dl>
                <dd>
                    <a href="#">
                        <div class="tu"></div> 女装
                        <p>|</p>
                    </a>
                </dd>
                <dd>
                    <a href="#">
                        <div class="tu"></div> 男装

                    </a>
                </dd>
                <dd>
                    <a href="#">
                        <div class="tu"></div> 鞋子
                        <p>|</p>
                    </a>
                </dd>
                <dd>
                    <a href="#">
                        <div class="tu"></div> 箱包

                    </a>
                </dd>
                <dd>
                    <a href="#">
                        <div class="tu"></div> 母婴
                        <p>|</p>
                    </a>
                </dd>
                <dd>
                    <a href="#">
                        <div class="tu"></div> 美妆

                    </a>
                </dd>
                <dd>
                    <a href="#">
                        <div class="tu"></div> 居家
                        <p>|</p>
                    </a>
                </dd>
                <dd>
                    <a href="#">
                        <div class="tu"></div> 家纺

                    </a>
                </dd>
                <dd>
                    <a href="#">
                        <div class="tu"></div> 文体
                        <p>|</p>
                    </a>
                </dd>
                <dd>
                    <a href="#">
                        <div class="tu"></div> 美食

                    </a>
                </dd>
                <dd>
                    <a href="#">
                        <div class="tu"></div> 数码
                        <p>|</p>
                    </a>
                </dd>
                <dd>
                    <a href="#">
                        <div class="tu"></div> 电器

                    </a>
                </dd>
                <dd>
                    <a href="#">
                        <div class="tu"></div> 内衣
                        <p>|</p>
                    </a>
                </dd>
                <dd>
                    <a href="#">
                        <div class="tu"></div> 配饰

                    </a>
                </dd>
            </dl>
        </div>
    </div>
    <script>
        var tu = document.querySelectorAll('.tu')

        for (var i = 0; i < tu.length; i++) {
            var index = i * 24
            tu[i].style.backgroundPosition = '-' + index + 'px 0px'


        }
    </script>
</body>

</html>

 添加鼠标经过时,图片变成红色:

 

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

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .dabox {
            width: 320px;
            height: 400px;
            margin: 0 auto;
            margin-top: 10px;
            border: 1px solid black
        }
        
        .box {
            width: 300px;
            height: 350px;
            background-color: pink;
            margin-top: 10px;
            margin-left: 10px;
        }
        
        .tu {
            width: 24px;
            height: 24px;
            display: inline-block;
            margin-left: 20px;
            background-image: url(ct_icons.png);
            vertical-align: text-top;
            background-position-y: 0px;
        }
        
        dd {
            background-color: white;
            width: 150px;
            height: 50px;
            margin-left: 0px;
            float: left;
            position: relative;
            text-align: center;
            line-height: 50px;
            border-bottom: 1px solid grey;
        }
        
        a {
            font-size: 15px;
            color: black;
            text-decoration: none;
        }
        
        p {
            position: absolute;
            left: 145px;
            top: -10px;
            display: inline-block;
            text-align: center;
            line-height: 30px;
            color: grey;
        }
        
        a:hover .tu {
            background-position-y: -24px;
        }
        a:hover {
            color: red
        }
    </style>
</head>

<body>
    <div class="dabox">
        <div class='box'>
            <dl>
                <dd>
                    <a href="#">
                        <div class="tu"></div> 女装
                        <p>|</p>
                    </a>
                </dd>
                <dd>
                    <a href="#">
                        <div class="tu"></div> 男装

                    </a>
                </dd>
                <dd>
                    <a href="#">
                        <div class="tu"></div> 鞋子
                        <p>|</p>
                    </a>
                </dd>
                <dd>
                    <a href="#">
                        <div class="tu"></div> 箱包

                    </a>
                </dd>
                <dd>
                    <a href="#">
                        <div class="tu"></div> 母婴
                        <p>|</p>
                    </a>
                </dd>
                <dd>
                    <a href="#">
                        <div class="tu"></div> 美妆

                    </a>
                </dd>
                <dd>
                    <a href="#">
                        <div class="tu"></div> 居家
                        <p>|</p>
                    </a>
                </dd>
                <dd>
                    <a href="#">
                        <div class="tu"></div> 家纺

                    </a>
                </dd>
                <dd>
                    <a href="#">
                        <div class="tu"></div> 文体
                        <p>|</p>
                    </a>
                </dd>
                <dd>
                    <a href="#">
                        <div class="tu"></div> 美食

                    </a>
                </dd>
                <dd>
                    <a href="#">
                        <div class="tu"></div> 数码
                        <p>|</p>
                    </a>
                </dd>
                <dd>
                    <a href="#">
                        <div class="tu"></div> 电器

                    </a>
                </dd>
                <dd>
                    <a href="#">
                        <div class="tu"></div> 内衣
                        <p>|</p>
                    </a>
                </dd>
                <dd>
                    <a href="#">
                        <div class="tu"></div> 配饰

                    </a>
                </dd>
            </dl>
        </div>
    </div>
    <script>
        var tu = document.querySelectorAll('.tu')

        for (var i = 0; i < tu.length; i++) {
            var index = i * 24
            tu[i].style.backgroundPositionX = '-' + index + 'px'


        }
    </script>
</body>

</html>

 主要修改的是背景图的x,y坐标,注意精灵图的使用,并且再script中驼峰命名法的书写方式

注意图片和文字的对齐方式: vertical-align: text-top;

            var index = i * 24
            tu[i].style.backgroundPositionX = '-' + index + 'px'

注意书写

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一夕ξ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值