点击放大图片预览

创造一个自适应的盒子,跟随浏览器宽高,目的是不覆盖body元素

css部分  

#box{
				justify-content: center;
				align-items: center;
				position: absolute;
				display: none;
				top:0px;
				left: 0;
				z-index: 15;
			}
			#box span{
				position: absolute;
				top:110px;
				right:22px;
				color: white;
				font-size: 30px;
				
			}

  html部分

	<div id="box">
		<span id="close">关闭</span>
	</div>
<div id="imgbox"> <img src="img/leaveform.png" id="theimg" style="width=200px,height="200px"/>
<div>

  js 部分生成自适应盒子 图片元素填入盒子,点击关闭隐藏并还原图片

	var theimg=document.getElementById('theimg');
        theimg.ontouchstart=function(){
  	        var winHeight=window.innerHeight;
		var winWidth=window.innerWidth;		
        	var BoxObj=document.getElementById('box');   //获取盒子对象 	
        	BoxObj.style.width=winWidth+'px';  //生成宽度
        	BoxObj.style.height=winHeight+'px';//生成高度
                BoxObj.style.backgroundColor='#929292'//添加背景色
                BoxObj.style.display='flex' //显示并弹性布局
                this.style.width=(BoxObj.offsetWidth-100)+'px'; //设置当前图片宽度
                this.style.height=(BoxObj.offsetHeight-400)+'px';//设置当前图片高度
        	$('#box').append(this) //填入元素   
           document.body.addEventListener('touchmove',bodyScroll,false);//禁止页面滑动
          $('body').css({'position':'fixed',"width":"100%"});

  

        	}
/*关闭灯箱*/
	     $('#close').click(function(){
	     	$('#box').hide()
	     	theimg.style.width=269+'px'; //还原
	     	theimg.style.height=240+'px';
	        $("#imgbox").append(theimg);

          

document.body.removeEventListener('touchmove',bodyScroll,false); 
          $("body").css({"position":"initial","height":"auto"});

  

	     })
	

  若是多张图片可用for循环遍历元素自行改造

转载于:https://www.cnblogs.com/huazai1/p/11063100.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值