利用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'
注意书写