简体   繁体   English

如果图像大于视口,请使用Javascript重新调整图像大小

[英]Re-sizing an image with Javascript if image is bigger than the viewport

Currently I am working off of this code to resize an image with javascript 目前,我正在处理此代码以使用javascript调整图像大小

function resize(which) {
    var elem = document.getElementById(which);
    if (elem == undefined || elem == null) return false;
        if (max == undefined) {
            if (elem.width > document.documentElement.clientWidth) {

            } else if (elem.height > document.documentElement.clientHeight) {

            } else if (elem.height > document.documentElement.clientHeight && elem.height > document.documentElement.clientWidth) {

            }
        }
        if (elem.width > elem.height) {
            if (elem.width > max) 
                elem.width = max;
        } else {
            if (elem.height > max) 
                elem.height = max;
        }
}

I was wondering if I am heading in the right direction to resize an Image with javascript if the image is bigger then the clients viewport. 我想知道我是否朝着正确的方向使用javascript调整图像的大小,如果图像较大则客户端视口。 I'd like some help with this also as I don't know what to do next to finish the code. 我也希望对此有所帮助,因为我不知道下一步该怎么做才能完成代码。 Thanks. 谢谢。 :) :)

A similar question has been asked before: 之前有人问过类似的问题:
Image resize to fit screen 调整图像大小以适合屏幕

Also, this jQuery plugin seems to do exactly what you need: 另外,这个jQuery插件似乎完全可以满足您的需求:
http://code.google.com/p/jquery-imagefit-plugin/ http://code.google.com/p/jquery-imagefit-plugin/

if you perform it on a 100% height, 100% width element: 如果在高度为100%,宽度为100%的元素上执行:
http://www.tutwow.com/htmlcss/quick-tip-css-100-height/ http://www.tutwow.com/htmlcss/quick-tip-css-100-height/

Here's an example of this solution: 这是此解决方案的示例:
http://jsfiddle.net/nottrobin/zndkg/ http://jsfiddle.net/nottrobin/zndkg/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM