javascript适配图片,对图片大小进行调整

<img src="http://home.baidu.com/resource/r/home/img/bnr_product1.jpg">

<script type="text/javascript">
var maxWidth = 500;
var img = document.getElementsByTagName('img');
for(var i=0;i<img.length;i++) {
    img[i].onload = function() {
        if(this.width>maxWidth) {
            this.width=maxWidth;
        }
    }
}
</script>

 

posted @ 2014-10-31 15:21  魔豆  阅读(361)  评论(0编辑  收藏  举报