• JavaScript常用项目(更新至19.11.17)


    目录

    项目一:鼠标拖动方块

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

    项目三:实现滚播图

    项目四:   本地数据记事本


    项目一:鼠标拖动方块

    代码:

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="utf-8">
    		<style type="text/css">
    			body,html{
    				 100%;
    				height: 100%;
    			}
    			#d1{
    				 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{
    				 800px;
    			}
    			.form{				
    				list-style: none;
    				position: relative; //和下面absolute实现将所有图片(列表)重叠起来
    			}
    			.line{
    				 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{
    				 500px;
    				margin: 0px auto; /*整体左右居中*/
    				background: white;
    			}
    			button{
    				cursor: pointer; /*鼠标放上button后出现小手*/
    			}
    			.inputNode{
    				 100%;
    				height: 30px;
    				box-sizing: border-box; /*解决加入文本框后边缘不齐*/
    				margin-bottom: 4px;
    			}
    			.addBtn{
    				 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>

    运行截图:

  • 相关阅读:
    第二冲刺阶段个人博客
    单元测试
    软件工程个人总结
    梦断代码阅读笔记03
    梦断编码阅读笔记02
    梦断代码阅读笔记01
    padding margin
    MVC模式在Java Web应用程序中的实例分析
    小问题
    MVC理解
  • 原文地址:https://www.cnblogs.com/ldu-xingjiahui/p/12594052.html
Copyright © 2020-2023  润新知