每天一个效果 :(6)相册选择功能

今天来写一个功能,就是鼠标放在那个小图片上,小图就会变到大图的位置上去。下面我们来看一下html代码

 <div>
        <img src="https://img0.baidu.com/it/u=2309643650,2890855463&fm=26&fmt=auto" alt="" class="big">
    </div>
    <div class="small">
        <img src="https://img0.baidu.com/it/u=2309643650,2890855463&fm=26&fmt=auto" alt="">
        <img src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fn.sinaimg.cn%2Fsinakd20200721ac%2F285%2Fw614h471%2F20200721%2F46f5-iwtqvyi9444293.jpg&refer=http%3A%2F%2Fn.sinaimg.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1651243261&t=9632e6b36e740bc4994fac0e46652bde" alt="">
        <img src="https://img2.baidu.com/it/u=3604067922,525804311&fm=26&fmt=auto" alt="">
        <img src="https://img2.baidu.com/it/u=3562558155,1107192441&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=711" alt="">
    </div>

css代码

    <style>
        * {
            padding: 0;
            margin: 0;
        }
       .big {
           
            width: 415px;
            height: 400px;
        }
        .small>img {
            width: 100px;
            height: 100px;
        }
    </style>

接着看一下效果

写一下逻辑:其实就是写一个鼠标放上去的事件onmouseover,当鼠标放上去时候,可以把小图的src放在大图的src上面。我们看一下js代码。

    <script>
        //逻辑:本质上,把小图的src赋值给大图的src
        //图片元素.src =''
        let imgs = document.getElementsByTagName('img');
            console.log(imgs);
        for(let i = 1;i < imgs.length;i++) {
            imgs[i].onmouseover = function(){
                //把当前小图的src给到大图src
                imgs[0].src = this.src;
            
            }
        }

    </script>

看最后效果

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值