简体   繁体   English

使用CSS变换或javascript来缩放元素以动态地适应其父级

[英]Use CSS transforms or javascript to scale an element to fit its parent dynamically

I have a page in which I have a wheel of <div> elements, the entire wheel rotates when you click a button. 我有一个页面,其中我有一个<div>元素的轮子,单击一个按钮时整个轮子旋转。

I achieve this effect by using CSS transforms, which are absolute in nature. 我通过使用CSS转换实现了这种效果,这些转换本质上是绝对的。 However the wheel is very big, it looks nice on my HD display, but smaller screens get the edges cut off. 然而,车轮非常大,在我的高清显示器上看起来不错,但是较小的屏幕会使边缘切断。 I can not use % widths like I could with a normal layout, what I need is to scale the entire page down in the same way most browsers zoom functions work. 我不能像普通布局那样使用%宽度,我需要的是缩放整个页面,就像大多数浏览器缩放功能一样。

For myself I know that ctr+mouseWheel will zoom out the page so I can see the entire page, however I can not expect others to do this. 对于我自己,我知道ctr + mouseWheel会缩小页面,所以我可以看到整个页面,但我不能指望其他人这样做。

I know I can use -browser-transform: scale(amt); 我知道我可以使用-browser-transform: scale(amt); on a wrapper div to get the effect I want, however I can not figure out a way to do it dynamically. 在包装器div上获得我想要的效果,但是我无法想出一种动态的方法。 If I set the scale to .5 it will be .5, no matter the screen. 如果我将比例设置为.5,它将是.5,无论屏幕如何。 I want the edges of the wheel to just be a few pixels from the edges of the screen on ANY screen. 我希望在任何屏幕上,滚轮的边缘距离屏幕边缘几个像素。 I know that media queries could be used to help the problem, but they would either leave me with results that are less than ideal, or require too many different queries. 我知道可以使用媒体查询来帮助解决问题,但是他们会给我留下不太理想的结果,或者需要太多不同的查询。 There must be a way to modify -browser-transform: scale(amt); 必须有一种方法来修改-browser-transform: scale(amt); programmatically, or some other way to have finite control. 以编程方式,或其他方式进行有限控制。

Any thoughts? 有什么想法吗?

Have you tried using media queries in css to target different screens. 您是否尝试在css中使用媒体查询来定位不同的屏幕。 for example, have a media query in your css file that states that at a width of 320 - 480 pixels, the div containing this wheel is scaled to 50%. 例如,在css文件中有一个媒体查询,指出宽度为320 - 480像素时,包含此轮的div将缩放到50%。 Then at 481-768 pixels, the div container is scaled to 75%. 然后在481-768像素处,div容器缩放到75%。 and from 769 pixels up, the div is scaled to 100%. 从769像素向上,div缩放到100%。

That should help you accomplish the dynamic scaling you want at different screen sizes. 这应该可以帮助您在不同的屏幕尺寸下完成所需的动态缩放。 If you would like a demo, I'll be glad to make a jsfiddle showing it. 如果你想要一个演示,我会很高兴让一个jsfiddle显示它。

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

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