自动等比例缩放网页中的图片

转载时请标明文章原始出处和作者信息, 作者: lostsnow.
http://www.lsproc.com/blog/auto_resize_images/

resizeimg.js

window.onload = function() {

    for (var index = 0; index < document.images.length; index++) {

        var widthRestriction = 400;
        var heightRestriction = 400;
        var rate = document.images[index].width / document.images[index].height;

        if (document.images[index].width > widthRestriction) {
            document.images[index].width = widthRestriction;
            document.images[index].height = widthRestriction / rate;
        } else if (document.images[index].height > heightRestriction) {
            document.images[index].height = heightRestriction;
            document.images[index].width = heightRestriction * rate;
        }

        document.images[index].onclick = function() {window.open(this.src)};
        document.images[index].title = document.images[index].title + ' 点击在新窗口中查看原图';
    }
}

-- EOF --

Related Posts

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>