現在想要實現的是:
1、如果圖片小於當前浏覽器窗口寬度,則顯示當前大小
2、如果圖片超過當前浏覽器窗口寬度,則顯示浏覽器窗口寬度-50px的寬度
高度都是自適應的(即按比例縮小)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文檔</title> <style> *{margin:0;padding:0;} #fontzoom{width:100%;overflow:hidden;} </style> <script> function $(id) { return document.getElementById(id); } function autoResizepic() { var bodywidth = $('fontzoom').style.width; bodywidth =document.body.scrollWidth var picTarget=$('fontzoom').getElementsByTagName('img'); if(picTarget.length>0) { for(var i =0;i<picTarget.length;i++) { if(picTarget[i].width>bodywidth) {picTarget[i].width=bodywidth-50;} if(picTarget[i].width<bodywidth) {picTarget[i].width=bodywidth-50;} } } } window.onresize=autoResizepic; </script> </head> <body> <div id="fontzoom"> <img src="http://read.91move.com/pics/nuojiya/6630/200607/02/0114370.jpg" /> <img src="http://read.91move.com/pics/nuojiya/6630/200607/02/0114370.jpg" /> </div> <script> autoResizepic(); </script> </body> </html>