Js無重新整理新增新層,拖動DIV層可互換位置的JavaScript實現
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Js無重新整理新增新層</title>
<style>
body {margin:0px;padding:0px;font-size:12px;text-align:center;}
body > div {text-align:center; margin-right:auto; margin-left:auto;}
.content{width:900px;}
.content .left{
float:left;
width:20%;
border:1px solid #0066CC;
margin:3px;
}
.content .center{float:left;border:1px solid #FF0000;margin:3px;width:57%}
.content .right{float:right;width:20%;border:1px solid #FF0000;margin:3px}
.mo{height:auto;border:1px solid #CCC;margin:3px;background:#FFF}
.mo h1{background:#ECF9FF;height:18px;padding:3px;cursor:move}
.closediv{cursor:default;}
.minusspan{cursor:default;}
.mo .nr{height:80px;border:1px solid #F3F3F3;margin:2px}
h1{margin:0px;padding:0px;text-align:left;font-size:12px}
.dragging {
FILTER: progid:DXImageTransform.Microsoft.Alpha(opacity=60); opacity: 0.6; moz-opacity: 0.6
}
</style>
<script language="javascript">
var dragobj={}
window.onerror=function(){return false}
var domid=12
function on_ini(){
String.prototype.inc=function(s){return this.indexOf(s)>-1?true:false}
var agent=navigator.userAgent
window.isOpr=agent.inc("Opera")
window.isIE=agent.inc("IE") && !isOpr
window.isMoz=agent.inc("Mozilla") && !isOpr && !isIE
if(isMoz){
Event.prototype.__defineGetter__("x",function(){return this.clientX+2})
Event.prototype.__defineGetter__("y",function(){return this.clientY+2})
}
basic_ini()
}
function basic_ini(){
window.$=function(obj){return typeof(obj)=="string"?document.getElementById(obj):obj}
window.oDel=function(obj){if($(obj)!=null){$(obj).parentNode.removeChild($(obj))}}
}
window.oDel=function(obj){if($(obj)!=null){$(obj).parentNode.removeChild($(obj))}}
window.onload=function(){
on_ini()
var o=document.getElementsByTagName("h1")
for(var i=0;i<o.length;i++){
o[i].onmousedown=addevent;
//新增摺疊和關閉按鈕
var tt = document.createElement("div");
tt.style.cssText = "float:left";
var span = document.createElement("span");
span.innerHTML = "--"+o[i].innerHTML;
span.style.cssText = "cursor:default;";
span.onmousedown = minusDiv;
tt.appendChild(span);
var close = document.createElement("div");
close.innerHTML = "X";
close.style.cssText = "cursor:default;float:right";
close.onmousedown = closeDiv;
o[i].innerHTML = "";
o[i].appendChild(tt);
o[i].appendChild(close);
}
}
//摺疊或者顯示層
function minusDiv(e)
{
e=e||event
var nr = this.parentNode.parentNode.nextSibling; //取得內容層
nr.style.display = nr.style.display==""?"none":"";
}
//移出層
function closeDiv(e)
{
e=e||event
var mdiv = this.parentNode.parentNode; //取得目標層
oDel(mdiv);
}
function addevent(e){
if(dragobj.o!=null)
return false
e=e||event
dragobj.o=this.parentNode
dragobj.xy=getxy(dragobj.o)
dragobj.xx=new Array((e.x-dragobj.xy[1]),(e.y-dragobj.xy[0]))
//dragobj.o.className = 'dragging';
dragobj.o.style.width=dragobj.xy[2]+"px"
dragobj.o.style.height=dragobj.xy[3]+"px"
dragobj.o.style.left=(e.x-dragobj.xx[0])+"px"
dragobj.o.style.top=(e.y-dragobj.xx[1])+"px"
dragobj.o.style.position="absolute"
dragobj.o.style.filter='alpha(opacity=60)'; //新增拖動透明效果
var om=document.createElement("div")
dragobj.otemp=om
om.style.width=dragobj.xy[2]+"px"
om.style.height=dragobj.xy[3]+"px"
om.style.border = "1px dashed red"; //ikaiser新增,實現虛線框
dragobj.o.parentNode.insertBefore(om,dragobj.o)
return false
}
document.onselectstart=function(){return false}
window.onfocus=function(){document.onmouseup()}
window.onblur=function(){document.onmouseup()}
document.onmouseup=function(){
if(dragobj.o!=null){
dragobj.o.style.width="auto"
dragobj.o.style.height="auto"
dragobj.otemp.parentNode.insertBefore(dragobj.o,dragobj.otemp)
dragobj.o.style.position=""
oDel(dragobj.otemp)
dragobj={}
}
}
document.onmousemove=function(e){
e=e||event
if(dragobj.o!=null){
dragobj.o.style.left=(e.x-dragobj.xx[0])+"px"
dragobj.o.style.top=(e.y-dragobj.xx[1])+"px"
createtmpl(e, dragobj.o) //傳遞當前拖動物件
}
}
function getxy(e){
var a=new Array()
var t=e.offsetTop;
var l=e.offsetLeft;
var w=e.offsetWidth;
var h=e.offsetHeight;
while(e=e.offsetParent){
t+=e.offsetTop;
l+=e.offsetLeft;
}
a[0]=t;a[1]=l;a[2]=w;a[3]=h
return a;
}
function inner(o,e){
var a=getxy(o)
if(e.x>a[1] && e.x<(a[1]+a[2]) && e.y>a[0] && e.y<(a[0]+a[3])){
if(e.y<(a[0]+a[3]/2))
return 1;
else
return 2;
}else
return 0;
}
//將當前拖動層在拖動時可變化大小,預覽效果
function createtmpl(e, elm){
for(var i=0;i<domid;i++){
if(document.getElementById("m"+i) == null) //已經移出的層不再遍歷
continue;
if($("m"+i)==dragobj.o)
continue
var b=inner($("m"+i),e)
if(b==0)
continue
dragobj.otemp.style.width=$("m"+i).offsetWidth
elm.style.width = $("m"+i).offsetWidth;
//1為下移,2為上移
if(b==1){
$("m"+i).parentNode.insertBefore(dragobj.otemp,$("m"+i))
}else{
if($("m"+i).nextSibling==null){
$("m"+i).parentNode.appendChild(dragobj.otemp)
}else{
$("m"+i).parentNode.insertBefore(dragobj.otemp,$("m"+i).nextSibling)
}
}
return
}
for(var j=0;j<3;j++){
if($("dom"+j).innerHTML.inc("div")||$("dom"+j).innerHTML.inc("DIV"))
continue
var op=getxy($("dom"+j))
if(e.x>(op[1]+10) && e.x<(op[1]+op[2]-10)){
$("dom"+j).appendChild(dragobj.otemp)
dragobj.otemp.style.width=(op[2]-10)+"px"
}
}
}
function add_div()
{
var o=document.createElement("div")
o.className="mo"
o.id="m"+domid
$('dom0').appendChild(o)
o.innerHTML="<h1>dom"+domid+"</h1><div class=nr></div>"
o.getElementsByTagName("h1")[0].onmousedown=addevent
domid++
}
</script>
</head>
<body>
<INPUT TYPE="button" value="新增一個新的DIV層" onclick="add_div();">
<div class=content>
<div class=left id=dom0>
<div class=mo id=m0>
<h1>dom0</h1>
<div class="nr"></div>
</div>
<div class=mo id=m1>
<h1>dom1</h1><div class="nr"></div>
</div>
<div class=mo id=m2><h1>dom2</h1><div class="nr"></div></div>
<div class=mo id=m3><h1>dom3</h1><div class="nr"></div></div>
</div>
<div class=center id=dom1>
<div class=mo id=m4><h1>dom4</h1><div class="nr"></div></div>
<div class=mo id=m5><h1>dom5</h1><div class="nr"></div></div>
</div>
<div class=right id=dom2>
<div class=mo id=m8><h1>dom8</h1><div class="nr"></div></div>
<div class=mo id=m9><h1>dom9</h1><div class="nr"></div></div>
</div>
</div>
<div><A href="http://www.999jiujiu.com/">http://www.999jiujiu.com/</A></div>
</body>
</html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Js無重新整理新增新層</title>
<style>
body {margin:0px;padding:0px;font-size:12px;text-align:center;}
body > div {text-align:center; margin-right:auto; margin-left:auto;}
.content{width:900px;}
.content .left{
float:left;
width:20%;
border:1px solid #0066CC;
margin:3px;
}
.content .center{float:left;border:1px solid #FF0000;margin:3px;width:57%}
.content .right{float:right;width:20%;border:1px solid #FF0000;margin:3px}
.mo{height:auto;border:1px solid #CCC;margin:3px;background:#FFF}
.mo h1{background:#ECF9FF;height:18px;padding:3px;cursor:move}
.closediv{cursor:default;}
.minusspan{cursor:default;}
.mo .nr{height:80px;border:1px solid #F3F3F3;margin:2px}
h1{margin:0px;padding:0px;text-align:left;font-size:12px}
.dragging {
FILTER: progid:DXImageTransform.Microsoft.Alpha(opacity=60); opacity: 0.6; moz-opacity: 0.6
}
</style>
<script language="javascript">
var dragobj={}
window.onerror=function(){return false}
var domid=12
function on_ini(){
String.prototype.inc=function(s){return this.indexOf(s)>-1?true:false}
var agent=navigator.userAgent
window.isOpr=agent.inc("Opera")
window.isIE=agent.inc("IE") && !isOpr
window.isMoz=agent.inc("Mozilla") && !isOpr && !isIE
if(isMoz){
Event.prototype.__defineGetter__("x",function(){return this.clientX+2})
Event.prototype.__defineGetter__("y",function(){return this.clientY+2})
}
basic_ini()
}
function basic_ini(){
window.$=function(obj){return typeof(obj)=="string"?document.getElementById(obj):obj}
window.oDel=function(obj){if($(obj)!=null){$(obj).parentNode.removeChild($(obj))}}
}
window.oDel=function(obj){if($(obj)!=null){$(obj).parentNode.removeChild($(obj))}}
window.onload=function(){
on_ini()
var o=document.getElementsByTagName("h1")
for(var i=0;i<o.length;i++){
o[i].onmousedown=addevent;
//新增摺疊和關閉按鈕
var tt = document.createElement("div");
tt.style.cssText = "float:left";
var span = document.createElement("span");
span.innerHTML = "--"+o[i].innerHTML;
span.style.cssText = "cursor:default;";
span.onmousedown = minusDiv;
tt.appendChild(span);
var close = document.createElement("div");
close.innerHTML = "X";
close.style.cssText = "cursor:default;float:right";
close.onmousedown = closeDiv;
o[i].innerHTML = "";
o[i].appendChild(tt);
o[i].appendChild(close);
}
}
//摺疊或者顯示層
function minusDiv(e)
{
e=e||event
var nr = this.parentNode.parentNode.nextSibling; //取得內容層
nr.style.display = nr.style.display==""?"none":"";
}
//移出層
function closeDiv(e)
{
e=e||event
var mdiv = this.parentNode.parentNode; //取得目標層
oDel(mdiv);
}
function addevent(e){
if(dragobj.o!=null)
return false
e=e||event
dragobj.o=this.parentNode
dragobj.xy=getxy(dragobj.o)
dragobj.xx=new Array((e.x-dragobj.xy[1]),(e.y-dragobj.xy[0]))
//dragobj.o.className = 'dragging';
dragobj.o.style.width=dragobj.xy[2]+"px"
dragobj.o.style.height=dragobj.xy[3]+"px"
dragobj.o.style.left=(e.x-dragobj.xx[0])+"px"
dragobj.o.style.top=(e.y-dragobj.xx[1])+"px"
dragobj.o.style.position="absolute"
dragobj.o.style.filter='alpha(opacity=60)'; //新增拖動透明效果
var om=document.createElement("div")
dragobj.otemp=om
om.style.width=dragobj.xy[2]+"px"
om.style.height=dragobj.xy[3]+"px"
om.style.border = "1px dashed red"; //ikaiser新增,實現虛線框
dragobj.o.parentNode.insertBefore(om,dragobj.o)
return false
}
document.onselectstart=function(){return false}
window.onfocus=function(){document.onmouseup()}
window.onblur=function(){document.onmouseup()}
document.onmouseup=function(){
if(dragobj.o!=null){
dragobj.o.style.width="auto"
dragobj.o.style.height="auto"
dragobj.otemp.parentNode.insertBefore(dragobj.o,dragobj.otemp)
dragobj.o.style.position=""
oDel(dragobj.otemp)
dragobj={}
}
}
document.onmousemove=function(e){
e=e||event
if(dragobj.o!=null){
dragobj.o.style.left=(e.x-dragobj.xx[0])+"px"
dragobj.o.style.top=(e.y-dragobj.xx[1])+"px"
createtmpl(e, dragobj.o) //傳遞當前拖動物件
}
}
function getxy(e){
var a=new Array()
var t=e.offsetTop;
var l=e.offsetLeft;
var w=e.offsetWidth;
var h=e.offsetHeight;
while(e=e.offsetParent){
t+=e.offsetTop;
l+=e.offsetLeft;
}
a[0]=t;a[1]=l;a[2]=w;a[3]=h
return a;
}
function inner(o,e){
var a=getxy(o)
if(e.x>a[1] && e.x<(a[1]+a[2]) && e.y>a[0] && e.y<(a[0]+a[3])){
if(e.y<(a[0]+a[3]/2))
return 1;
else
return 2;
}else
return 0;
}
//將當前拖動層在拖動時可變化大小,預覽效果
function createtmpl(e, elm){
for(var i=0;i<domid;i++){
if(document.getElementById("m"+i) == null) //已經移出的層不再遍歷
continue;
if($("m"+i)==dragobj.o)
continue
var b=inner($("m"+i),e)
if(b==0)
continue
dragobj.otemp.style.width=$("m"+i).offsetWidth
elm.style.width = $("m"+i).offsetWidth;
//1為下移,2為上移
if(b==1){
$("m"+i).parentNode.insertBefore(dragobj.otemp,$("m"+i))
}else{
if($("m"+i).nextSibling==null){
$("m"+i).parentNode.appendChild(dragobj.otemp)
}else{
$("m"+i).parentNode.insertBefore(dragobj.otemp,$("m"+i).nextSibling)
}
}
return
}
for(var j=0;j<3;j++){
if($("dom"+j).innerHTML.inc("div")||$("dom"+j).innerHTML.inc("DIV"))
continue
var op=getxy($("dom"+j))
if(e.x>(op[1]+10) && e.x<(op[1]+op[2]-10)){
$("dom"+j).appendChild(dragobj.otemp)
dragobj.otemp.style.width=(op[2]-10)+"px"
}
}
}
function add_div()
{
var o=document.createElement("div")
o.className="mo"
o.id="m"+domid
$('dom0').appendChild(o)
o.innerHTML="<h1>dom"+domid+"</h1><div class=nr></div>"
o.getElementsByTagName("h1")[0].onmousedown=addevent
domid++
}
</script>
</head>
<body>
<INPUT TYPE="button" value="新增一個新的DIV層" onclick="add_div();">
<div class=content>
<div class=left id=dom0>
<div class=mo id=m0>
<h1>dom0</h1>
<div class="nr"></div>
</div>
<div class=mo id=m1>
<h1>dom1</h1><div class="nr"></div>
</div>
<div class=mo id=m2><h1>dom2</h1><div class="nr"></div></div>
<div class=mo id=m3><h1>dom3</h1><div class="nr"></div></div>
</div>
<div class=center id=dom1>
<div class=mo id=m4><h1>dom4</h1><div class="nr"></div></div>
<div class=mo id=m5><h1>dom5</h1><div class="nr"></div></div>
</div>
<div class=right id=dom2>
<div class=mo id=m8><h1>dom8</h1><div class="nr"></div></div>
<div class=mo id=m9><h1>dom9</h1><div class="nr"></div></div>
</div>
</div>
<div><A href="http://www.999jiujiu.com/">http://www.999jiujiu.com/</A></div>
</body>
</html>
相關文章
- JS實現拖動div層移動JS
- jquery div層拖動效果封裝jQuery封裝
- 使用easydrag實現可拖動的DIV彈出框
- iGoogle的模組拖動層拖動Go
- js實現的div拖動效果例項程式碼JS
- 06_應用層和核心層實現資料互動
- jQuery實現的自定義可以拖動的彈出層效果jQuery
- asp.net,網頁層與div的資料互動ASP.NET網頁
- JavaScript拖動div元素詳解JavaScript
- vueusejs實現拖動VueJS
- JavaScript滑鼠拖動調整div大小JavaScript
- 網頁模組拖動,Js仿Google和Windows Live的層拖拽網頁JSGoWindows
- JS實現載入層JS
- Jquery實現滑鼠拖動改變div高度jQuery
- js 實現 bind 的這五層,你在第幾層?JS
- javascript div元素滑鼠拖動效果詳解JavaScript
- javascript動態設定一個div圖層覆蓋住另一個div圖層,並設定成透明JavaScript
- WKWebViewJavascriptBridge - 優雅的 iOS 與 JS 互動層框架(Swift)WebViewJavaScriptiOSJS框架Swift
- javacript 拖動 divJava
- js如何動態在div中新增一個新的元素JS
- CSS實現div層半透明效果程式碼例項CSS
- javascript事件機制底層實現原理JavaScript事件
- C# 操作PDF 圖層(Layer)—新增、刪除圖層、設定圖層可見性C#
- html隨意拖動內容位置的兩種實現方式HTML
- js實現的通過拖動調整元素間的相對位置程式碼例項JS
- 滑鼠拖動實現將div放入方格例項程式碼
- 頁面無重新整理切換實現
- js實現的使用鍵盤操作div位置程式碼例項JS
- 拖動滾動條實現div跟隨效果程式碼例項
- # MySQL server 層和儲存引擎層是怎麼互動資料的?MySqlServer儲存引擎
- javascript實現掉落彈出層------Day29JavaScript
- QT實現可拖動自定義控制元件QT控制元件
- html5拖放--15行js程式碼實現兩個div內容互換HTMLJS
- Flutter使用Draggable與自定義RenderObject實現圖片新增標籤,隨意拖動位置效果FlutterObject
- 利用HTML5,無JS實現各種互動效果HTMLJS
- jquery智慧彈出層,自動判斷位置jQuery
- js實現彈出層滑鼠跟隨效果JS
- javascript層自動關閉效果JavaScript