本文实例讲述了JS拖动鼠标画出方框实现鼠标选区的方法。分享给大家供大家参考。具体如下:
相当实用的一个JS技巧,拖动鼠标可画出一个方框,可作为一个选区的功能,可以用来画流程图,设计草图什么的,也可以作为上传头像时裁切选择图片,在头像裁切中使用广泛,鼠标在图片上拖动拉出一个方框,这个方框就代表着选区的功能,目前网页上已流行的一种操作。主要是依赖于JavaScript代码来实现。
运行效果如下图所示:
具体代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh" lang="zh"> <head> <title>鼠标拖动画矩形</title> <meta http-equiv="content-type" content="text/html;charset=utf-8"> <style type="text/css"> body,h1,h2,h3,h4,h5,h6,hr,p,blockquote,dl,dt,dd,ul,ol,li,pre,form,fieldset,legend,button,input,textarea,th,td{margin:0;padding:0} html{color:#000;overflow-y:scoll;overflow:-moz-scrollbars-vertical} .div{position:absolute; border:1px dashed blue; width:0px; height:0px;left:0px; top:0px; overflow:hidden;} .retc{position:absolute; border:1px solid #CCCCCC; overflow:hidden; background:#EFEFEF} </style> </head> <body> </body> <script language = "javascript"> var wId = "w"; var index = 0; var startX = 0, startY = 0; var flag = false; var retcLeft = "0px", retcTop = "0px", retcHeight = "0px", retcWidth = "0px"; document.onmousedown = function(e){ flag = true; try{ var evt = window.event || e; var scrollTop = document.body.scrollTop || document.documentElement.scrollTop; var scrollLeft = document.body.scrollLeft || document.documentElement.scrollLeft; startX = evt.clientX + scrollLeft; startY = evt.clientY + scrollTop; index++; var div = document.createElement("div"); div.id = wId + index; div.className = "div"; div.style.marginLeft = startX + "px"; div.style.marginTop = startY + "px"; document.body.appendChild(div); }catch(e){ //alert(e); } } document.onmouseup = function(){ try{ document.body.removeChild($(wId + index)); var div = document.createElement("div"); div.className = "retc"; div.style.marginLeft = retcLeft; div.style.marginTop = retcTop; div.style.width = retcWidth; div.style.height = retcHeight; document.body.appendChild(div); }catch(e){ //alert(e); } flag = false; } document.onmousemove = function(e){ if(flag){ try{ var evt = window.event || e; var scrollTop = document.body.scrollTop || document.documentElement.scrollTop; var scrollLeft = document.body.scrollLeft || document.documentElement.scrollLeft; retcLeft = (startX - evt.clientX - scrollLeft > 0 ? evt.clientX + scrollLeft : startX) + "px"; retcTop = (startY - evt.clientY - scrollTop > 0 ? evt.clientY + scrollTop : startY) + "px"; retcHeight = Math.abs(startY - evt.clientY - scrollTop) + "px"; retcWidth = Math.abs(startX - evt.clientX - scrollLeft) + "px"; $(wId + index).style.marginLeft = retcLeft; $(wId + index).style.marginTop = retcTop; $(wId + index).style.width = retcWidth; $(wId + index).style.height = retcHeight; }catch(e){ //alert(e); } } } var $ = function(id){ return document.getElementById(id); } </script> </html>
希望本文所述对大家的javascript程序设计有所帮助。
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索js
, 拖动鼠标
, 画出方框
鼠标选区
javascript鼠标拖动、ps拖动选区、ps怎么拖动选区、ps如何拖动选区、javascript拖动div,以便于您获取更多的相关知识。
时间: 2024-09-14 20:03:09