基于jquery的用鼠標(biāo)畫出可移動(dòng)的div
更新時(shí)間:2012年09月06日 16:33:57 作者:
這個(gè)測(cè)試用例的div移動(dòng)出了點(diǎn)問題,因?yàn)槭堑谝淮问褂胘sfiddle網(wǎng)站,所以用的不是很熟悉,還希望高手指點(diǎn)
具體的原理我就不多說了,直接貼代碼。
html代碼:
<!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">
<head>
<title>Draw rectangle</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<script src="jquery-1.6.2.min.js" type="text/javascript"></script>
<script src="jquery.ui.core.js" type="text/javascript"></script>
<script src="jquery.ui.widget.js" type="text/javascript"></script>
<script src="jquery.ui.mouse.js" type="text/javascript"></script>
<script src="jquery.ui.draggable.js" type="text/javascript"></script>
<link href="catch.css" rel="stylesheet" type="text/css";charset=gb2312/>
<script src="catch.js" type="text/javascript";charset=gb2312></script>
<!--[if gte IE 7]>
<style type="text/css">
</style>
<![endif]-->
</head>
<body>
<!-- header -->
<div id="header">
<label>Draw!</label>
</div>
<!-- content -->
<div id="content">
</div>
<!-- bottom -->
<div id="bottom">
</div>
</body>
</html>
css代碼:
body
{
font-family: "Helvetica Neue", "Lucida Grande", "Segoe UI", Arial, Helvetica, Verdana, sans-serif;
margin: 0px;
}
#header
{
width:150px;
margin:0px auto;
}
#header label
{
font-size:45px;
font-weight:bolder;
}
#content
{
width:90%;
height:600px;
margin:10px auto;
border:1px solid blue;
}
.new_rect
{
opacity: 0.7;
-moz-opacity: 0.7;
filter: alpha(opacity=70);
-ms-filter: alpha(opacity=70);
background:#A8CAEC;
border:1px solid #3399FF;
position:fixed;
float:left;
}
.new_rect:hover{
cursor:move;
}
.mousedown{
-webkit-box-shadow:5px 5px 5px black;
-moz-box-shadow:5px 5px 5px black;
box-shadow:5px 5px 5px black;
z-index:999;
}
js代碼:
//////////////////////////////////////////////////////////
$(function () {
//$("div[title=new_rect]").click(function(){alert("click");});
//$(".new_rect").draggable();
drow_rect("#content");
})
/////////////////////////////////////////////////////////
function drow_rect(the_id){//theid表示用作畫布的層
var num=1;
var x_down=0,y_down=0;
var new_width=0,new_height=0;
var x_original=0,y_original=0;
var original_flag=true,down_flag=false;
var x_point=0,y_point=0;
var append_string;
var MouseDown=function(e){
down_flag=true;
x_down=e.pageX;
y_down=e.pageY;//記錄鼠標(biāo)的當(dāng)前坐標(biāo)
if(original_flag){//如果是第一次點(diǎn)擊,把起始點(diǎn)的坐標(biāo)記錄到 x_original 和 y_original中
x_original=e.pageX;
y_original=e.pageY;
original_flag=false;
}
};
var MouseMove=function(e){
if(down_flag){//鼠標(biāo)有移動(dòng)
x_down=e.pageX;
y_down=e.pageY;
x_point=x_original;
y_point=y_original;
new_width=x_down-x_original;
if(new_width<0){//鼠標(biāo)向左運(yùn)動(dòng)
new_width=-new_width;
x_point=x_down;
}
new_height=y_down-y_original;
if(new_height<0){ //鼠標(biāo)向右運(yùn)動(dòng)
new_height=-new_height;
y_point=y_down;
}
$("div[name='"+num+"']").remove();//把前面的層刪除,并在后面的代碼中生成新的層
append_string="<div class='new_rect' style='left:"+x_point+"px;top:"+y_point+"px;"+"width:"+new_width+"px;height:"
+new_height+"px' name='"+num+"' title='第"+num+"個(gè)'></div>";
$(the_id).append(append_string);
}
}
$(the_id).bind("mousedown",MouseDown);
$(the_id).bind("mousemove",MouseMove);//事件綁定
$(the_id).mouseup(function(e){//松開鼠標(biāo)左鍵,初始化標(biāo)志位
down_flag=false;
original_flag=true;
$("div[name='"+num+"']").draggable();
$("div[name='"+num+"']").mousedown(function(){
$(this).addClass("mousedown");//添加陰影
$(the_id).unbind("mousedown",MouseDown);
$(the_id).unbind("mousemove",MouseMove);//取消事件綁定
});
$("div[name='"+num+"']").mouseup(function(){
$(this).removeClass("mousedown");//刪除陰影
$(the_id).bind("mousedown",MouseDown);
$(the_id).bind("mousemove",MouseMove);//事件綁定
});
num++;
});
}
上傳一個(gè)實(shí)例圖片:
html代碼:
復(fù)制代碼 代碼如下:
<!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">
<head>
<title>Draw rectangle</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<script src="jquery-1.6.2.min.js" type="text/javascript"></script>
<script src="jquery.ui.core.js" type="text/javascript"></script>
<script src="jquery.ui.widget.js" type="text/javascript"></script>
<script src="jquery.ui.mouse.js" type="text/javascript"></script>
<script src="jquery.ui.draggable.js" type="text/javascript"></script>
<link href="catch.css" rel="stylesheet" type="text/css";charset=gb2312/>
<script src="catch.js" type="text/javascript";charset=gb2312></script>
<!--[if gte IE 7]>
<style type="text/css">
</style>
<![endif]-->
</head>
<body>
<!-- header -->
<div id="header">
<label>Draw!</label>
</div>
<!-- content -->
<div id="content">
</div>
<!-- bottom -->
<div id="bottom">
</div>
</body>
</html>
css代碼:
復(fù)制代碼 代碼如下:
body
{
font-family: "Helvetica Neue", "Lucida Grande", "Segoe UI", Arial, Helvetica, Verdana, sans-serif;
margin: 0px;
}
#header
{
width:150px;
margin:0px auto;
}
#header label
{
font-size:45px;
font-weight:bolder;
}
#content
{
width:90%;
height:600px;
margin:10px auto;
border:1px solid blue;
}
.new_rect
{
opacity: 0.7;
-moz-opacity: 0.7;
filter: alpha(opacity=70);
-ms-filter: alpha(opacity=70);
background:#A8CAEC;
border:1px solid #3399FF;
position:fixed;
float:left;
}
.new_rect:hover{
cursor:move;
}
.mousedown{
-webkit-box-shadow:5px 5px 5px black;
-moz-box-shadow:5px 5px 5px black;
box-shadow:5px 5px 5px black;
z-index:999;
}
js代碼:
復(fù)制代碼 代碼如下:
//////////////////////////////////////////////////////////
$(function () {
//$("div[title=new_rect]").click(function(){alert("click");});
//$(".new_rect").draggable();
drow_rect("#content");
})
/////////////////////////////////////////////////////////
function drow_rect(the_id){//theid表示用作畫布的層
var num=1;
var x_down=0,y_down=0;
var new_width=0,new_height=0;
var x_original=0,y_original=0;
var original_flag=true,down_flag=false;
var x_point=0,y_point=0;
var append_string;
var MouseDown=function(e){
down_flag=true;
x_down=e.pageX;
y_down=e.pageY;//記錄鼠標(biāo)的當(dāng)前坐標(biāo)
if(original_flag){//如果是第一次點(diǎn)擊,把起始點(diǎn)的坐標(biāo)記錄到 x_original 和 y_original中
x_original=e.pageX;
y_original=e.pageY;
original_flag=false;
}
};
var MouseMove=function(e){
if(down_flag){//鼠標(biāo)有移動(dòng)
x_down=e.pageX;
y_down=e.pageY;
x_point=x_original;
y_point=y_original;
new_width=x_down-x_original;
if(new_width<0){//鼠標(biāo)向左運(yùn)動(dòng)
new_width=-new_width;
x_point=x_down;
}
new_height=y_down-y_original;
if(new_height<0){ //鼠標(biāo)向右運(yùn)動(dòng)
new_height=-new_height;
y_point=y_down;
}
$("div[name='"+num+"']").remove();//把前面的層刪除,并在后面的代碼中生成新的層
append_string="<div class='new_rect' style='left:"+x_point+"px;top:"+y_point+"px;"+"width:"+new_width+"px;height:"
+new_height+"px' name='"+num+"' title='第"+num+"個(gè)'></div>";
$(the_id).append(append_string);
}
}
$(the_id).bind("mousedown",MouseDown);
$(the_id).bind("mousemove",MouseMove);//事件綁定
$(the_id).mouseup(function(e){//松開鼠標(biāo)左鍵,初始化標(biāo)志位
down_flag=false;
original_flag=true;
$("div[name='"+num+"']").draggable();
$("div[name='"+num+"']").mousedown(function(){
$(this).addClass("mousedown");//添加陰影
$(the_id).unbind("mousedown",MouseDown);
$(the_id).unbind("mousemove",MouseMove);//取消事件綁定
});
$("div[name='"+num+"']").mouseup(function(){
$(this).removeClass("mousedown");//刪除陰影
$(the_id).bind("mousedown",MouseDown);
$(the_id).bind("mousemove",MouseMove);//事件綁定
});
num++;
});
}
上傳一個(gè)實(shí)例圖片:
您可能感興趣的文章:
- jQuery實(shí)現(xiàn)鼠標(biāo)滑過Div層背景變顏色的方法
- jquery鼠標(biāo)放上去顯示懸浮層即彈出定位的div層
- jQuery實(shí)現(xiàn)div跟隨鼠標(biāo)移動(dòng)
- jQuery實(shí)現(xiàn)鼠標(biāo)跟隨提示層效果代碼(可顯示文本,Div,Table,Html等)
- jQuery實(shí)現(xiàn)DIV層收縮展開的方法
- jquery實(shí)現(xiàn)鼠標(biāo)滑過顯示提示框的方法
- jQuery實(shí)現(xiàn)的鼠標(biāo)滑過彈出放大圖片特效
- jQuery實(shí)現(xiàn)表格展開與折疊的方法
- Jquery實(shí)現(xiàn)由下向上展開效果的例子
- jQuery實(shí)現(xiàn)DIV響應(yīng)鼠標(biāo)滑過由下向上展開效果示例【測(cè)試可用】
相關(guān)文章
jQuery插件HighCharts繪制2D圓環(huán)圖效果示例【附demo源碼下載】
這篇文章主要介紹了jQuery插件HighCharts繪制2D圓環(huán)圖效果,結(jié)合實(shí)例形式分析了jQuery使用HighCharts插件繪制圓環(huán)圖的實(shí)現(xiàn)步驟與相關(guān)操作技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-03-03
jQuery實(shí)現(xiàn)的隔行變色功能【案例】
這篇文章主要介紹了jQuery實(shí)現(xiàn)的隔行變色功能,結(jié)合具體實(shí)例形式分析了jQuery事件響應(yīng)、元素遍歷及屬性動(dòng)態(tài)操作相關(guān)使用技巧,需要的朋友可以參考下2019-02-02
jquery調(diào)用wcf并展示出數(shù)據(jù)的方法
網(wǎng)上看了很多jquery調(diào)用wcf的例子,可能是主機(jī)的原因,我用的是gd主機(jī),所以都沒有成功,昨天自己搞了一天,終于成功了,現(xiàn)把方法和代碼和大家分享2011-07-07
Treegrid的動(dòng)態(tài)加載實(shí)例代碼
這篇文章主要介紹了Treegrid的動(dòng)態(tài)加載實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-04-04
jquery按回車鍵實(shí)現(xiàn)表單提交的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)硪黄猨query按回車鍵實(shí)現(xiàn)表單提交的簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-05
jquery的相對(duì)父元素和相對(duì)文檔定位示例代碼
在開發(fā)jquery時(shí)候經(jīng)常需要用到定位,有相對(duì)父元素定位和相對(duì)文檔定位,本文為此總結(jié)下,有需要的朋友可以參考下2013-08-08
基于jQuery的可用于選項(xiàng)卡及幻燈的切換插件
最近公司項(xiàng)目頁面中用到選項(xiàng)卡與幻燈比較多,特地寫了個(gè)集選項(xiàng)卡、幻燈片與播放控制于一體的插件,同頁面可多次使用。2011-03-03
jquery如何判斷表格同一列不同行input數(shù)據(jù)是否重復(fù)
這篇文章主要介紹了jquery如何判斷表格同一列不同行input數(shù)據(jù)是否重復(fù),需要的朋友可以參考下2014-05-05

