Home Web Front-end JS Tutorial Drag layer effect, compatible with IE and FF!

Drag layer effect, compatible with IE and FF!

May 16, 2016 pm 07:23 PM

Copy code The code is as follows:


<!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>
    <meta http-equiv="content-type" content="text/html; charset=gb2312">
    <meta http-equiv="content-script-type" content="text/javascript">
    <meta http-equiv="content-style-type" content="text/css">
    <title>DoDi Chat v1.0 Beta</title>
    <style rel="stylesheet" type="text/css" media="all" />
    <!--
    body {
        text-align:left;
        margin:0;
        font:normal 12px Verdana, Arial;
        background:#FFEEFF
    }
    form {
        margin:0;
        font:normal 12px Verdana, Arial;
    }
    table,input {
        font:normal 12px Verdana, Arial;
    }
    a:link,a:visited{
        text-decoration:none;
        color:#333333;
    }
    a:hover{
        text-decoration:none;
        color:#FF6600
    }
    #main {
        width:400px;
        position:absolute;
        left:600px;
        top:100px;
        background:#EFEFFF;
        text-align:left;
        filter:Alpha(opacity=90)
    }
    #ChatHead {
        text-align:right;
        padding:3px;
        border:1px solid #003399;
        background:#DCDCFF;
        font-size:11px;
        color:#3366FF;
        cursor:move;
    }
    #ChatHead a:link,#ChatHead a:visited, {
        font-size:14px;
        font-weight:bold;
        padding:0 3px
    }
    #ChatBody {
        border:1px solid #003399;
        border-top:none;
        padding:2px;
    }
    #ChatContent {
        height:200px;
        padding:6px;
        overflow-y:scroll;
        word-break: break-all
    }
    #ChatBtn {
        border-top:1px solid #003399;
        padding:2px
    }
    -->
    </style>
    <script language="javascript" type="text/javascript">
    <!--
    function ChatHidden()
    {
        document.getElementById("ChatBody").style.display = "none";
    }
    function ChatShow()
    {
        document.getElementById("ChatBody").style.display = "";
    }
    function ChatClose()
    {
        document.getElementById("main").style.display = "none";
    }
    function ChatSend(obj)
    {
        var o = obj.ChatValue;
        if (o.value.length>0){
            document.getElementById("ChatContent").innerHTML  = "<strong>Akon说:</strong>" o.value "<br/>";
            o.value='';
        }
    }

    if  (document.getElementById)
    {
        (
            function()
            {
                if (window.opera){ document.write("<input type='hidden' id='Q' value=' '>"); }

                var n = 500;
                var dragok = false;
                var y,x,d,dy,dx;

                function move(e)
                {
                    if (!e) e = window.event;
                    if (dragok){
                        d.style.left = dx   e.clientX - x   "px";
                        d.style.top  = dy   e.clientY - y   "px";
                        return false;
                    }
                }

                function down(e)
                {
                    if (!e) e = window.event;
                    var temp = (typeof e.target != "undefined")?e.target:e.srcElement;
                    if (temp.tagName != "HTML"|"BODY" && temp.className != "dragclass"){
                        temp = (typeof temp.parentNode != "undefined")?temp.parentNode:temp.parentElement;
                    }
                    if('TR'==temp.tagName){
                        temp = (typeof temp.parentNode != "undefined")?temp.parentNode:temp.parentElement;
                        temp = (typeof temp.parentNode != "undefined")?temp.parentNode:temp.parentElement;
                        temp = (typeof temp.parentNode != "undefined")?temp.parentNode:temp.parentElement;
                    }

                    if (temp.className == "dragclass"){
                        if (window.opera){ document.getElementById("Q").focus(); }
                        dragok = true;
                        temp.style.zIndex = n ;
                        d = temp;
                        dx = parseInt(temp.style.left 0);
                        dy = parseInt(temp.style.top 0);
                        x = e.clientX;
                        y = e.clientY;
                        document.onmousemove = move;
                        return false;
                    }
                }

                function up(){
                    dragok = false;
                    document.onmousemove = null;
                }

                document.onmousedown = down;
                document.onmouseup = up;

            }
        )();
    }
    -->
    </script>
</head>

<body>
<p id="main" class="dragclass">
    <p id="ChatHead">
        <a href="#" onclick="ChatHidden();">-</a>
        <a href="#" onclick="ChatShow();">+</a>
        <a href="#" onclick="ChatClose();">x</a>
    </p>
    <p id="ChatBody">
        <p id="ChatContent"></p>
        <p id="ChatBtn">
            <form action="" name="chat" method="post">
            <textarea name="ChatValue" rows="3" style="width:350px"></textarea>
            <input name="Submit" type="button" value="Chat" onclick="ChatSend(this.form);" />
            </form>
        </p>
    </p>
</p>

</body>
</html>


一个拖动效果,根据论坛的一些帖子改的,但还有一些BUG一直没法解决,谁能帮我改改?
当第一次拖动层时,层的位置会偏离很远。
呃。。。这涉及到一个style的问题。。。
在ie和firefox中,obj.style这个东西实际上只是取得元素中属性style中的值!
如下例,你会发现style块中的属性一个都取不到!

Copy code The code is as follows:


<style>
#test{width:100px;background-color:red;}
</style>
<script>
window.onload=function(){
var t=document.getElementById('test')
var ts=t.style;
t.innerHTML=
"t.style.width:"+ts.width+"<br />"+
"t.style.backgroundColor:"+ts.backgroundColor+"<br />"+
"t.style.color:"+ts.color+"<br />"+
"t.style.paddingLeft:"+ts.paddingLeft
}
</script>
<body>
<p id="test" style="color:yellow;padding-left:100px;">
</p>
</body>


看到了没?前两个style 为空,后两个才有值。
如果是ie,问题很好解决,只要把style改成currentStyle即可。
IE Only

Copy code The code is as follows:


<style>
#test{width:100px;background-color:red;}
</style>
<script>
window.onload=function(){
var t=document.getElementById('test')
var ts=t.currentStyle;
t.innerHTML=
"t.style.width:"+ts.width+"<br />"+
"t.style.backgroundColor:"+ts.backgroundColor+"<br />"+
"t.style.color:"+ts.color+"<br />"+
"t.style.paddingLeft:"+ts.paddingLeft
}
</script>
<body>
<p id="test" style="color:yellow;padding-left:100px;">
</p>
</body>


FF only

Copy code The code is as follows:


<style>
#test{width:100px;background-color:red;}
</style>
<script>
window.onload=function(){
var t=document.getElementById('test')
var ts=document.defaultView.getComputedStyle(t, null);
t.innerHTML=
"t.style.width:"+ts.width+"<br />"+
"t.style.backgroundColor:"+ts.backgroundColor+"<br />"+
"t.style.color:"+ts.color+"<br />"+
"t.style.paddingLeft:"+ts.paddingLeft
}
</script>
<body>
<p id="test" style="color:yellow;padding-left:100px;">
</p>
</body>


我绕了半天,你明白你的错误原因了吗?你的style全都是文档级style,而你试图获取left的时候,第一次获得的只是0,自然会把你的框给挪到边上去了。

最终效果

Copy code The code is as follows:


<!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>
  <meta http-equiv="content-type" content="text/html; charset=gb2312">
  <meta http-equiv="content-script-type" content="text/javascript">
  <meta http-equiv="content-style-type" content="text/css">
  <title>DoDi Chat v1.0 Beta</title>
  <style rel="stylesheet" type="text/css" media="all" />
  <!--
  body {
    text-align:left;
    margin:0;
    font:normal 12px Verdana, Arial;
    background:#FFEEFF
  }
  form {
    margin:0;
    font:normal 12px Verdana, Arial;
  }
  table,input {
    font:normal 12px Verdana, Arial;
  }
  a:link,a:visited{
    text-decoration:none;
    color:#333333;
  }
  a:hover{
    text-decoration:none;
    color:#FF6600
  }
  #main {
    width:400px;
    position:absolute;
    left:600px;
    top:100px;
    background:#EFEFFF;
    text-align:left;
    filter:Alpha(opacity=90)
  }
  #ChatHead {
    text-align:right;
    padding:3px;
    border:1px solid #003399;
    background:#DCDCFF;
    font-size:11px;
    color:#3366FF;
    cursor:move;
  }
  #ChatHead a:link,#ChatHead a:visited, {
    font-size:14px;
    font-weight:bold;
    padding:0 3px
  }
  #ChatBody {
    border:1px solid #003399;
    border-top:none;
    padding:2px;
  }
  #ChatContent {
    height:200px;
    padding:6px;
    overflow-y:scroll;
    word-break: break-all
  }
  #ChatBtn {
    border-top:1px solid #003399;
    padding:2px
  }
  -->
  </style>
  <script language="javascript" type="text/javascript">
  <!--
  function $(d){return document.getElementById(d);}
  function gs(d){var t=$(d);if (t){return t.style;}else{return null;}}
  function gs2(d,a){
    if (d.currentStyle){ 
      var curVal=d.currentStyle[a]
    }else{ 
      var curVal=document.defaultView.getComputedStyle(d, null)[a]
    } 
    return curVal;
  }
  function ChatHidden(){gs("ChatBody").display = "none";}
  function ChatShow(){gs("ChatBody").display = "";}
  function ChatClose(){gs("main").display = "none";}
  function ChatSend(obj){
    var o = obj.ChatValue;
    if (o.value.length>0){
      $("ChatContent").innerHTML  = "<strong>Akon说:</strong>" o.value "<br/>";
      o.value='';
    }
  }

  if  (document.getElementById){
    (
      function(){
        if (window.opera){ document.write("<input type='hidden' id='Q' value=' '>"); }

        var n = 500;
        var dragok = false;
        var y,x,d,dy,dx;

        function move(e)
        {
          if (!e) e = window.event;
          if (dragok){
            d.style.left = dx   e.clientX - x   "px";
            d.style.top  = dy   e.clientY - y   "px";
            return false;
          }
        }

        function down(e){
          if (!e) e = window.event;
          var temp = (typeof e.target != "undefined")?e.target:e.srcElement;
          if (temp.tagName != "HTML"|"BODY" && temp.className != "dragclass"){
            temp = (typeof temp.parentNode != "undefined")?temp.parentNode:temp.parentElement;
          }
          if('TR'==temp.tagName){
            temp = (typeof temp.parentNode != "undefined")?temp.parentNode:temp.parentElement;
            temp = (typeof temp.parentNode != "undefined")?temp.parentNode:temp.parentElement;
            temp = (typeof temp.parentNode != "undefined")?temp.parentNode:temp.parentElement;
          }

          if (temp.className == "dragclass"){
            if (window.opera){ document.getElementById("Q").focus(); }
            dragok = true;
            temp.style.zIndex = n ;
            d = temp;
            dx = parseInt(gs2(temp,"left"))|0;
            dy = parseInt(gs2(temp,"top"))|0;
            x = e.clientX;
            y = e.clientY;
            document.onmousemove = move;
            return false;
          }
        }

        function up(){
          dragok = false;
          document.onmousemove = null;
        }

        document.onmousedown = down;
        document.onmouseup = up;

      }
    )();
  }
  -->
  </script>
</head>

<body>
<p id="main" class="dragclass" style="left:600px;top:300px;">
  <p id="ChatHead">
    <a href="#" onclick="ChatHidden();">-</a>
    <a href="#" onclick="ChatShow();"> </a>
    <a href="#" onclick="ChatClose();">x</a>
  </p>
  <p id="ChatBody">
    <p id="ChatContent"></p>
    <p id="ChatBtn">
      <form action="" name="chat" method="post">
      <textarea name="ChatValue" rows="3" style="width:350px"></textarea>
      <input name="Submit" type="button" value="Chat" onclick="ChatSend(this.form);" />
      </form>
    </p>
  </p>
</p>

</body>
</html>


附解决问题的过程:
1、首先debug,看表现,就知道是在第一次点击的时候,对象的左右边距出错,变成0
2、找到代码中对应位置,输出left和top
3、知道原因,我之前已经知道currentStyle的效果,所以我不需要去网络上搜索相关资料
4、但是我不知道在firefox下如何处理
5、我在google里直接搜索“currentStyle firefox”,很快就找到符合我目的的信息
6、测试通过,发帖子。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Replace String Characters in JavaScript

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

Custom Google Search API Setup Tutorial

Example Colors JSON File Example Colors JSON File Mar 03, 2025 am 12:35 AM

Example Colors JSON File

10 jQuery Syntax Highlighters 10 jQuery Syntax Highlighters Mar 02, 2025 am 12:32 AM

10 jQuery Syntax Highlighters

8 Stunning jQuery Page Layout Plugins 8 Stunning jQuery Page Layout Plugins Mar 06, 2025 am 12:48 AM

8 Stunning jQuery Page Layout Plugins

Build Your Own AJAX Web Applications Build Your Own AJAX Web Applications Mar 09, 2025 am 12:11 AM

Build Your Own AJAX Web Applications

What is 'this' in JavaScript? What is 'this' in JavaScript? Mar 04, 2025 am 01:15 AM

What is 'this' in JavaScript?

10  JavaScript & jQuery MVC Tutorials 10 JavaScript & jQuery MVC Tutorials Mar 02, 2025 am 01:16 AM

10 JavaScript & jQuery MVC Tutorials

See all articles