利用cookie来保存div位置信息 load的时候读取并设置就可以了

解决方案 »

  1.   

    不可能每移动一下下边的div就保存吧?晕了,请各位想想办法哦
      

  2.   

    提供个思路,用hidden来存储值..demo.htm<script>
    //用hidden记录层位置变化,刷新后,位置不变.
    /* Start */
    window.onbeforeunload=function()
    {
        document.all.leftpos.value = parseInt(bao.style.left);
        document.all.toppos.value = parseInt(bao.style.top);
    }
    window.onload=function()
    {
        if(document.all.leftpos.value!="")bao.style.left=document.all.leftpos.value+"px";
        if(document.all.toppos.value!="")bao.style.top=document.all.toppos.value+"px";
    }
    /* End */function beginDrag(elementToDrag,event)  {    var delatX=event.clientX-parseInt(elementToDrag.style.left);
        var delatY=event.clientY-parseInt(elementToDrag.style.top);
        document.attachEvent("onmousemove",moveHandler);
        document.attachEvent("onmouseup",upHandler);
        event.cancelBubble=true;
        event.returnValue=false;    function moveHandler(e)  {
        if(!e) e=window.event;  
        elementToDrag.style.left=(e.clientX-delatX)+"px";
        elementToDrag.style.top=(e.clientY-delatY)+"px";
        e.cancelBubble=true;
        }    function upHandler(e) {
        if(!e) e=window.event;  
        document.detachEvent("onmouseup",upHandler);
        document.detachEvent("onmousemove",moveHandler);
        e.cancelBubble=true;
        elementToDrag.style.left=elementToDrag.style.left;
        elementToDrag.style.top=elementToDrag.style.top;
        }   
    }</script>
    <input type="image" src="csdn.gif" id=bao
    style="position:absolute;left:0px;top:0px;"
    onmousedown="beginDrag(this,event);">
    <input type="hidden" name="leftpos">
    <input type="hidden" name="toppos">
      

  3.   

    提供个思路,用hidden来存储值..demo.htm<script>
    //用hidden记录层位置变化,刷新后,位置不变.
    /* Start */
    window.onbeforeunload=function()
    {
        document.all.leftpos.value = parseInt(bao.style.left);
        document.all.toppos.value = parseInt(bao.style.top);
    }
    window.onload=function()
    {
        if(document.all.leftpos.value!="")bao.style.left=document.all.leftpos.value+"px";
        if(document.all.toppos.value!="")bao.style.top=document.all.toppos.value+"px";
    }
    /* End */function beginDrag(elementToDrag,event)  {    var delatX=event.clientX-parseInt(elementToDrag.style.left);
        var delatY=event.clientY-parseInt(elementToDrag.style.top);
        document.attachEvent("onmousemove",moveHandler);
        document.attachEvent("onmouseup",upHandler);
        event.cancelBubble=true;
        event.returnValue=false;    function moveHandler(e)  {
        if(!e) e=window.event;  
        elementToDrag.style.left=(e.clientX-delatX)+"px";
        elementToDrag.style.top=(e.clientY-delatY)+"px";
        e.cancelBubble=true;
        }    function upHandler(e) {
        if(!e) e=window.event;  
        document.detachEvent("onmouseup",upHandler);
        document.detachEvent("onmousemove",moveHandler);
        e.cancelBubble=true;
        elementToDrag.style.left=elementToDrag.style.left;
        elementToDrag.style.top=elementToDrag.style.top;
        }   
    }</script>
    <input type="image" src="csdn.gif" id=bao
    style="position:absolute;left:0px;top:0px;"
    onmousedown="beginDrag(this,event);">
    <input type="hidden" name="leftpos">
    <input type="hidden" name="toppos">
      

  4.   

    我来试试:灰豆大哥的那个在刷新后可以保持原来的状态,但是重新读取后就不行了。所以Attribute来做就OK了:
    --------------------------
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title></title>
    <style>  
    .userData {behavior:url(#default#userdata);}
    </style>
    <script language="javascript">
    <!--
    /**********************************
    ** 2004-5-14 16:10
    ** DSclub
    ** 兀儿
    ** [email protected]
    ***********************************
    **重新装入页面后也可保持当前的状态
    **
    **********************************/
    var garrLayoutParts;

    function loadPage()
    {
    loadSavedShowHide();
    } function saveState()
    {
    var tempobj = document.all("oLayer");

    tempobj.setAttribute("DSy", oLayer.style.top);
    tempobj.setAttribute("DSx", oLayer.style.left);

    tempobj.save("DSclub");
    } function loadSavedShowHide()
    {
    var tempobj = document.all("oLayer");

    tempobj.load("DSclub");

    oLayer.style.top = tempobj.getAttribute("DSy");
    oLayer.style.left = tempobj.getAttribute("DSx");
    }

    function beginDrag(elementToDrag,event)  { var delatX=event.clientX-parseInt(elementToDrag.style.left);
    var delatY=event.clientY-parseInt(elementToDrag.style.top);
    document.attachEvent("onmousemove",moveHandler);
    document.attachEvent("onmouseup",upHandler);
    event.cancelBubble=true;
    event.returnValue=false; function moveHandler(e)  {
    if(!e) e=window.event;  
    elementToDrag.style.left=(e.clientX-delatX)+"px";
    elementToDrag.style.top=(e.clientY-delatY)+"px";
    e.cancelBubble=true;
    } function upHandler(e) {
    if(!e) e=window.event;  
    document.detachEvent("onmouseup",upHandler);
    document.detachEvent("onmousemove",moveHandler);
    e.cancelBubble=true;
    elementToDrag.style.left=elementToDrag.style.left;
    elementToDrag.style.top=elementToDrag.style.top;
    saveState();
    }   
    } /////////////////////////////////////////////////////////////////////////////////////////////////
    //-->
    </script>
    </head>
    <body onload="loadPage()">
    <div id="oLayer" style="position:absolute;border:1px solid;cursor:hand;" class=userData onmousedown="beginDrag(this,event);">
    永邦-你是我最深爱的人<BR>
    欧阳洋葱<BR>
    爱上了一个人能够为她牺牲就算付出了生命<BR>
    我甘心为了你<BR>
    两个人在一起分享爱的命运<BR>
    永远都不会忘记我们生活点滴<BR>
    你是我最深爱的女人<BR>
    你有最美丽的嘴唇你拥有最动人的眼神<BR>
    你带给我幸福和快乐我是你最深爱的男人<BR>
    我的爱绝对是永恒做什么都值得爱上了一个人
    </div>
    </body>
    </html>
      

  5.   

    为什么我取不出来我的div的scrollLeft的值,总是显示为0;各位请看下
    <div class="ItemDiv" id="divItemBody" style="LEFT: 0px; OVERFLOW: scroll; POSITION: relative; TOP: 0px; HEIGHT: 230px" onscroll='scrollItemBody("HeaderFrame","divItemBody","FooterFrame")' ms_positioning="GridLayout">
    内容太多,不贴了,就是一些输入框等
    </div>
    <div class="SumDiv" id="divFooter" style="TOP: 217px; HEIGHT: 20px" onscroll='scrollSumItem("HeaderFrame","divItemBody","FooterFrame")'>
    内容略
    </div>
    然后我在javascript中取 document.all("divItemBody").scrollLeft能取到,而
    取document.all("divFooter").scrollLeft时,不管滚动条在哪,都是0,不知道
    为什么,请各位大大帮忙了。
      

  6.   

    下边的div,是另一个页面,是动态的数据(已经实现);==============
    这个是什么意思?你用的iframe?
      

  7.   

    ItemDiv和SumDiv 的style是不是不一样?
      

  8.   

    gaofaq(老高) :你好,是的,那个页面是一些动态的数据处理,用的iframe实现;
     gcbxjkh12345(最佳拍档):第一个代码是这样的
    function scrollSumItem(HeaderFrame,divItemBody,divFooter)
    {
    try
    {
    if(divFooter == null) return false;
    document.frames(HeaderFrame).document.body.scrollLeft = document.all(divFooter).scrollLeft;
    document.all(divItemBody).scrollLeft = document.all(divFooter).scrollLeft;
    }
    catch(err)
    {
    alert(err.description);
    return false;
    }

    }
    第二个就不贴了,其实就是读取数据的加载页面。。
      

  9.   

    dsclub(兀儿) :是啊,如我上面贴得style,难道和这个有关系吗?我对div不太熟习啊,帮忙啊,大大!!
      

  10.   

    按照MSDN上所述
    scrollLeftThis property is always 0 for objects that do not have scroll bars. For these objects, setting the property has no effect.所以,这俩个style对滚动条的设置是不是有不同的地方?
      

  11.   

    其实,你的问题我已经解决了!!!
    刷新以后仍然保持刷新前的位置这个和你那个scrollLeft有关系么?
      

  12.   

    哦?你的页面里边有错误啊,根本都没有滚动条阿?怎么实现的?实现后肯定给分,
    要不你帮我看看我的style有什么不同,要不我也全给你分,怎么样?为什么我取得
    scrollLeft全是0啊???
      

  13.   

    噢,是这个代码才对!拿错了:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title></title>
    <style> .userData {behavior:url(#default#userdata);}
    </style>
    <script language="javascript">
    <!--
    /**********************************
    ** 2004-5-14 17:34
    ** DSclub
    ** 兀儿
    ** [email protected]
    ***********************************
    **
    **重新装入页面后也可保持当前的状态
    **
    **********************************/
    var garrLayoutParts;

    function loadPage()
    {
    loadSavedShowHide();
    } function saveState()
    {
    var tempobj = document.all("oLayer");

    tempobj.setAttribute("DSy", oLayer.style.top);
    tempobj.setAttribute("DSx", oLayer.style.left);

    window.status = oLayer.style.top;

    tempobj.save("DSclub");
    } function loadSavedShowHide()
    {
    var tempobj = document.all("oLayer");

    tempobj.load("DSclub");

    oLayer.style.top = ((tempobj.getAttribute("DSy") == null) || (tempobj.getAttribute("DSy") == '')) ? '0px' : tempobj.getAttribute("DSy");
    oLayer.style.left = ((tempobj.getAttribute("DSx") == null) || (tempobj.getAttribute("DSx") ==''))  ? '0px' : tempobj.getAttribute("DSx");
    }

    function beginDrag(elementToDrag,event)  {
    var delatX=event.clientX-parseInt(elementToDrag.style.left);
    var delatY=event.clientY-parseInt(elementToDrag.style.top);
    document.attachEvent("onmousemove",moveHandler);
    document.attachEvent("onmouseup",upHandler);
    event.cancelBubble=true;
    event.returnValue=false; function moveHandler(e)  {
    if(!e) e=window.event;  
    elementToDrag.style.left=(e.clientX-delatX)+"px";
    elementToDrag.style.top=(e.clientY-delatY)+"px";
    e.cancelBubble=true;
    } function upHandler(e) {
    if(!e) e=window.event;  
    document.detachEvent("onmouseup",upHandler);
    document.detachEvent("onmousemove",moveHandler);
    e.cancelBubble=true;
    elementToDrag.style.left=elementToDrag.style.left;
    elementToDrag.style.top=elementToDrag.style.top;
    saveState();
    }   
    } /////////////////////////////////////////////////////////////////////////////////////////////////
    //-->
    </script>
    </head>
    <body onload="loadPage()">
    <div unselectable="on" id="oLayer" style="BORDER:1px solid #999999; CURSOR:hand; POSITION:absolute; font:9pt;color:#BBBBBB;padding:10px;"
    class="userData" onmousedown="beginDrag(this,event);">
    永邦-你是我最深爱的女人<BR>
    <BR>
    爱上了一个人 能够为她牺牲<BR>
    就算付出了生命 我甘心为了你<BR>
    两个人在一起 分享爱的命运<BR>
    永远都不会忘记 我们生活点滴<BR>
    你是我最深爱的女人<BR>
    你有最美丽的嘴唇<BR>
    你拥有最动人的眼神<BR>
    你带给我幸福和快乐<BR>
    我是你最深爱的男人<BR>
    我的爱绝对是永恒<BR>
    做什么都值得爱上了一个人
    </div>
    </body>
    </html>