现在的位置: 首页 > web前端 > 正文

js实现div色块拖动录制

2020年02月18日 web前端 ⁄ 共 1753字 ⁄ 字号 评论关闭

本文实例为大家分享了js实现p色块拖动录制的具体代码,供大家参考,具体内容如下

描述:

实现一个p50*50的色块,拖动它生成一个轨迹,松手后,这个p会重复你刚才拖动的这个路径。

效果:

代码:

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <style> p { width: 50px; height: 50px; background-color: deepskyblue; position: absolute; border: 2px solid #656565; } </style> <script src="js/Method.js"></script></head><body><p></p> <script> var elem; var state=0;//当前的状态 初始0——拖动录制1——回放2 var arr=[];//存放我们的行走路径的 数组 var list=[];//存放我们的行走路径的 数组 var index=0; init(); function init() { elem=document.querySelector("p"); Method.dragElem(elem); elem.addEventListener("mousedown",mouseHandler); elem.addEventListener("mouseup",mouseHandler); setInterval(animation,16); } function mouseHandler(e) {//当前的状态 初始0——拖动录制1——回放2 if(e.type==="mousedown"){//鼠标按下 state=1; }else if(e.type==="mouseup"){//鼠标抬起 createElemShadow(); state=2; } } function createElemShadow() { var bool=false; if(list.length===0) bool=true; for(var i=0;i<5;i++){ if(bool)list.push(elem.cloneNode(false)); list[i].style.opacity=1-i*0.2; document.body.appendChild(list[i]) } } function animation() { if(!state) return; if(state===1){ record(); }else if(state===2){ play(); } } function record() { var rect=elem.getBoundingClientRect(); arr.push({x:rect.x,y:rect.y}); } function play() { /* if(index>=arr.length-1){ state=0; return; }*/ index++; var point=arr[index]; if(point){ elem.style.left=point.x+"px"; elem.style.top=point.y+"px"; elem.style.backgroundColor=Method.pColor(); } var bool=false; for(var i=0;i<list.length;i++){ if(!arr[index-i*2]) continue; list[i].style.left=arr[index-i*2].x+"px"; list[i].style.top=arr[index-i*2].y+"px"; list[i].style.backgroundColor=Method.pColor(); bool=true; } if(!bool){ state=0; for(var j=0;j<list.length;j++){ list[j].remove(); } } } </script></body></html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

以上就上有关js实现div色块拖动录制的全部内容,学步园全面介绍编程技术、操作系统、数据库、web前端技术等内容。

抱歉!评论已关闭.