ldu-xingjiahui

目录

项目一:鼠标拖动方块

项目二:网页显示键入字母

项目三:实现滚播图

项目四:   本地数据记事本


项目一:鼠标拖动方块

代码:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<style type="text/css">
			body,html{
				width: 100%;
				height: 100%;
			}
			#d1{
				width: 200px;
				height: 200px;
				background-color: aquamarine;
				position: absolute;
			}
		</style>
		<title></title>
	</head>
	<body>
		<div id="d1">
			
		</div>
		<script type="text/javascript">
			var flag=false;
			var div1=document.getElementById(\'d1\');
			var moveDiv1=function(x,y){
				if(flag){
					div1.style.left=x-100+\'px\';
					div1.style.top=y-100+\'px\';
				}
			}
			document.body.addEventListener(\'mousedown\',function(){
				flag=true;
			})
			document.body.addEventListener(\'mouseup\',function(){
				flag=false;
			})
			document.body.addEventListener(\'mousemove\',function(result){
				moveDiv1(result.clientX,result.clientY);
			})
		</script>
	</body>
</html>

运行截图:

 

项目二:网页显示键入字母

代码:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script type="text/javascript">
			var str="";
			document.body.addEventListener(\'keypress\',function(result){
				str+=result.key;
				document.body.innerHTML=str;
			});
		</script>
	</body>
</html>

运行截图: 

项目三:实现滚播图

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>图片滚动</title>
		<style type="text/css">
			.wrap{
				width: 800px;
			}
			.form{				
				list-style: none;
				position: relative; //和下面absolute实现将所有图片(列表)重叠起来
			}
			.line{
				width: 800px;
				height: 300px;
				position: absolute;
			}
			.line:nth-child(1){ //便于通过css来控制多个相同class属性的标签
				background: mediumvioletred;
			}
			.line:nth-child(2){
				background: blueviolet;
			}
			.line:nth-child(3){
				background: greenyellow;
			}
			.line:nth-child(4){
				background: goldenrod;
			}
			.line:nth-child(5){
				background: aquamarine;
			}
			.line.active{ //谁在最上面显示谁
				z-index: 10;
			}
		</style>
	</head>
	<body>
		<div id="" class="wrap">
			<ul class="form">
				<li class="line">1</li>
				<li class="line">2</li>
				<li class="line">3</li>
				<li class="line">4</li>
				<li class="line">5</li>
			</ul>
		</div>
		<script type="text/javascript">
			var line=document.getElementsByClassName(\'line\');
			var index=0; //通过index来控制现在是显示的第几张图片
			line[index].className="line active";
			var changeLine=function(n){
				index++;
				console.log(index);
				for(var i=0;i<line.length;i++){
					line[i].className="line";
				}
				line[index%line.length].className="line active";
			}
			setInterval(changeLine,1000);
		</script>
	</body>
</html>

运行截图:

项目四、本地数据记事本

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title></title>
		<style type="text/css">
			body{
				background: #e3e3e3;
			}
			.wrap{
				width: 500px;
				margin: 0px auto; /*整体左右居中*/
				background: white;
			}
			button{
				cursor: pointer; /*鼠标放上button后出现小手*/
			}
			.inputNode{
				width: 100%;
				height: 30px;
				box-sizing: border-box; /*解决加入文本框后边缘不齐*/
				margin-bottom: 4px;
			}
			.addBtn{
				width: 100%;
				height: 30px;
			}
		</style>
	</head>
	<body>
		<div class="wrap">
			<ul id="list"></ul>
			<div class="addwrap">
				<input type="text" id="addTitle" class="inputNode" placeholder="输入标题"/>
				<input type="text" id="addContent" class="inputNode" placeholder="输入内容"/> //初始化input的内容 不同于value
			</div>
			<button type="button" id="addBtn">添加</button>
		</div>
		<script type="text/javascript">
			var ul=document.getElementById(\'list\'); //如果是根据ClassName返回的是数组
			var addBtn=document.getElementById(\'addBtn\');
			var inputTitle=document.getElementById(\'addTitle\');
			var inputContent=document.getElementById(\'addContent\');
			var list=[];
			var tpl=\'<li class="item"><p class="title">{%temp1%}</p><p class="content">{%temp1%}</p><button type="button" class="changebtn">修改</button></li>\';
                        //定义模板
			var filllist=function(){
				if(list.length==0){
					ul.innerHTML=\'<li>没有内容</li>\'; //套入模板
					return
				}
				var str=\' \';
				for(var i=0;i<list.length;i++){
					str+=tpl.replace(\'{%temp1%}\',list[i].title).replace(\'{%temp1%}\',list[i].content);
				}
				document.getElementById(\'list\').innerHTML=str;
			}
			addBtn.addEventListener(\'click\',function(){ //添加时事件 (事件和运行函数不矛盾)
				if(!inputTitle.value||!inputTitle.value){
					alert(\'存在空白项,请重新输入!\');
					return;
				}
				var obj={ //定义数组 (键值对)
					title:inputTitle.value,
					content:inputContent.value,
				}
				list.push(obj);
				inputTitle.value=\'\';
				inputContent.value=\'\';
				filllist();
				savaList();
			});
			
			var savaList=function(){ 
				localStorage.setItem(\'localData\',JSON.stringify(list)); //强制转化为字符串形式
			}
			
			var getList=function(){
				var localList=localStorage.getItem(\'localData\');
				localList=JSON.parse(localList); //解析字符串
				return localList;
			}
			var main=function(){
				if(!localStorage.length){
					list=[];
				}else{
					list=getList();
				}
				filllist();
			}
			main();
		</script>
	</body>
</html>

运行截图:

分类:

技术点:

相关文章:

  • 2021-07-05
  • 2021-12-03
  • 2022-12-23
  • 2021-07-30
  • 2021-12-19
  • 2022-12-23
  • 2022-12-23
  • 2021-09-14
猜你喜欢
  • 2021-10-02
  • 2021-08-10
  • 2022-12-23
  • 2022-02-01
  • 2022-12-23
  • 2021-12-12
  • 2021-07-20
相关资源
相似解决方案