JS拖动鼠标画出方框实现鼠标选区的方法,js选区

本文实例陈述了JS拖动鼠标画出方框完毕鼠标选区的方法。分享给我们供我们参照他事他说加以考察。具体如下:

JS拖动鼠标画出方框完成鼠标选区的主意,js选区

正文实例呈报了JS拖动鼠标画出方框达成鼠标选区的艺术。共享给大家供我们参谋。具体如下:

一定实用的贰个JS技能,拖动鼠标可画出多少个四方,可作为叁个选区的作用,可以用来画流程图,设计草图什么的,也得以当作上传头像时裁切选拔图片,在头像裁切中选择大范围,鼠标在图纸上拖动拉出二个四方,那一个方框就表示着选区的职能,近年来网页三春流行的一种操作。首要是借助于JavaScript代码来贯彻。

运转作效果果如下图所示:

图片 1

切实代码如下:

<!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拖动鼠标画出方框完结鼠标选区的法子。分享给我们供我们参谋。具体如…

JS实现超简单的鼠标拖动作效果果,js完成鼠标拖动

本文实例叙述了JS实现超简单的鼠标拖动作效果果。分享给大家供我们参谋,具体如下:

此地运用尽恐怕短的JavaScript代码写三个JS拖动,函数本人2八十六个字符。。。假如不是挂念包容性和变量封装,还足以更加短点。

运行作效果果截图如下:

图片 2

在线演示地址如下:

现实代码如下:

<title>尽可能短的写一个JS拖动</title>
<body>
<div id="demo" style="width:100px; height:100px; position:absolute; background-color:silver;"></div>
<script>
function dragable(id){var d=document,o=d.getElementById(id),s=o.style,x,y,p='onmousemove';o.onmousedown=function(e){e=e||event;x=e.clientX-o.offsetLeft;y=e.clientY-o.offsetTop;d[p]=function(e){e=e||event;s.left=e.clientX-x+'px';s.top=e.clientY-y+'px'};d.onmouseup=function(){d[p]=null}}}
dragable("demo");
</script>

梦想本文所述对大家JavaScript程序设计有所支持。

本文实例陈述了JS达成超简单的鼠标拖动作效果果。分享给我们供我们参照他事他说加以考察,具体如下:
这里使…

正文实例汇报了JS完成超轻松的鼠标拖动作效果果。分享给我们供我们参照他事他说加以考察,具体如下:

<?xml version=”1.0″ encoding=”utf-8″?>
<s:Application xmlns:fx=””
xmlns:s=”library://ns.adobe.com/flex/spark”
xmlns:mx=”library://ns.adobe.com/flex/mx” >
<fx:Script>
<![CDATA[
import flash.events.MouseEvent;

一定实用的二个JS手艺,拖动鼠标可画出三个四方,可作为一个选区的意义,能够用来画流程图,设计草图什么的,也足以用作上传头像时裁切选取图片,在头像裁切中选用大范围,鼠标在图纸上拖动拉出多个四方,这些方框就表示着选区的功效,前段时间网页寒食流行的一种操作。主如果借助于JavaScript代码来贯彻。

那边运用尽或许短的JavaScript代码写二个JS拖动,函数自己2九十多个字符。。。假若不是思虑包容性和变量封装,还足以更加短点。

import mx.binding.utils.BindingUtils;
import mx.core.UIComponent;

运行作效果果如下图所示:

运作效果截图如下:

private var mX:Number;
private var mY:Number;
private var IsDown:Boolean=false;
private var sprite:Sprite;
private var uic:UIComponent;

图片 3

图片 4

public var lineSize:int;
[Bindable]
public var lineColor:int=0xff1100;

实际代码如下:

在线演示地址如下:

private function DrawLine():void{

<!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>

paper.addEventListener(MouseEvent.MOUSE_DOWN,downHandler);
paper.addEventListener(MouseEvent.MOUSE_MOVE,movelineHandler);
paper.addEventListener(MouseEvent.MOUSE_UP,upHandler);
BindingUtils.bindProperty(this,”lineColor”,cp,”selectedColor”);

可望本文所述对大家的javascript程序设计具备协助。

切实代码如下:

}
private function movelineHandler(e:MouseEvent):void{
if(IsDown){
var x:Number=paper.mouseX;
var y:Number=paper.mouseY;

你也许感兴趣的篇章:

  • Javascript贯彻鼠标框选操作
    不是点击采纳
  • 用javascript达成鼠标框选
  • js完毕鼠标点击文本框自动选中内容的法子
  • javascript兑现淡士林蓝的鼠标拖动选用框实例
  • JavaScript不难落成鼠标拖动选择功用
  • 当鼠标滑过文本框自动选中输入框内容的JS代码分享
  • 用javascript
    css达成GridView行背景观交替、鼠标划过行变色,点击行变色选中
  • javascript达成鼠标采用拖动或Ctrl选择拖动
  • JS实现鼠标框选效果总体实例
<title>尽可能短的写一个JS拖动</title>
<body>
<div id="demo" style="width:100px; height:100px; position:absolute; background-color:silver;"></div>
<script>
function dragable(id){var d=document,o=d.getElementById(id),s=o.style,x,y,p='onmousemove';o.onmousedown=function(e){e=e||event;x=e.clientX-o.offsetLeft;y=e.clientY-o.offsetTop;d[p]=function(e){e=e||event;s.left=e.clientX-x+'px';s.top=e.clientY-y+'px'};d.onmouseup=function(){d[p]=null}}}
dragable("demo");
</script>

paper.contentGroup.graphics.clear();
paper.contentGroup.graphics.lineStyle(lineSize,lineColor,1);

企望本文所述对大家JavaScript程序设计具备支持。

paper.contentGroup.graphics.moveTo(mX,mY);
paper.contentGroup.graphics.lineTo(x,y);

你也许感兴趣的小说:

  • JS响应鼠标点击达成三个滑块区间拖动作效果果
  • JS达成左右拖动改造内容展现区域大小的章程
  • js达成div拖动动画运维轨迹效果代码分享
  • avalon
    js完毕仿今日头条拖动图片排序
  • JS完结弹出浮动窗口(协理鼠标拖动和停业)实例详解
  • JS完结动态移动层及拖动浮层关闭的章程
  • JS完成可缩放、拖动、关闭和最小化的变型窗口完整实例
  • js用拖动滑块来支配图片大小的方法
  • Js可拖拽放大的层拖动特效完成情势
  • JS+CSS达成可拖动的弹出提醒框
  • js完结图片拖动改换各种附图
  • JS完毕的自定义网页拖动类

}
}

private function downHandler(e:MouseEvent):void{
IsDown=true;

mX=paper.mouseX;
mY=paper.mouseY;
}

private function upHandler(e:MouseEvent):void{
IsDown=false;
}

private function moveHandler(e:MouseEvent):void{
if(IsDown){

var x:Number = paper.mouseX;
var y:Number = paper.mouseY;
paper.contentGroup.graphics.clear();
paper.contentGroup.graphics.lineStyle(lineSize,lineColor,1);
paper.contentGroup.graphics.moveTo(mX,mY);
paper.contentGroup.graphics.lineTo(x,y);
mX=x;
mY=y;

}
}

private function doChange():void{
lineSize=int(sel.value);

}

]]>
</fx:Script>
<s:Panel title=”test” width=”894″ height=”537″>
<s:Panel width=”743″ x=”0″ y=”38″ height=”462″ title=”记事板”
fontFamily=”中易钟鼓文”>
<s:BorderContainer id=”paper” width=”741″ height=”429″ x=”0″
contentBackgroundColor=”#FFFFFF” chromeColor=”#120101″ y=”0″>
</s:BorderContainer>
</s:Panel>

<mx:ColorPicker x=”138″ y=”10″ width=”23″ height=”24″ id=”cp”
selectedColor=”{lineColor}”/>

<s:NumericStepper x=”169″ y=”10″ id=”sel” change=”doChange()”/>
<s:Button x=”252″ y=”9″ label=”直线” id=”line”
click=”DrawLine()”/>
</s:Panel>
</s:Application>

相关文章