2014-09-10 63 views
0

我有一個67x67像素圖像,我想縮小到45x45以適合錨點,但我不知道如何調整它。我的HTML是:如何使用JavaScript來縮放圖像以適合

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

我的CSS是:

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

而我的JS是:

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

我所有的搜查,對不斷變化的是什麼阻礙了圖像的大小,但需要保持45x45。有關如何將圖像縮放到該尺寸的任何建議?

感謝您的任何提示或指針。

回答

0

看看 here:

我用

document.getElementById('CompareProfilePic1').style.backgroundSize = "45px 45px"; 
+0

啊,那很完美!不能相信它是如此簡單,但我無法在其他任何地方找到它。謝謝! – Scott 2014-09-10 16:23:09

1

怎麼樣,而不是使用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; /*<----*/ 
}