点击之后上传图片到页面 input type="file" 样式

时间:2022-07-03 12:04:13

<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test js</title>
<style>
#pic{
width:100px;
height:100px;
border-radius:50% ;
margin:20px auto;
cursor: pointer;
}
.upfile{
/*opacity: 0;*/
}
.file {
position: relative;
display: inline-block;
background: #D0EEFF;
border: 1px solid #99D3F5;
border-radius: 4px;
padding: 4px 12px;
overflow: hidden;
color: #1E88C7;
text-decoration: none;
text-indent: 0;
line-height: 20px;
}
.file input {
position: absolute;
font-size: 100px;
right: 0;
top: 0;
opacity: 0;
}
.file:hover {
background: #AADFFD;
border-color: #78C3F3;
color: #004974;
text-decoration: none;
}

</style>
</head>
<script src="http://code.jquery.com/jquery-latest.js"></script>

<body>

<div class="img_show img_show1">
<p class="p-flex">
<span class="vertical-blue"></span><span class="left-A" >水印设置</span>
<img src="" alt="" class="img-big" width="103" height="103">
<img src="" alt="" class="img-centre" width="103" height="103">
<img src="" alt="" class="img-small" width="103" height="103" >
</p>
<p class="text-blue">
<a href="javascript:;" class="file">上传水印
<input type="file" class="upfile" value="更换" class="changes" accept="image/*">
</a>
</p>
</div>

</body>
<script>
upfile = function(){
$('.img_show').each(function(){
var $this = $(this),
btn = $this.find('.upfile'),
img = $this.find('img');
btn.on('change',function(){
var file = $(this)[0].files[0],
imgSrc = $(this)[0].value,
url = URL.createObjectURL(file);
if (!/\.(jpg|jpeg|png|JPG|PNG|JPEG)$/.test(imgSrc)){
alert("请上传jpg或png格式的图片!");
return false;
}else{
img.attr('src',url);
img.css({'opacity':'1'});
}
});
});
}();
</script>
</html>