Javascript图像调整大小 [英] Javascript Image Resize

查看:162
本文介绍了Javascript图像调整大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何使用JavaScript按比例调整图片大小?

Does anyone know how to resize images proportionally using JavaScript?

我试图通过添加属性来修改DOM height 宽度即时,但似乎无法在IE6上运行。

I have tried to modify the DOM by adding attributes height and width on the fly, but seems did not work on IE6.

推荐答案

要按比例修改图像,只需更改其中一个宽度/高度css属性,将另一个设置保留为自动。

To modify an image proportionally, simply only alter one of the width/height css properties, leave the other set to auto.

image.style.width = '50%'
image.style.height = 'auto'

这将确保其宽高比保持不变。

This will ensure that its aspect ratio remains the same.

请记住,浏览器在调整图像大小时往往 suck 很好 - 你可能会发现你重新调整大小的图像看起来很糟糕。

Bear in mind that browsers tend to suck at resizing images nicely - you'll probably find that your resized image looks horrible.

这篇关于Javascript图像调整大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆