js上传头像

46 篇文章 0 订阅
41 篇文章 1 订阅
<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
		<title></title>
		<script>
			window.onload = function() {
				getRem(750, 100)
			};
			window.onresize = function() {
				getRem(750, 100)
			};

			function getRem(pwidth, prem) {
				var html = document.getElementsByTagName("html")[0];
				var oWidth = document.body.clientWidth || document.documentElement.clientWidth;
				html.style.fontSize = oWidth / pwidth * prem + "px";
			}
		</script>
		<style type="text/css">
			button {
				width: 3rem;
				height: 1rem;
				font-size: 0.3rem;
			}
			/*半透明膜背景*/
			
			.shareWay {
				background: #FFFFFF;
				display: flex;
				flex-direction: column;
				height: 4rem;
				/* justify-content: center; */
				align-items: center;
				padding-top: 0.88rem;
			}
			
			.am-share2 {
				font-size: 14px;
				border-radius: 0;
				bottom: 0;
				left: 0;
				position: fixed;
				text-align: center;
				-webkit-transform: translateX(100%);
				-ms-transform: translateX(100%);
				transform: translateY(100%);
				-webkit-transition: -webkit-transform 300ms;
				transition: transform 300ms;
				width: 100%;
				z-index: 99999999;
				background: #fff;
			}
			
			.am-modal-active {
				-webkit-transform: translateX(0);
				-ms-transform: translateX(0);
				transform: translateX(0)
			}
			
			.am-share-title {
				background-color: #f8f8f8;
				border-bottom: 1px solid #fff;
				border-top-left-radius: 2px;
				border-top-right-radius: 2px;
				color: #555;
				font-weight: 400;
				margin: 0 10px;
				padding: 10px 0 0;
				text-align: center;
			}
			
			.am-share-title::after {
				border-bottom: 1px solid #dfdfdf;
				content: "";
				display: block;
				height: 0;
				margin-top: 10px;
				width: 100%;
			}
			
			.am-share-footer {
				margin: 10px;
			}
			
			.am-share-footer .share_btn {
				color: #555;
				display: block;
				width: 100%;
				background-color: #e6e6e6;
				border: 1px solid #e6e6e6;
				border-radius: 0;
				cursor: pointer;
				font-size: 16px;
				font-weight: 400;
				line-height: 1.2;
				padding: 0.625em 0;
				text-align: center;
				transition: background-color 300ms ease-out 0s, border-color 300ms ease-out 0s;
				white-space: nowrap;
				font-family: "微软雅黑";
			}
			
			.am-share-sns {
				background-color: #f8f8f8;
				border-radius: 0 0 2px 2px;
				margin: 0 10px;
				padding-top: 15px;
				height: auto;
				zoom: 1;
				overflow: auto;
			}
			
			.am-share-sns li {
				margin-bottom: 15px;
				display: block;
				float: left;
				height: auto;
				width: 25%;
			}
			
			.am-share-sns a {
				color: #555;
				display: block;
				text-decoration: none;
			}
			
			.am-share-sns span {
				display: block;
			}
			
			.am-share-sns li i {
				background-position: center 50%;
				background-repeat: no-repeat;
				background-size: 36px 36px;
				background-color: #ccc;
				color: #fff;
				display: inline-block;
				font-size: 18px;
				height: 36px;
				line-height: 36px;
				margin-bottom: 5px;
				width: 36px;
			}
			
			.am-share-sns .share-icon-weibo {
				background-image: url();
			}
			
			.sharebg {
				background-color: rgba(0, 0, 0, 0.68);
				bottom: 0;
				height: 100%;
				left: 0;
				opacity: 0;
				position: fixed;
				right: 0;
				top: 0;
				width: 100%;
				z-index: 999999;
				display: none;
			}
			
			.sharebg-active {
				opacity: 1;
				display: block;
			}
			
			.TakingPictures,
			.ChoosePhoto,
			.cancel {
				width: 6.9rem;
				height: 0.88rem;
				line-height: 0.88rem;
				text-align: center;
				border-radius: 0.44rem;
				border: 1px solid rgba(0, 0, 0, 1);
				margin-bottom: 0.3rem;
				font-size: 0.36rem;
			}
			
			.paizhao {
				position: absolute;
				top: 0.9rem;
				height: 0.9rem;
				opacity: 0;
			}
		</style>
	</head>

	<body>
		<p>
			<button onclick="toshare()">点我,可以是个图片奥</button>
		</p>

		<!-- 半透膜背景 -->
		<div class="am-share2">
			<div class="shareWay">
				<div class="TakingPictures" style="font-weight: bold;">从相册选择</div>
				<div class="cancel share_btn">
					取消
				</div>
				<input type="file" accept="image/*;" id="file" class="paizhao">

			</div>
		</div>
	</body>
	<script type="text/javascript">
		// 弹出遮罩层
		$(".share").click(function() {
			$("transition").css("display", "block");
		})
		$(".cancel").click(function() {
			$("transition").css("display", "none");
		})

		function toshare() {
			$(".am-share2").addClass("am-modal-active");
			if($(".sharebg").length > 0) {
				$(".sharebg").addClass("sharebg-active");
			} else {
				$("body").append('<div class="sharebg"></div>');
				$(".sharebg").addClass("sharebg-active");
			}
			$(".sharebg-active,.share_btn").click(function() {
				$(".am-share2").removeClass("am-modal-active");
				setTimeout(function() {
					$(".sharebg-active").removeClass("sharebg-active");
					$(".sharebg").remove();
				}, 300);
			});

		}

		//  上传头像
		document.getElementById('file').addEventListener('change', function() {
			var reader = new FileReader();
			reader.onload = function(e) {
				compress(this.result);
			};
			reader.readAsDataURL(this.files[0]);
		}, false);

		var compress = function(res) {
			var img = new Image(),
				maxH = 160;
			img.onload = function() {
				var cvs = document.createElement('canvas'),
					ctx = cvs.getContext('2d');
				if(img.height > maxH) {
					img.width *= maxH / img.height;
					img.height = maxH;
				}
				cvs.width = img.width;
				cvs.height = img.height;
				ctx.clearRect(0, 0, cvs.width, cvs.height);
				ctx.drawImage(img, 0, 0, img.width, img.height);
				var dataUrl = cvs.toDataURL('image/jpeg', 0.6);
				// 上传略
				console.log(dataUrl)
				$.ajax({
					type: "post",
					data: {
						file: dataUrl
					},
					url: "{:U('User/uploadHead')}",
					dataType: 'json',
					success: function(data) {
						console.log(data)
						let $info = data.info
						if(data.status == 1) {
							$(".photo").attr('src', dataUrl)
							window.location.reload()
						} else if(data.status == 2) {
							// layer.open({
							//     type: 1,
							//     anim: 'up',
							//     content: $info,
							// });
							alert($info)
							$(".sharebg-active").css('display', 'none')
							$(".am-share2").css('display', 'none')
						}
					}
				});
			}
			img.src = res;
		}
	</script>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Element Plus 是 Vue 3 的 UI 组件库,它提供了一个简洁易用的界面构建工具。关于上传头像的功能,Element Plus 的 `el-upload` 组件可以用来实现图片上传。以下是一个基本的使用步骤: 1. 引入组件:首先,你需要在你的项目安装并引入 `ElUpload` 组件。在你的入口文件或组件的 `import` 块添加: ```js import { ElUpload } from 'element-plus'; ``` 2. 定义组件:在模板使用 `ElUpload`,设置一些基本配置,如上传按钮、上传路径、文件格式等: ```html <el-upload :action="uploadUrl" // 你的服务器接收图片的URL :on-change="handleChange" // 上传成功后的回调 :file-list="fileList" // 存储已选择文件的列表 :accept="'.jpg, .png'" // 可接受的文件类型 :auto-upload="false" // 是否自动上传,通常为 false,以便于控制 > <el-button slot="trigger">选择图片</el-button> <div slot="tip">点击上传</div> </el-upload> ``` 3. 处理上传事件:在组件的 script 部分,定义 `handleChange` 方法处理文件上传和状态更新: ```js export default { data() { return { uploadUrl: 'your-api-url', // 上传到服务器的接口地址 fileList: [], // 用于存储上传的图片信息 }; }, methods: { handleChange(file) { if (file.status === 'success') { // 处理上传成功的逻辑,比如更新文件列表,显示预览等 this.fileList.push({ name: file.name, url: file.response, // 服务器返回的新图片URL thumbUrl: file.url, // 预览图URL }); } }, }, }; ``` 记得替换 `uploadUrl` 为你实际的服务器接口地址,并根据需求定制其他上传参数和回调行为。如果需要额外功能,如进度条、限制上传大小等,可以在 `el-upload` 的选项添加相应的属性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值