二つのiframeの中のdiv連動
17181 ワード
ホームページ:main.
- 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>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
- <title>Iframe DIV title>
- head>
- <body>
- <iframe id="left" name="left" width="40%" height="500px" src="left.html" >iframe>
- <iframe id="right" name="right" width="40%" height="500px" src="right.html">iframe>
- body>
- html>
左側iframe:left.
- <html>
- <head>
- <script>
- var obj;
- var x;
- var y;
- function fun_down(divobj){
- obj = divobj;
- x = event.x;
- y = event.y;
- }
- document.onmousemove = function(){
- if(obj){
- var x1 = event.x - x;
- var y1 = event.y - y;
- x = event.x;
- y = event.y;
- objobj.style.left = obj.offsetLeft + x1;
- objobj.style.top = obj.offsetTop + y1;
- }
- }
- document.onmouseup = function(){
- if(obj){
- obj.releaseCapture();
- obj = null;
- }
- }
-
-
- script>
- head>
- <body>
- <div style="text-align:center"> iframediv>
- <div id="div1" onmousedown="fun_down(this);" style="position:absolute; left:200px; top:200px;
- background-color:#00FF00; width:50px; height:50px" >LEFT div>
- body>
- html>
右側iframe:right.
- <html>
- <head>
- <script>
- var intX;
- var intY;
- var intX1;
- var intY1;
- var blnDrag = false; //
- // iframe
- var v = parent.document.frames['left'];
- //
- function MouseDown() {
- //
- blnDrag = true;
- //alert(v.document.body.innerHTML);
- intX = event.clientX - GetDiv().style.pixelLeft;
- intY = event.clientY - GetDiv().style.pixelTop;
- intX1 = event.clientX - v.document.getElementById("div1").style.pixelLeft;
- intY1 = event.clientY - v.document.getElementById("div1").style.pixelTop;
- }
-
- //
- function DragDiv() {
- //
- if (!blnDrag) {
- return false;
- }
- else {
- GetDiv().style.pixelLeft = event.clientX - intX;
- GetDiv().style.pixelTop = event.clientY - intY;
- v.document.getElementById("div1").style.pixelLeft = event.clientX - intX1;
- v.document.getElementById("div1").style.pixelTop = event.clientY - intY1;
- }
- }
-
- //
- function MouseUp() {
- //
- blnDrag = false;
- }
-
- // DIV
- function GetDiv() {
- return document.getElementById("div2");
- }
- script>
- head>
- <body>
- <div style="text-align:center"> iframediv>
- <div id="div2" style=" text-align:center;position:absolute; left:200px; top:200px; background-color:#FF0000;
- width:50px; height:50px" onmousemove="DragDiv()" onMouseDown="MouseDown()" onMouseUp="MouseUp()">RIGHT<br> div>
- body>
- html>