Javascript Image Resize

前端 未结 13 1470
南旧
南旧 2020-11-28 23:03

Does anyone know how to resize images proportionally using JavaScript?

I have tried to modify the DOM by adding attributes height and width

13条回答
  •  死守一世寂寞
    2020-11-28 23:55

    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.

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

提交回复
热议问题