简体   繁体   English

如何使用JavaScript缩放图像以适合

[英]How to use JavaScript to scale an image to fit

I have a 67x67 px image that I would like to scale down to 45x45 to fit into an anchor but I do not know how to adjust it. 我有一个67x67像素的图像,我想缩小到45x45以适合锚点,但我不知道如何调整它。 My HTML is: 我的HTML是:

<a class="CompareProfilePic" id="CompareProfilePic1" href="#">
   <span id="CompareProfilePicActionBtn1" class="autocenter">
   </span>
</a>

My CSS is: 我的CSS是:

a.CompareProfilePic {
   height: 45px;
   width: 45px;
   border-radius: 50%;
   float: left;
   margin-right: 10px;
   cursor: default !important;
}

And my JS is: 我的JS是:

document.getElementById('CompareProfilePic1').style.cssText = 'background-image: url(img/pic1.jpg)';

All my searches led to changing the size of what is holding the image, but that needs to stay 45x45. 我的所有搜索都导致更改了保存图像的大小,但是需要保持45x45。 Any suggestiosn on how to scale the image to that size? 关于如何将图像缩放到该尺寸有什么建议吗?

Thanks for any tips or pointers. 感谢您的提示或指示。

How about instead of using JS, use only CSS with background-size ? 不使用JS,而是仅使用具有background-size CSS怎么样?

a.CompareProfilePic {
    height: 45px;
    width: 45px;
    border-radius: 50%;
    float: left;
    margin-right: 10px;
    cursor: default !important;
    background-image: url(img/pic1.jpg);
    background-size: 45px 45px; /*<----*/
}

Take a look here: 在这里看看

i used 我用了

document.getElementById('CompareProfilePic1').style.backgroundSize = "45px 45px";
 $(this).css('width','px');
 $(this).css('height','auto');

answered by this thread on stackoverflow 这个线程在stackoverflow上回答

javascript Image Resize javascript图像大小调整

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

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