来源: 互联网 日期: 2020-07-13 20:02:36
现在想要实现的是:
1、如果图片小于当前浏览器窗口宽度,则显示当前大小
2、如果图片超过当前浏览器窗口宽度,则显示浏览器窗口宽度-50px的宽度
高度都是自适应的(即按比例缩小)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "upload/article/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>