悬浮的购物车

今天在项目中看到可能会用到这个东西,我做了个小例子,其原理就是把一个层(定位一定要是absolute),在页面滚动的时候,触发事件,改变这个层的位置。
一、定义这个层:
     <div id="div1" style="position: absolute; z-index: 10000; background-color: Red;
        width: 300px; height: 200px;">
        这是一个悬浮的层!
    </div>
二、定义改变层位置的方法:
      function scrolls()
    {
        div1.style.marginTop=document.documentElement.scrollTop+div1.clientHeight;              div1.style.marginLeft=document.documentElement.scrollLeft+document.body.clientWidth - div1.clientWidth;
    }
三、在滚动事件中调用方法:
 window.onscroll = function()
    {
        scrolls();
    }

四、在加载事件中调用方法:
 window.onload=function()
    {
        scrolls();
    }
posted @ 2008-05-14 14:17  point.deng  阅读(512)  评论(1编辑  收藏  举报