点击上传图片

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>apply</title>

    <!-- 导入css文件 -->
    <link rel="stylesheet" type="text/css" href="index.css">

    <!-- 定义App端H5 -->
    <meta name="viewport" content="width=device-width,height=device-height,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">

</head>

<body>

    <div id="oDiv1">
		<!-- 都做为背景图 -->
        <label for="File1" style="display:block" id="Label1"></label>
        <input id="File1" type="file"  οnchange="func1(this)" style="display:none;" accept="image/jpeg,image/jpg,image/png">
       
        <input id="oIn" type="text" placeholder="请输入姓名" maxlength="5">
  
        <P id="Text1"></P>
    </div>
	
	<!-- 导入jq库 -->
    <script src="./jquery-3.2.1.js"></script>

    <!-- 导入js文件,放在html后面 -->
    <script src="index.js"></script>
</body>

</html>


CSS部分

	*{
			list-style: none;
			text-decoration: none;
			padding: 0;
			margin: 0;
		}
		::-webkit-input-placeholder {   //给input若提示更改字体颜色
			color: hotpink; 
		}
		body{
			background-image: url('../images/Bimg.jpg');
			background-repeat: no-repeat;
			background-size: 100%;
		}
		#Label1{
			width:60%;
			height: 250px;
			display: block;
			margin: 100px auto 0;
			background-image: url('../images/520_uploading.jpg');
			background-repeat: no-repeat;
			background-size: 100% auto;
		}
		


JS部分

if (typeof FileReader == 'undefined') {
			document.getElementById("Text1").InnerHTML = "<h1>当前浏览器不支持FileReader接口</h1>";
		}

		//选择图片,马上预览
		function func1(obj) {
			var file = obj.files[0];

			console.log(obj);
			console.log(file);
			console.log("file.size = " + (file.size / 1024).toFixed(2) + "M");  //file.size 单位为byte
			// alert("file.size = " + (file.size/1024).toFixed(2) + "M");
			var reader =new FileReader();

			//读取文件过程方法
			reader.onloadstart = function (e) {
				console.log("开始读取....");
			}
			reader.onprogress = function (e) {
				console.log("正在读取中....");
			}
			reader.onabort = function (e) {
				console.log("中断读取....");
			}
			reader.onerror = function (e) {
				console.log("读取异常....");
			}
			reader.onload = function (e) {
				console.log("成功读取....");

				$("#Label1").css({ "backgroundImage": "url(" + e.target.result + ")" });
			}

			reader.readAsDataURL(file);  //把选择的图片文件调用readAsDataURL方法,把图片数据转成base64字符串形式显示在页面上
		}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值