简体   繁体   English

CSS缩放和转换导致重叠

[英]CSS Scale and Transform causing overlaps

In HTML page I am trying to achieve zoom functionality using CSS transform scale property. 在HTML页面中,我尝试使用CSS变换比例属性实现缩放功能。 Inside the HTML page, I am having more div elements and all are placed using absolute position technique. 在HTML页面中,我有更多的div元素,所有元素都使用绝对位置技术放置。 Then I apply the scale property to each div and I was able to see like zoom but all div positions gots collapsed each other. 然后,将scale属性应用于每个div,我可以看到像zoom一样的效果,但是所有div位置彼此折叠。 So how to avoid collapse and maintain the gap between each other after applied scaling. 因此,在应用缩放后如何避免崩溃并保持彼此之间的间隙。

Please suggest me how to stop overlapping and how to calculate left and top position of each element accordingly to scale property. 请建议我如何停止重叠以及如何根据scale属性计算每个元素的左侧和顶部位置。

 var zoomScale = 1; $(document).ready(function(){ $("#zoomin").click(function(){ if(zoomScale >=4 ) return; zoomScale = zoomScale +1; dispStatus(); applyZoomIn(); }); function applyZoomIn(){ $(".zoomable").each(function(index){ var left = $(this).position().left; var top = $(this).position().top; var newLeft = left*zoomScale +"px"; var newTop = top*zoomScale +"px"; $(this).css('transform',' scale('+zoomScale+','+zoomScale+')') }); } function dispStatus(){ $("#displayStatus").text("Current Zoom Scale : "+zoomScale); } dispStatus(); }); 
 #container{ width:500px; height:500px; border:1px solid red; position:relative; overflow:auto; } .zoomable{ background-color:lightblue; border:1px solid red; width:100px; height:100px; position:absolute; transform-origin: top left; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <button id="zoomin"> Zoom In</button> <div id="displayStatus"></div> <br/><br/> <div id="container"> <div class="zoomable">First Div</div> <div class="zoomable" style="left:105px">Second Div</div> </div> 

JSFiddle Link JSFiddle链接

You can add .zoomable div into one more parent (called .inner-container ) and add zoom functionality to it. 您可以将.zoomable div添加到另一个父级(称为.inner-container )中,并.inner-container添加缩放功能。 So that .container div will not get zoomed and zoomable div will maintain position. 这样.container div将不会缩放,而zoomable div将保持位置。

See the Snippet below: 请参见下面的代码段:

 var zoomScale = 1; $(document).ready(function(){ $("#zoomin").click(function(){ if(zoomScale >=4 ) return; zoomScale = zoomScale +1; dispStatus(); applyZoomIn(); }); function applyZoomIn(){ $(".inner-container").css('transform',' scale('+zoomScale+','+zoomScale+')'); } function dispStatus(){ $("#displayStatus").text("Current Zoom Scale : "+zoomScale); } dispStatus(); }); 
 .zoomable{ background-color:lightblue; width:100px; height:100px; position:absolute; border:1px solid red; } #container{ width:500px; height:500px; border:1px solid red; position:relative; overflow:auto; } .inner-container{ transform-origin: top left; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button id="zoomin"> Zoom In</button> <div id="displayStatus"></div> <br/><br/> <div id="container"> <div class="inner-container"> <div class="zoomable">First Div </div> <div class="zoomable" style="left:105px">Second Div </div> </div> </div> <br/><br/> 

You can also test it here 您也可以在这里进行测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM