JS效果日记(五)
——封装函数之图片切换
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
* {
margin: 0;
padding: 0;
}
#box {
width: 400px;
height: 400px;
border: 1px solid #666666;
background: url(img/1112.jpg);
}
#box ul li img {
width: 50px;
height: 50px;
}
#box ul li {
display: inline-block;
margin-right: 20px;
margin-top: 350px;
}
</style>
</head>
<body>
<div id="box">
<ul>
<li id="item1">
<img src="img/1112.jpg">
</li>
<li id="item2">
<img src="img/1113.jpg">
</li>
<li id="item3">
<img src="img/1114.jpg">
</li>
<li id="item4">
<img src="img/1115.jpg">
</li>
<li id="item5">
<img src="img/1116.jpg">
</li>
</ul>
</div>
<script type="text/javascript">
function $(id) {
return typeof id === 'string' ? document.getElementById(id) : null;
}
function changebgcImg(liId, imgSrc) {
$(liId).onmouseover = function() {
$('box').style.background = imgSrc;
}
}
changebgcImg('item1', 'url(img/1112.jpg)');
changebgcImg('item2', 'url(img/1113.jpg)');
changebgcImg('item3', 'url(img/1114.jpg)');
changebgcImg('item4', 'url(img/1115.jpg)');
changebgcImg('item5', 'url(img/1116.jpg)');
</script>
</body>
</html>
新手小白
还请大佬多多指教