2012-12-21 26 views
-1

我有一個圖像。我想在一個小窗口中調整圖像大小。如何通過將圖像放大並縮小圖像來拉伸窗口中的圖像。jQuery或Javascript圖像查看器爲不同大小的單個圖像?

寬度和高度屬性無法正常工作。 我的意思是如何通過如下給出寬度和高度來拉伸圖像;

width=300px;height=300px 
again width=600px;height=600px; 
and width=900px;height=900px; 
and widht=1800px;height=1800px; 
+0

類似於'document.getElementById('middle-logo')。style.width ='300px''和'document.getElementById('middle-logo')。style.height ='300px''你是什麼尋找?如果是,請閱讀更多細節[這裏](https://developer.mozilla.org/en-US/docs/DOM/element.style) –

回答

1

將高度設置爲auto,這將保持相對於寬度的比率。此外,HTML屬性不需要「px」單位,這是CSS。

<img src="someimage.jpg" width="300" height="auto" alt="Some image"> 
<img src="someimage.jpg" width="600" height="auto" alt="Some image"> 
<img src="someimage.jpg" width="900" height="auto" alt="Some image"> 
<img src="someimage.jpg" width="1800" height="auto" alt="Some image"> 

您已標記此javascript。如果您想用JavaScript更改大小,則需要先使用id引用圖像,然後再更改屬性。

<script> 
    window.onload = function(){ 
    document.getElementById('testimg').width = 500; 
    } 
</script> 

<img id="testimg" src="http://farm9.staticflickr.com/8426/7758832526_cc8f681e48.jpg" height="auto"> 

如果你想要的形象去放大或縮小與窗口,就像在一個彈出窗口,設置寬度爲100%並保留高度auto。這將使圖像符合其包含的元素。