2012-12-21 24 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。这将使图像符合其包含的元素。