`
f303153041
  • 浏览: 44858 次
社区版块
存档分类
最新评论

可以拖动的悬浮窗

阅读更多
style{
.show {
display: none;
background: transparent;
text-align: center;
position: absolute;
z-index: 2;
width: 770px;
height: 500px;
}

.title {
background: #C0C0C0;
width: 770px
}
}
<script>
//移动DIV

function closePreWin(){
       preWin.style.display = "none";
     }
    
     
      $(document).ready(function() 
        { 
            $(".title").mousedown(function(e)//e鼠标事件 
            { 
                $(this).css("cursor","move");//改变鼠标指针的形状 
                 
                var offset = $(this).offset();//DIV在页面的位置 
                var x = e.pageX - offset.left;//获得鼠标指针离DIV元素左边界的距离 
                var y = e.pageY - offset.top;//获得鼠标指针离DIV元素上边界的距离 
                $(document).bind("mousemove",function(ev)//绑定鼠标的移动事件,因为光标在DIV元素外面也要有效果,所以要用doucment的事件,而不用DIV元素的事件 
                { 
                    $(".show").stop();//加上这个之后 
                     
                    var _x = ev.pageX - x;//获得X轴方向移动的值 
                    var _y = ev.pageY - y;//获得Y轴方向移动的值 
                     
                    $(".show").animate({left:_x+"px",top:_y+"px"},10); 
                }); 
                 
            }); 
             
            $(document).mouseup(function() 
            { 
                $(".title").css("cursor","default"); 
                $(this).unbind("mousemove"); 
            }) 
        });

</script>


<div id="firstDiv" style="position: absolute; z-index: 1;">
</div>

<div id="preWin" class="show">
        <div id="title" class="title">
<p align="right">
<span onClick="closePreWin()">关闭</span>
</p>
</div>
<div id="pre">content</div>
</div>

拖动title 即可拖动 preWin div了 preWin 是悬浮在firstDiv之上
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics