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;
    }
    }

  • 相关阅读:
    Stopping User Manager for UID 121” error
    IDEA——如何再次开启ESLint
    堆排序
    akka-typed(10)
    微慕WordPress小程序专业版v3.0发布
    微慕WordPress小程序增强版v2.0发布
    微慕WordPress小程序开源版v3.6发布
    微慕小程序开源版A标签优化说明
    微信小程序开放「分享到朋友圈」功能
    微信小程序直播安装和开发指南
  • 原文地址:https://www.cnblogs.com/freefish12/p/4347321.html
Copyright © 2011-2022 走看看