原生js手机相册制作 原创

饭后茶余.写着玩的,适用于初学者做参考的

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		
		<title>原生js手机相册制作</title>
	</head>
	<style type="text/css">
		*{
			padding: 0;
			margin: 0;
		}	
		body{
			font-size: 0.15rem;
			background: #000;
		}	
		a{
			color: #fff;
			text-decoration: none;
		}
		header{
			position: relative;
			width: 3rem;
			height: 0.45rem;
			text-align: center;
			color: #fff;
			line-height: 0.45rem;
			background: #5d5959;
		}
		header a{
			position: absolute;
			top: 0.15rem;
			width: 0.5rem;
			height: 0.25rem;
			box-shadow: 0 0 5px #000;
			line-height: 0.25rem;
			border-radius: 5px;
		}
		header a:nth-child(1){
			display: none;
			left: 15px;
		}
		
		header a:nth-child(2){
			right: 15px;
		}
		section{
			position: relative;
		}
		ul li{
			position:absolute;
			list-style: none;
			width: 1rem;
			height: 1rem;
			border:1px solid #000;
			box-sizing: border-box;
			background-size: 100% 100%;
			transition: left 0.5s, top 0.5s;
		}
		
		footer{
			position: fixed;
			left: 0;
			bottom: 0;
			width: 3rem;
			height: 0.45rem;
			line-height: 0.45rem;
			text-align: center;
			background: #5d5959;
		}
		footer a{
			float: left;
			width: 1.5rem;
			height: 0.45rem;
		}
		footer a:nth-child(1){
			border-right: 1px solid #ccc;
			box-sizing: border-box;
		}
		
	</style>
	<body>
		<header>
			<a href="javascript:;" class="btn">删除</a>
			手机相册
			<a href="javascript:;" class="btn">选择</a>
		</header>
		<section>
			<ul id="content">
				
			</ul>
		</section>
		<footer>
			<a href="javascript:;">手机相册</a>
			<a href="javascript:;">所有相册</a>
		</footer>
	</body>
	<script type="text/javascript">
		function photos(){
			this.removes = [];//存储删除对象
			this.html = '';	//定义添加对象li
			this.mark = true;	//开关
			this.dom = document.getElementsByTagName('html')[0];
			this.oUl = document.getElementById('content');
			this.aLi = this.oUl.getElementsByTagName('li');
			this.btn = document.getElementsByClassName('btn');
			this.init = function(){
				//设置移动端默认
				this.dom.style.fontSize = document.documentElement.clientWidth/3 + 'px';
				window.onresize = function(){
					this.dom.style.fontSize = document.documentElement.clientWidth/3 + 'px';
				}
				//添加对象
				for (var i =1;i<=16; i++) {
					this.html += '<li style="background-image: url(images/'+i+'.jpg);"></li>';
				}
				
				this.oUl.innerHTML = this.html;
				this.positions();
				//给选择按钮监听触摸结束事件,模拟点击
				this.btn[1].addEventListener('touchend',this.selects,false);	
				//删除按钮功能
				this.btn[0].addEventListener('touchend',this.removeLi,false);
			}
			
			this.positions = function(){
				for (var i=0,l=this.aLi.length;i<l;i++) {
					this.aLi[i].style.left = i%3 + 'rem';
					this.aLi[i].style.top =  Math.floor(i/3) +'rem';
				}
			}
			
			
			this.selects = function (){
				if(photos.mark){
					photos.btn[1].innerHTML = '取消';
					//让每个li拥有选择功能
					for (var i=0,l=photos.aLi.length;i<l;i++) {
						photos.aLi[i].index = i;
						photos.aLi[i].onOff = true;//添加一个自定义开关属性
						photos.aLi[i].addEventListener('touchend',photos.selectLi,false);
					}
				}else{
					photos.btn[1].innerHTML = '选择';
					photos.btn[0].style.display = 'none';
					for (var i=0,l=photos.aLi.length;i<l;i++) {
						photos.aLi[i].style.opacity = 1;
						photos.aLi[i].removeEventListener('touchend',photos.selectLi,false);
					}
				}
				photos.mark = !photos.mark; //开关取反
			}
			
			//选择li功能
			this.selectLi = function (){
				if(this.onOff){
					this.style.opacity = 0.3;
					photos.btn[0].style.display = 'block';
					photos.removes.push(this.index); //需要删除的对象
				}else{
					this.style.opacity = 1;
					photos.removes.pop(this.index);
				}
				this.onOff = !this.onOff;
			}
			
			this.removeLi = function (){
				photos.removes = photos.removes.sort(function(a,b){
					 return a - b;
				})				
				//删除并返回最后一个元素
				while(photos.removes.length){
					photos.oUl.removeChild(photos.aLi[photos.removes.pop()]);
				}
				photos.mark = false;
				photos.selects();
				photos.positions();
				photos.btn[0].style.display = 'none';
			}
			
		}
		
		var photos = new photos();
			photos.init();
	</script>
</html>

原生js手机相册制作,面向对象开发过程,适合初学者学习

效果预览点击   http://hao2013.cn/photos/index.html 

pc端请换成手机模式预览,或者直接用手机浏览器打开

demo下载地址:链接:http://pan.baidu.com/s/1o8qNJuE 密码:k78p

参考:http://hao2013.cn/?id=26

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要实现在手机端上传身份证并提交表单的功能,可以使用原生 JavaScript编写以下代码: HTML部分: ``` <form id="myForm" enctype="multipart/form-data"> <input type="file" accept="image/*" capture="camera" id="idcard" name="idcard" /> <button type="button" onclick="submitForm()">提交</button> </form> ``` JavaScript部分: ```javascript function submitForm() { let formData = new FormData(); // 创建FormData对象 let fileInput = document.getElementById("idcard"); if (fileInput.files.length > 0) { let file = fileInput.files[0]; formData.append("idcard", file); // 将文件添加到FormData对象中 } // 发送表单数据 let xhr = new XMLHttpRequest(); xhr.open("POST", "submit_url", true); xhr.onreadystatechange = function() { if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) { // 请求成功的处理代码 console.log(xhr.responseText); } }; xhr.send(formData); // 发送FormData对象 } ``` 以上代码中,表单中的文件输入框为`<input type="file">`,通过`capture="camera"`属性可以直接调用手机相机进行拍照或选择相册中的图片。使用`accept="image/*"`限制只能选择图片文件。点击提交按钮时,会调用`submitForm`函数。 函数内部,首先创建一个`FormData`对象用于存储表单数据。然后,从文件输入框中获取选中的文件对象,并将其添加到`FormData`对象中,使用`append`方法。在`xhr`对象上调用`open`方法打开一个POST请求,并设置请求地址。然后,通过`onreadystatechange`函数监听请求状态和响应。最后,使用`send`方法发送`FormData`对象。 你需要将"submit_url"替换为实际的提交地址。在请求成功时,可以根据需要处理响应数据。通过控制台打印`xhr.responseText`,你可以查看服务器返回的内容。 请注意,上传文件可能需要服务器端的相应配置。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狼丶宇先森

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值