设为首页 加入收藏

TOP

点击放大图片预览
2019-09-17 18:47:04 】 浏览:19
Tags:点击 放大 图片 预览

创造一个自适应的盒子,跟随浏览器宽高,目的是不覆盖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循环遍历元素自行改造

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇解决npm install卡住不动的小尴尬 下一篇Vue.js 源码分析(九) 基础篇 生命..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目