zoukankan      html  css  js  c++  java
  • 牛掰的图片等比缩放js代码

    function resizeImg(img,oAW,oAH){
    var oimgW = img.width,
    oimgH = img.height,
    oimg = img,
    oY = (oimgH/oimgW).toFixed(2),
    oX = (oimgW/oimgH).toFixed(2);
    if(oimgW <= oAW&&oimgH <= oAH){//图片高和宽比指定的宽高都小
    oimg.style.height = oimgH+'px';
    oimg.style.width = oimgW+'px';
    } else if(oimgW >= oAW&&oimgH >= oAH){//图片高和宽比指定的宽高都大
    if(oY*oAW>=oAH){ //图片高比宽大
    oimg.style.height = oAH+'px';
    oimg.style.width = oX*oAH+'px';
    }else{ //图片高比宽小
    oimg.style.height = oY*oAW+'px';
    oimg.style.width = oAW+'px';
    }
    }else if(oimgW>oAW &&oimgH < oAH){//图片宽比指定宽大,高比指定的小
    oimg.style.height = oY*oAW+'px';
    oimg.style.width = oAW+'px';
    }else if(oimgW<oAW &&oimgH > oAH){//图片宽比指定宽小,高比指定的大
    oimg.style.height = oAH+'px';
    oimg.style.width = oX*oAH+'px';
    }
    }
    function resizeImgMid(img,oAW,oAH){
    var oimgW = img.width,
    oimgH = img.height,
    oimg = img,
    oY = (oimgH/oimgW).toFixed(2),
    oX = (oimgW/oimgH).toFixed(2);
    if(oimgW <= oAW&&oimgH <= oAH){//图片高和宽比指定的宽高都小
    oimg.style.height = oimgH+'px';
    oimg.style.width = oimgW+'px';
    oimg.style.marginLeft = 1/2*(oAW-oimgW)+'px';
    oimg.style.marginTop = 1/2*(oAH-oimgH)+'px';
    } else if(oimgW >= oAW&&oimgH >= oAH){//图片高和宽比指定的宽高都大
    if(oY*oAW>=oAH){ //图片高比宽大
    oimg.style.height = oAH+'px';
    oimg.style.width = oX*oAH+'px';
    oimg.style.marginLeft = 1/2*(oAW-oX*oAH)+'px';
    oimg.style.marginTop = 0;
    }else{ //图片高比宽小
    oimg.style.width = oAW+'px';
    oimg.style.height = oY*oAW+'px';
    oimg.style.marginLeft = 0;
    oimg.style.marginTop = 1/2*(oAH-oY*oAW)+'px';
    }
    }else if(oimgW>oAW &&oimgH < oAH){//图片宽比指定宽大,高比指定的小
    oimg.style.height = oY*oAW+'px';
    oimg.style.width = oAW+'px';
    oimg.style.marginLeft = 0;
    oimg.style.marginTop = 1/2*(oAH-oY*oAW)+'px';
    }else if(oimgW<oAW &&oimgH > oAH){//图片宽比指定宽小,高比指定的大
    oimg.style.height = oAH+'px';
    oimg.style.width = oX*oAH+'px';
    oimg.style.marginLeft = 1/2*(oAW-oX*oAH)+'px';
    oimg.style.marginTop = 0;
    }
    }

  • 相关阅读:
    mysql慢日志设置
    CURL模拟登陆
    违法图片检测
    PHP取二进制文件头快速判断文件类型
    重写session
    mysql处理高并发,防止库存超卖
    mysql中使用update select
    PHP中使用Luhn算法校验信用卡及借记卡卡号
    红包算法
    DIV当textarea使用,在聚焦的时候将光标移动到内容的末尾
  • 原文地址:https://www.cnblogs.com/freefish12/p/4347321.html
Copyright © 2011-2022 走看看