• js实现图片的大小自适应效果


    需求是传过来一个图片,根据外层div的大小自动进行缩放效果。

    function ShowSecondImg(v) {
        var rate, newX, newY,newW, newH = 0;   //表示图片相对窗口的缩放比例
        var imgW, imgH;
        var centerW = $(window).width() - $("#alarmQueue").width() - $("#presInfo").width() - 40;
    
        var centerH = $(window).height() - $("#detailedInfor").height()-148;
        $("#imgBoxs").find("#second").remove();
        var img = $('<img/>', { src: v.First_frame_image_url, "id": "second" });//v.First_frame_image_url
    
        img.prependTo('#imgBoxs');
    
        imgW = img.width();
        imgH = img.height();
        //获取的参数
        var rectX= parseInt(v.facerect.x);
        var rectY =parseInt(v.facerect.y);
        var rectWidth =parseInt(v.facerect.w);
        var rectHeight =parseInt(v.facerect.h);
        // alert(v.facerect.x);
    
        //var i = (centerW / 16 - centerH / 9) > 0 ? 0 : 1;
        var i= (centerW/centerH - imgW/imgH ) > 0 ? 0 : 1;
        if (i == 1) {
            //如果窗口宽不长,高长。以宽为准。
            rate = imgW / centerW;
            var imgRateH = imgH / rate;  //img real rate height;
            newX = rectX / rate
            newY = rectY / rate;
            newW = rectWidth / rate;
            newH = rectHeight / rate;
    
            $("#imgBox").width(centerW);
            $("#imgBox").css("height", "100%");
            $("#second").css({ "width": centerW, "height": imgRateH });
        }
        else if (i == 0) {
            //如果窗口宽足够长,高不长。以高为准。
            rate = centerH / imgH;
            var imgRateW = imgW * rate;  //img real rate width;
            var leftsideW = (centerW - imgRateW) / 2;    //leftside add rightside  width;
            newX = rectX * rate + leftsideW;
            newY = rectY * rate;
            newW = rectWidth * rate;
            newH = rectHeight * rate;
    
            $("#imgBox").height(centerH);
            $("#imgBox").css("width", "100%");
            $("#second").css({ "width": imgRateW, "height": centerH });
    
        }
        // $("#detailedInfor").height($(".container").height()-centerH);
        $("#faceDiv").attr('style', 'display:block;top:' + newY + 'px;left:' + newX + 'px;' + newW + 'px;height:' + newH + 'px');
    
    }
  • 相关阅读:
    webpack : 无法加载文件 D: odejs ode_globalwebpack.ps1,因为在此系统上禁止运行脚本。
    TypeError: __WEBPACK_IMPORTED_MODULE_0_react___default.a.createContext is not a function报错处理
    React中import的用法
    antd-react-mobile(踩坑记录)
    [转][C#]BarCodeToHTML
    [转][C#]Environment 类
    [转][easyui]右键菜单
    [转][C#]Web.config 相关配置
    [转]模拟键盘输入
    python3 的 zip
  • 原文地址:https://www.cnblogs.com/pengpenglin/p/6232261.html
Copyright © 2020-2023  润新知