简单图片的切换+高亮设置

实现效果:

实现代码:

<!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>
        .small-img{
            height: 100px;
        }

    </style>

</head>

<body>
    <div>
        <img class="big-img" width="300px" height="200px" src="01.jpg" alt="">
    </div>
    <div>
        <img class="samll-img" onclick="clickImg('01.jpg')" width="100px" height="60px" src="01.jpg" alt="">
        <img class="samll-img" onclick="clickImg('02.jpg')" width="100px" height="60px" src="02.jpg" alt="">
        <img class="samll-img" onclick="clickImg('03.jpg')" width="100px" height="60px" src="03.jpg" alt="">
        <img class="samll-img" onclick="clickImg('04.jpg')" width="100px" height="60px" src="04.jpg" alt="">
    </div>
    <script>
        function clickImg(imgsrc){
            let bigImgEle=document.querySelector(".big-img")
            bigImgEle.setAttribute("src",imgsrc)
        }
    </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>
        .small-img{
            height: 100px;
        }
        .active{
            border: 2px solid black;
        }
    </style>

</head>

<body>
    <div>
        <img class="big-img" width="300px" height="200px" src="01.jpg" alt="">
    </div>
    <div>
        <!-- 必须有一个active标签才能把代码运行起来 -->
        <img class="samll-img img1 active" onclick="clickImg('01.jpg','.img1')" width="100px" height="60px" src="01.jpg" alt="">
        <img class="samll-img img2" onclick="clickImg('02.jpg','.img2')" width="100px" height="60px" src="02.jpg" alt="">
        <img class="samll-img img3" onclick="clickImg('03.jpg','.img3')" width="100px" height="60px" src="03.jpg" alt="">
        <img class="samll-img img4" onclick="clickImg('04.jpg','.img4')" width="100px" height="60px" src="04.jpg" alt="">
    </div>
    <script>
        function clickImg(imgsrc,imgclass){
            let bigImgEle=document.querySelector(".big-img");
            bigImgEle.setAttribute("src",imgsrc);

            // 下面是高亮样式代码

            let highLight=document.querySelector(".active");

            // 判断当前标签对象的class属性是否包含指定的class类名
            console.log(highLight.classList.contains("active"));

            // 删除当前标签对象的class属性值中指定的class类名
            highLight.classList.remove("active");


            let currenImg=document.querySelector(imgclass);

            // 在当前标签对象的class属性值后面添加指定的class类名
            currenImg.classList.add("active");
        }
    </script>

</body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值