源码网,源码论坛,源码之家,商业源码,游戏源码下载,discuz插件,棋牌源码下载,精品源码论坛

 找回密码
 立即注册
楼主: ttx9n

[JavaScript] javascript实现淡蓝色的鼠标拖动选择框实例

[复制链接]

7万

主题

861

回帖

32万

积分

论坛元老

Rank: 8Rank: 8

积分
329525
发表于 2015-5-9 09:14:27 | 显示全部楼层 |阅读模式
这篇文章主要介绍了javascript实现淡蓝色的鼠标拖动选择框,可实现鼠标拖动出现淡蓝色选择框的效果,涉及javascript鼠标事件及样式的操作技巧,需要的朋友可以参考下

本文实例讲述了javascript实现淡蓝色的鼠标拖动选择框。分享给大家供大家参考。具体实现方法如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
#rectBox
{
background:#CCFFFF;
border:2px solid #0099FF;
filter:alpha(opacity=30);
opacity:0.3;
position:absolute;
}
</style>
<head>
<title>一个鼠标选择框</title>
<script type="text/javascript">
function Rect()
{
this.doc = document.documentElement;
if(!this.doc) return;
this.startX = '';
this.startY = '';
this.rect = null;
rectSelf = this;
}
Rect.prototype.down = function(e)
{
var e = e?e:window.event;
rectSelf.startX = e.clientX?e.clientX + document.body.scrollLeft:e.pageX;
rectSelf.startY = e.clientY?e.clientY + document.body.scrollTop:e.pageY;
rectSelf.showRect(true);
}
Rect.prototype.up = function(e)
{
rectSelf.rectBox.style.width = '0px';
rectSelf.rectBox.style.height = '0px';
rectSelf.showRect(false);
}
Rect.prototype.move = function(e)
{
if(rectSelf.rectBox)
{
var currentX = e.clientX?e.clientX + rectSelf.doc.scrollLeft:e.pageX;
var currentY = e.clientY?e.clientY + rectSelf.doc.scrollTop:e.pageY;
rectSelf.rectBox.style.width = Math.abs(currentX - rectSelf.startX) + 'px';
rectSelf.rectBox.style.height = Math.abs(currentY - rectSelf.startY) + 'px';
if(currentX - rectSelf.startX < 0)
{
rectSelf.rectBox.style.left = currentX + 'px';
}
if(currentY - rectSelf.startY < 0)
{
rectSelf.rectBox.style.top = currentY + 'px';
}
//document.title = "left:"+currentX + 'px '+"top:"+currentY + 'px ';
}
}
Rect.prototype.showRect = function(bool)
{
if(bool)
{
if(!this.rectBox)
{
this.rectBox = document.createElement("div");
this.rectBox.id = "rectBox";
document.body.appendChild(this.rectBox);
}
this.rectBox.style.display = "block";
this.rectBox.style.left = this.startX + 'px';
this.rectBox.style.top = this.startY + 'px'; 
this.addEventListener(this.doc , 'mousemove' , this.move);
}
else
{
if(this.rectBox)
{
this.rectBox.style.display = "none";
}
this.removeEventListener(this.doc , 'mousemove' , this.move);
}
}
Rect.prototype.addEventListener = function(o,e,l) 
{
if (o.addEventListener) {
o.addEventListener(e,l,false);
} else if (o.attachEvent) {
o.attachEvent('on'+e,function() {
l(window.event);
});
}
}
Rect.prototype.removeEventListener = function(o,e,l) 
{
if (o.removeEventListener) {
o.removeEventListener(e,l,false);
} else if (o.detachEvent) {
o.detachEvent('on'+e,function() {
l(window.event);
});
}
}
window.onload = function()
{
var oRect = new Rect();
oRect.addEventListener(oRect.doc , 'mousedown' , oRect.down);
oRect.addEventListener(oRect.doc , 'mouseup' , oRect.up);
}
</script>
</head>
<body>
<h1>拖动你的鼠标就会出现选择框</h1>
</body>
</html>

希望本文所述对大家的javascript程序设计有所帮助。

回复

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2022-11-21 17:40:44 | 显示全部楼层
儿飞飞微风DVD谁vdsvd
回复 支持 反对

使用道具 举报

6

主题

2万

回帖

2万

积分

论坛元老

Rank: 8Rank: 8

积分
25525
发表于 2023-6-18 07:19:31 | 显示全部楼层
来看看!!!
回复 支持 反对

使用道具 举报

0

主题

1万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2023-9-11 14:55:26 | 显示全部楼层
的沙发水电费水电费
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

0

积分

中级会员

Rank: 3Rank: 3

积分
0
发表于 2023-12-3 14:02:11 | 显示全部楼层
这个源码不错啊
回复 支持 反对

使用道具 举报

11

主题

1万

回帖

1万

积分

论坛元老

Rank: 8Rank: 8

积分
13286
发表于 2024-2-27 17:21:15 | 显示全部楼层
搞个免费的用用
回复 支持 反对

使用道具 举报

5

主题

2万

回帖

2万

积分

论坛元老

Rank: 8Rank: 8

积分
25219
发表于 2024-3-23 19:48:04 | 显示全部楼层
啊啊啊啊啊啊啊啊啊啊啊啊啊啊
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

2万

积分

论坛元老

Rank: 8Rank: 8

积分
25852
发表于 2024-4-27 13:14:58 | 显示全部楼层
554411515451555
回复 支持 反对

使用道具 举报

13

主题

2万

回帖

2万

积分

论坛元老

Rank: 8Rank: 8

积分
25229
发表于 2024-5-2 01:32:17 | 显示全部楼层
问问问企鹅哇哇哇哇哇
回复 支持 反对

使用道具 举报

0

主题

2万

回帖

2万

积分

论坛元老

Rank: 8Rank: 8

积分
25860
发表于 2024-5-4 17:22:59 | 显示全部楼层
借款金额看了就立刻
回复 支持 反对

使用道具 举报

高级模式
B Color Image Link Quote Code Smilies

本版积分规则

手机版|小黑屋|网站地图|源码论坛 ( 海外版 )

GMT+8, 2025-2-11 09:55 , Processed in 0.072033 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表