简体   繁体   English

如何动态设置图像尺寸?

[英]How to set image size dynamically?

I am trying to place an image element on top of the background image. 我试图将图像元素放在背景图像的顶部。

The image element will need to animate based on user's action. 图像元素将需要根据用户的动作进行动画处理。 I have my background image css like the following: 我的背景图片CSS如下所示:

css 的CSS

body {
    background: url('test.png') no-repeat center center;
    -webkit-background-size: 100% auto;
    background-color: #00ABBA;
    -moz-background-size: 100% auto;
    -o-background-size: 100% auto;
    background-size: 100% auto;
}

html html

<div> <img src='ball.png' /> <div > <div> <img src='ball.png' /> <div >

so the background image will always be 100% of the screen and be responsive. 因此背景图片将始终是屏幕的100%并具有响应能力。

However, the image element has a set size and I am not sure how to set the size to accommodate the background image size (bigger screen has bigger background image/ smaller screen has smaller background image). 但是,图像元素具有设置的大小,我不确定如何设置大小以适应背景图像的大小(较大的屏幕具有较大的背景图像/较小的屏幕具有较小的背景图像)。

Can anyone help me to set the image element with the consistent size with background image? 谁能帮助我将图像元素设置为与背景图像一致的大小? Thanks a lot! 非常感谢!

You have a couple of options here. 您在这里有几个选择。

Most of them boil down to comparing some "base size" of the background image to it's actual size. 他们中的大多数归结为将背景图像的某些“基本尺寸”与其实际尺寸进行比较。

Let's say you pick out your numbers when your background is 1000px wide and 700px tall. 假设您选择背景宽为1000px且高度为700px的数字。 That is our base size. 那是我们的基本尺寸。

var baseWidth = 1000;
var baseHeight = 700;

Now we need to know what we actually ended up with. 现在,我们需要知道最终的结果。 To do this, we get the size of the element with the background. 为此,我们获得具有背景的元素的大小。 Let's say the div has an ID of "background" for simplicity sake. 为了简单起见,假设div的ID为“背景”。

var backgroundElement = document.getElementById('background');
var currentWidth = backgroundElement.width;
var currentHeight = backgroundElement.height;

(Note, padding, margin, border, etc. can affect the sizing of the width, so you probably want to use a third party library like jQuery or make sure you take those in to account yourself if they are applicable to ensure an appropriate measurement). (请注意,填充,边距,边框等可能会影响宽度的大小,因此您可能希望使用jQuery之类的第三方库,或者确保将它们考虑在内以确保合适的尺寸)。

Then, we want to know, relatively speaking, how much we changed. 然后,相对而言,我们想知道我们做了多少更改。

var scaleWidth = currentWidth / baseWidth;
var scaleHeight = currentHeight / baseHeight;

If we are larger than the base, scale* is greater than 0. If it's smaller, scale* is less than zero. 如果我们大于基数,则scale *大于0。如果小于基数,则scale *小于零。

Now, we decide what we want to do. 现在,我们决定要做什么。 Let's assume the image element has an id of "image". 假设image元素的ID为“ image”。

var image = document.getElementById('image');

If we want to increase the image and don't care about saving aspect ratio: 如果我们想增加图像并且不关心保存宽高比:

image.width = image.width * scaleWidth;
image.height = image.height * scaleHeight;

If we do want to save aspect ratio and want to keep it all within the div. 如果我们确实要保存长宽比并希望将其全部保持在div之内。

image.width = image.width * Math.min(scaleWidth, scaleHeight);
image.height = image.height * Math.min(scaleWidth, scaleHeight);

(Note, if the aspect ratio can change drastically, that snippet is a bit more complicated because you'd have to make sure the resulting size is small enough to still fit after the fact. Sometimes the scale you need would be smaller than both scaleWidth and scaleHeight). (请注意,如果长宽比可以急剧变化,则该代码段会稍微复杂一些,因为您必须确保生成的大小足够小,以使事后仍然可以容纳。有时,您需要的缩放比例会小于两个scaleWidth和scaleHeight)。

If you want to shift it's position, make it absolutely positioned and then move it by a scaled amount. 如果要移动它的位置,请使其绝对定位,然后按比例移动它。

// Assumes it already has an absolute position of top and left already set.
image.style.left = parseInt(image.style.left.replace(/[a-z]+$/, '') * scaleWidth;
image.style.top = parseInt(image.style.top.replace(/[a-z]+$/, '')) * scaleHeight;

And so on... 等等...

Basically, once you have decided on a base size and know your scale, you can start making tweaks relative to your base, which can then be scaled up to your background. 基本上,一旦确定了基准大小并知道了比例,就可以相对于基准进行调整,然后可以根据背景进行调整。

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

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