<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>rect sample</TITLE>
<script Language="javascript">
var startX,startY,endX,endY;
var dragFlag;function doMouseDown(){
dragFlag = 0;
startX = event.clientX;
startY = event.clientY;
document.all.rect.style.pixelLeft = startX;
document.all.rect.style.pixelTop = startY;
document.all.rect.style.pixelHeight = 0;
document.all.rect.style.pixelWidth = 0;
}function doMouseUp(){
dragFlag = 1;
}function doMouseMove(){
if(dragFlag == 0){
endX = event.clientX;
endY = event.clientY;
nowHeight = endY - startY;
nowWidth = endX - startX;
if(nowHeight<0){
document.all.rect.style.pixelTop = endY;
nowHeight = -1*nowHeight;
}
if(nowWidth<0){
document.all.rect.style.pixelLeft = endX;
nowWidth = -1*nowWidth;
} document.all.rect.style.pixelHeight = nowHeight;
document.all.rect.style.pixelWidth = nowWidth;
}
}document.onmousemove = doMouseMove;
document.onmousedown = doMouseDown;
document.onmouseup = doMouseUp;</script>
<div id="rect" style="position:absolute;left:0px; top:0px;width:0;height:0;border-top: 1px dotted #999999;border-left: 1px dotted #999999;border-right: 1px dotted #999999;border-bottom: 1px dotted #999999;">
</div>
</HEAD><BODY>
</BODY>
</HTML>
注:以上代码只是雏形,实现了拖动矩形的功能,你可以根据自己的需要进行改动。
<HTML>
<HEAD>
<TITLE>rect sample</TITLE>
<script Language="javascript">
var startX,startY,endX,endY;
var dragFlag;function doMouseDown(){
dragFlag = 0;
startX = event.clientX;
startY = event.clientY;
document.all.rect.style.pixelLeft = startX;
document.all.rect.style.pixelTop = startY;
document.all.rect.style.pixelHeight = 0;
document.all.rect.style.pixelWidth = 0;
}function doMouseUp(){
dragFlag = 1;
}function doMouseMove(){
if(dragFlag == 0){
endX = event.clientX;
endY = event.clientY;
nowHeight = endY - startY;
nowWidth = endX - startX;
if(nowHeight<0){
document.all.rect.style.pixelTop = endY;
nowHeight = -1*nowHeight;
}
if(nowWidth<0){
document.all.rect.style.pixelLeft = endX;
nowWidth = -1*nowWidth;
} document.all.rect.style.pixelHeight = nowHeight;
document.all.rect.style.pixelWidth = nowWidth;
}
}document.onmousemove = doMouseMove;
document.onmousedown = doMouseDown;
document.onmouseup = doMouseUp;</script>
<div id="rect" style="position:absolute;left:0px; top:0px;width:0;height:0;border-top: 1px dotted #999999;border-left: 1px dotted #999999;border-right: 1px dotted #999999;border-bottom: 1px dotted #999999;">
</div>
</HEAD><BODY>
</BODY>
</HTML>
注:以上代码只是雏形,实现了拖动矩形的功能,你可以根据自己的需要进行改动。
解决方案 »
- 省市县横向三级菜单如何实现,请高手指点?
- 拖拽时,页面下有一些控件变成阴影了,就是选中状态了,这个怎么解决啊?
- 菜鸟学JS遇到的简单问题
- 请问如何在一个function中调用另外一个function中定义产生的数组???
- 高分求够常用控件(列表控件、树目录控件等)的JavaScript控件包
- 单选按钮为什么不能选?
- 求!我要在<script language="JavaScript">写ASP代码
- return this.replace(/(^\s*)|(\s*$)/g, "")正则里面的符号是什么意思呢?
- 求iframe使用的demo,比较全面的,或者URL也行
- 怎么让textarea自动换行啊
- 如何实现在用户第二次登陆时,输入框会自动列出以前类似的文本,就如IE 地址栏的功能
- 打开页面时怎么用javascript脚本直接转到页面末尾
<head>
<STYLE>
v\:* { Behavior: url(#default#VML) }
</STYLE>
<script language="JavaScript">
<!--
var x=0;
var y=0;
var isMouseDown=false;
var newRect=null;document.onmousedown=function(){
x=window.event.clientX;
y=window.event.clientY;
isMouseDown=true;
if(newRect==null)
newRect=document.createElement('<v:rect filled="false" colors="30% yellow,70% green" style="position:absolute; top:'+ y +';left:'+ x +';width:1px;height:1px"></v:rect>');
else{
newRect.style.top=y;
newRect.style.left=x;
newRect.style.width="1px";
newRect.style.height="1px";
}
newRect.style.border="2px solid blue";//这里设置border的样式
}
document.onmousemove=function(){
if(isMouseDown==false) return;
document.body.insertBefore(newRect);
var mx=window.event.clientX;
var my=window.event.clientY;
if(mx>x){
newRect.style.width=mx-x;
}
else{
newRect.style.width=x-mx;
newRect.style.left=mx;
}
if(my>y){
newRect.style.height=my-y;
}
else{
newRect.style.height=y-my;
newRect.style.top=my;
}}
document.onmouseup=function(){
x=0;
y=0;
isMouseDown=false;
}//-->
</script>
</head>
<body onselectstart="return false;"></body>
</html>