1.div布局

<div class="card-img-show">
<div class="upload-img-conss" >
<img style="width: 200px" class="addPersonFile" id="personImg" :src="personImgDataUrl" >
<div class="upload-img-success-bg">
<div class="upload-promise-img-chuan-margin">
</div>
<p class="upload-img-add-front" style="margin-top: 11px; padding-left: 0">证件有效信息面</p>
</div>
</div>
<div class="card-img-show">
<input style="position: absolute; width: 52%; height: 73%" class="register-person-260-upload" :class="{'is-danger': errors.has('cardImg') }" v-validate="'required|unitCardImg'" name="cardImg" type="file" id="addPersonFile" v-on:change="getFile($event)"
placeholder="请上传证件照">
<p v-show="errors.has('cardImg')" style="display:none;color: red;font-size: 12px;">{{ errors.first('cardImg') }}</p> <p class="card-img-show-p">文件格式为:jpg或png;文件大小:1M至5M</p>
</div>
</div>

  效果图

根据图片原始尺寸等比缩放,不拉伸图片宽高;

前端使用vue框架:图片上传完成后渲染

updated(){
$(".register-container img").each(function(){
if(!!$(this).attr("src") && $(this).attr("src").indexOf("http")>-1){
var imgid= $(this).attr("id")
//添加遮罩可忽略
$("#"+imgid+"+ div:first").addClass("upload-img-success-bg-change");
//图片id
var img = $(this);
//图片自适应宽高
imgSuite(imgid,img);
}
});
},

 公共js方法

function imgSuite(imgid,img) {
var realWidth;//真实的宽度
var realHeight;//真实的高度
//虚拟img标签
$("<img/>").attr("src", $(img).attr("src")).on('load',function() {
realWidth = this.width;
realHeight = this.height;
var upPercent = realWidth/realHeight;
var parentDiv = $("#"+imgid).parent();
var parentWidth = parentDiv.width();//父级宽
var parentHeight = parentDiv.height();//父级高
var orgPercent = parentWidth/parentHeight;
if(upPercent<orgPercent){
$("#"+imgid).css({width:"auto",height:parentHeight+"px",margin:"0 auto",display:"block"});
}else {
var differHeight = parentWidth*realHeight/realWidth;//父级高度减去图片高度差
var marginTop = (parentHeight - differHeight)/2;//距离顶部高
$("#"+imgid).css({height:"auto",width:parentWidth+"px",marginTop: marginTop + "px",display:"block"});
}
});
}

  

 

css图片根据div宽高比例自适应的更多相关文章

  1. css实现左边定宽右边自适应的5种方法总汇

    在网页布局中,通常需要实现左边定宽右边自适应布局,默认html的结构如下: <div class="box"> <div class="left&quo ...

  2. css布局:定宽,自适应

    css三栏布局:1.中自:float,absolute,margin三种方法.2.中固:margin,table两种方法. 两边定宽,中间自适应: float: #left{ float:left; ...

  3. jq获取图片的原始尺寸,自适应布局

    原理: each()遍历,width().height()获取宽高, load() 注意: 由于页面加载完了,但图片不一定加载完了,所以直接通过 $("img").width(), ...

  4. UIImageView圆角,自适应图片宽高比例,图片拉伸,缩放比例和图片缩微图

    /*      设置圆角,通过layer中的cornerRadius和masksToBounds即可.            自适应图片宽高比例.通过UIViewContentModeScaleAsp ...

  5. 【CSS】318- CSS实现宽高等比自适应容器

    点击上方"前端自习课"关注,学习起来~ 在最近开发移动端页面,遇到这么一个情况:当页面宽度 100% 时,高度为宽度一半,并随手机宽度变化依然是一半. 于是我们就需要实现一个宽度自 ...

  6. 漂亮的自适应宽度的多色彩CSS图片按钮

    一.素材               二.效果 三.CSS *{padding:0;margin:0} /*----------------------------------- 自适应宽度图片按钮 ...

  7. 从零开始学习前端开发 — 7、CSS宽高自适应

    一.宽度自适应 语法:width:100%; 注: a)块状元素的默认宽度为100% b) 当给元素设置宽度为100%时,继承父元素的宽度 c) 通常使用宽度自适应实现通栏效果 二.高度自适应 语法: ...

  8. 纯Css实现Div高度根据自适应宽度(百分比)调整

    在如今响应式布局的要求下,很多能自动调整尺寸的元素能够做到高宽自适应,如img,通过{width:50%;height:auto;}实现图片高度跟随宽度比例调整. 然而,用的最多的标签一哥Div却不能 ...

  9. 使用CSS让元素尺寸缩小时保持宽高比例一致

    CSS中有一个属性padding对元素宽度存在依存关系.如果一个元素的 padding属性以百分比形式表示,padding 的大小是以该元素自身宽度为参照的. 若想要元素尺寸变化时,宽高比例不变,可以 ...

随机推荐

  1. linux-history显示历史命令执行时间

    vim  ~/.bashrc 或者 ~/.bash_profile 增加:export HISTTIMEFORMAT="%F %T  " 一次会话的话先执行 然后使用history ...

  2. css中input框不可点击+首行缩进

    Css 1)text-indent::首行缩进 2)disabled="true"设置input框不可以点击 3)Css:xx!important:声明提前优先级最高..!impo ...

  3. Windows Service 2012 R2 下如何建立ftp服务器

    1.首先在本地机器上创建一个用户!这些用户是用来登录到FTP的!我的电脑右键->管理->本地用户和组->用户->“右键”新建用户->输入用户名和密码再点创建就行了! 2. ...

  4. Confluence 6 workbox 包含从 Jira 来的通知

    如果你的 Confluence 站点链接了一个 Jira 应用,你可以包含从 Jira 应用来的通知,例如 Jira 软化或 Jira 服务器桌面. 希望包含有从 Jira 应用来的通知: 你的 Ji ...

  5. SELinux简介(转载)

    几乎可以肯定每个人都听说过 SELinux (更准确的说,尝试关闭过),甚至某些过往的经验让您对 SELinux 产生了偏见.不过随着日益增长的 0-day 安全漏洞,或许现在是时候去了解下这个在 L ...

  6. 【python】升级pip后报错解决pkg_resources.DistributionNotFound: The 'pip==7.1.0' distribution was not found and is required by the application

    原本使用pip版本为7.1.0,后升级至9.0.1 之后使用pip list提示pkg_resources.DistributionNotFound: The 'pip==7.1.0' distrib ...

  7. react 中子组件调用父组件的方法

    1.在父组件中定义方法,并绑定在子组件上 // 在子组件中调用父组件中的方法 import React,{Component} from 'react'; import Child from './c ...

  8. 改变html结构可以实现优先加载

    我们通过一个实例来看一下: 本编程题目,完成一个混合布局的编写吧!最终效果如下图: 任务 任务1:完成顶部(top).底部(foot)宽度自适应 任务2:中间分为2两栏,其中,左侧(left)宽度为2 ...

  9. 开启或停止website

    1.添加:Microsoft.Web.Administration 2.代码: static void Main(string[] args) { var server = new ServerMan ...

  10. Python面向对象 三大特性 综合案例

    class Animal: # 所用的知识 Animal类的封装 -> Dog类,Cat类,Person类的继承->多态 # 把所有的共同属性和方法封装在一个公有类里面让子类继承父类的方法 ...