动态切换图片

css/index.css

* {
    margin: 0px;padding: 0px;
}

body {
    width: 632px;
    /*background-color: blue;*/
    margin: 0 auto;
}

#imgsCom {
    background-color: yellow;
    /*相对定位,为了下层可以使用绝对定位时以本div的原点为原点*/
    position: relative;
}

#ulnav{
    list-style-type: none;
   
    position: absolute;
    /*使用以imgsCom为原点来绝对定位到右下角*/
    bottom: 0px;
    right: 0px;
}
#ulnav li{
    list-style-type: none;
    float: left;
    background-color: black;
    color: white;
    margin-right: 5px;
    width: 20px;
    height: 20px;
    line-height: 20px;
    text-align: center;
    cursor: pointer;
}

index.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>js,css动态切换图片</title>
    <link href="css/index.css" rel="stylesheet" />
    <script type="text/javascript">

        function gel(id) {
            return document.getElementById(id);
        }

        function  clearLiBg() {
            var mylis = gel("ulnav").childNodes;
            for (var i = 0; i < mylis.length; i++) {
                if (mylis[i].nodeType == 1) {
                    mylis[i].style.backgroundColor = "black";
                }
                
            }
        }
      
        window.onload = function() {
            //给三个li都指定一个属性
            var lis = gel("ulnav").childNodes;
            for (var i = 0; i < lis.length; i++) {
                if (lis[i].nodeType == 1) {
                    lis[i].onclick = function () {
                        //更换图片
                        gel("myimg").setAttribute("src", "images/" + this.innerHTML + ".png");
                        //所有LI颜色复原
                        clearLiBg();
                        //更换LI背景标签颜色
                        this.style.backgroundColor = "silver";
                    };
                }
            }
        };
    </script>
</head>
<body>
    <div id="imgsCom" style="width: 632px; height: 412px;">
        <img src="images/1.png" id="myimg" style="width: 632px; height: 412px; " />
        <ul id="ulnav">
            <li>1</li>
            <li>2</li>
            <li>3</li>
        </ul>
    </div>
</body>
</html>

图片素材



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值