简体   繁体   English

CSS3 缩放变换和定位

[英]CSS3 scale transform and positioning

So I'm working on a project where I need to scale a div (and all the elements it contains) as the window resizes.所以我正在做一个项目,当窗口调整大小时,我需要缩放一个 div(以及它包含的所有元素)。 My idea is to use the CSS3 scale transform.我的想法是使用 CSS3 缩放变换。 However, as I resize I find that the div shrinks into the middle of the window rather than staying in the spot I positioned it in.但是,当我调整大小时,我发现 div 缩小到窗口的中间,而不是停留在我放置它的位置。

Here is a (really) simple example that I hope will illustrate when I mean: http://jsfiddle.net/hmY9q/3/ -- What I would like to see is the blue box flush with both the border of the result box and the bottom of the red box.这是一个(非常)简单的例子,我希望能说明我的意思: http : //jsfiddle.net/hmY9q/3/——我想看到的是蓝色框与结果框的边框齐平和红色盒子的底部。

My first thought at rectifying the problem was to calculate the difference between the postion I would like the div to be at and the position it actually appears and use the transform matrix to both scale and translate at the same time.我在纠正问题时的第一个想法是计算我希望 div 所在的位置与其实际出现的位置之间的差异,并使用变换矩阵同时缩放和平移。 I wasn't able to get this to work properly though and it seems like an inefficient and otherwise inelegant solution.不过,我无法让它正常工作,这似乎是一个低效且不优雅的解决方案。

Here's the code for that (won't let me post question without code apparently):这是代码(显然不会让我发布没有代码的问题):

var yShift = (container.height() * scale) - container.height();
var xShift = (container.width() * scale) - container.width();
container.css(browserID + "transform", "matrix("+ scale + ",0,0," + scale + "," + -xShift + "," + -yShift + ")");

note: I'm programming in haxe and compiling to js, so that's why the code is a bit different.注意:我正在用 haxe 编程并编译为 js,所以这就是代码有点不同的原因。 'container' is a JQuery object and can be treated as one. 'container' 是一个 JQuery 对象,可以被视为一个对象。

So, can anyone out there think of some magic CSS or javascript that would simplify this?那么,有没有人能想到一些神奇的 CSS 或 javascript 来简化这个? Am I on crazy pills and missing something really dumb?我是不是吃了一些疯狂的药而错过了一些非常愚蠢的东西? Thanks!谢谢!

It is simple with transform-origin .使用transform-origin很简单

For example: transform-origin: top left;例如: transform-origin: top left;

Demo演示

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

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