简体   繁体   English

将div缩放为div,就像背景图像一样

[英]Scale div into div like background image

I'm working on a web app and now facing a rather tricky task. 我正在开发一个Web应用程序,现在面临一项相当棘手的任务。 I have an image that I have to add overlay for. 我有一个图像,我必须添加叠加。

Let's say I have an image of a car. 假设我有一辆汽车的图像。 I need to add values, like outside and inside temperature, on top of the car image. 我需要在汽车图像上添加外部和内部温度等值。

It would be easy to add fixed pixel offsets for those temperature labels, but the image need to be scalable to every screen height and width-wise. 为这些温度标签添加固定像素偏移量会很容易,但图像需要可以扩展到每个屏幕的高度和宽度。 I can't think of easy way to scale div into div exactly as "background-size:contain;" 我想不出简单的方法将div缩放为div完全像“background-size:contain;” does for images. 适用于图像。

Could someone point me to right tracks before I write complex javascript scaling logic? 在我编写复杂的javascript缩放逻辑之前,有人能指出我正确的轨道吗?

If you are using a SVG image this would be easy: http://css-tricks.com/svg-text-typographic-designs/ 如果您使用SVG图像,这将很容易: http//css-tricks.com/svg-text-typographic-designs/

Here is two examples with CSS: 以下是CSS的两个示例:

  1. http://codepen.io/chriscoyier/pen/zxqdev
  2. http://maketea.co.uk/2013/12/16/smooth-text-overlays-with-css-transforms.html
ww = window width
wh = window height
ow = Original Width of your image
oh = Original Height of your image
nw = New Width of your image
nh = New Height of your image
mt = Margin top to adjust image to the screen vertically
ox = overlay location x coordinate according to original size of the image
oy = overlay location y coordinate according to original size of the image
nox = New ox
noy = new oy
sf = scaling factor

based on screen size you'll have a scaling factor for your image. 根据屏幕尺寸,您将拥有图像的缩放系数。

sf = ww / ow -> we find our scaling factor
nh = oh * sf -> then calculate our new image height
mt = (oh - nh) / 2 -> amount we need to shift image up to center screen
nox = ox * sf -> new overlay x coordinate
noy = (oy * sf) - mt -> new overlay y coordinate

edit 编辑

if image is too wide then this logic needs to be adjusted to shift image horizontally not vertically 如果图像太宽,则需要调整此逻辑以水平移动图像而不是垂直移位

edit 2 编辑2

If you're targeting modern browsers only, then you can use CSS transform: scale(sx[, sy]); 如果您只针对现代浏览器,则可以使用CSS transform: scale(sx[, sy]); . This will re-size your DIV and all it's content proportionally Use transform: scale() with transform-origin: 0 0; 这将按比例重新调整DIV及其所有内容的大小使用transform: scale() with transform-origin: 0 0; to re-size from top left corner. 从左上角重新调整大小。

I'm never tested CSS3 object-fit , but I think it's very useful for your solution: 我从来没有测试过CSS3 对象 ,但我认为它对你的解决方案非常有用:

https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit

https://dev.opera.com/articles/css3-object-fit-object-position/ https://dev.opera.com/articles/css3-object-fit-object-position/

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

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