在将图像分配给 img 标签之前使用 javascript 调整图像大小
我对 HTML 和 CSS 开发非常陌生。
我有一个带有 img
标记的 HTML 页面。该标签将保存一些图像。这些图像具有不同的尺寸。我使用 CSS 设置了 img
标签的宽度和高度。
所有图像都将使用 JavaScript 显示。
在将图像分配给 IMG
标记之前,我是否需要使用 JavaScript 调整图像大小? 也许,我不需要这样做,因为我使用 CSS 设置了 IMG
大小。比例会受到限制吗?
对不起我的英语。
I'm very very new on HTML and CSS development.
I have a HTML page with an img
tag. This tag will hold some images. These images has different sizes. I set up a width and height to img
tag using CSS.
All images will be shown using JavaScript.
May I need to resize images with JavaScript before I assign an image to IMG
tag?
Maybe, I don't need to do that because I set up IMG
size using CSS. Proportions will be constrained?
Sorry for my English.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 css 设置了高度和宽度,则不会保持宽高比。但是,如果您使用 html
img
标记指定任一属性,则将保持宽高比。The aspect ratio wont be maintained if you have set both the height and width using css. But if you specify any one of the properties using the html
img
tag then the aspect ratio would be maintained.您应该在服务器端执行此操作。带有管理脚本。您可以在上传原始图像时创建原始图像的版本,然后这些图像可以被 HTML/CSS 端安全地使用。
这不是客户端需要完成的任务。基本上,您在这里所做的就是下载一个大图像(大量数据),然后将其简化以创建一个较小的图像。所以你浪费了客户端的带宽和服务器的带宽。
如果绝对没有这种可能性,并且您有一个固定区域(例如 640x480),那么您可以通过以下方式实现基本裁剪并保持宽高比:
HTML:
CSS:
You should do this on server side. With an administration script. You can create the versions of the original image when it is uploaded, then those images can be safely used by the HTML/CSS side.
This is not a task to do on the client side. Basically what you are doing here is to download a big image (lots of data) then dumb it down to create a smaller image. So you are wasting bandwindth of the client and bandwidth of the server.
If there is absolutely no ther possibility and you have a fixed area say 640x480, then you can acieve rudimentary cropping with maintaining ascpect ratio with the following:
HTML:
CSS: