vue--点击图片放大的自定义指令(低级版)

 1 Vue.directive('showImg',{
 2     inserted(el){
 3         el.addEventListener('click',function(e){
 4             e.stopPropagation()
 5             const imgSrc = el.getAttribute('src'); //获取当前的图片链接
 6             let _showImg_ =  document.getElementById('__showImg__')
 7             if( _showImg_){
 8                 _showImg_.style.display = 'flex';
 9                 const childImg =  _showImg_.childNodes[0]
10                 childImg.setAttribute('src',imgSrc)
11             }else{
12                 _showImg_ =  document.createElement('div');
13                 const body = document.body
14                 _showImg_.innerHTML = `<img src="${imgSrc}" style="max-width: 100%;" />`
15                 _showImg_.setAttribute('id','__showImg__');
16                 _showImg_.setAttribute('style','display: flex;justify-content: center;align-items:center;position:fixed;z-index:1000;left:0;top:0;right:0;bottom:0;background:rgba(0,0,0,0.7);');
17                 body.appendChild(_showImg_);
18 
19                 _showImg_.addEventListener('click',function(e){
20                     e.stopPropagation();
21                     this.style.display = 'none'
22                 })
23             }
24         })
25     }
26 })

 

转载于:https://www.cnblogs.com/carrotWu/p/9088283.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值