JS弹出可拖动层,并蒙住页面

其实这个效果是很常见的,很多现在的网站上都有这个效果,它的主要部分都是用JS实现的,昨天注意看了一下,原理说通了还是很简单的,不过我一开始什么都不知道.所以做起来很麻烦..换了很多的方案.,.

1)首先是要弹出一个层来,这个我想很简单.一开始.把这个层隐藏了,在方法中显示就行了,

2)然后是要禁用整个页面,这里我走了很多的弯路.,其实它也是一个层,不过是加了点小东西在里面,

3)然后是弹出的这个层要可以拖动,

先在页面上布几个层,分别如下;

<div id="ly" style="position: absolute; top: 0px; filter: alpha(opacity=60); background-color: #777;
            z-index: 2; left: 0px; display: none;">

这个层是蒙住页面的层,filter: alpha(opacity=60); 应该就是禁用那个效果的滤镜吧,

<div>
                <a οnclick="show()">弹出</a>
            </div>

<div id="divTest" style="position: absolute; z-index: 3; width: 540; height: 170px;
            background-color: Yellow; display: none; top: 100px; left: 100px;">
            <div id="dd" style="background-color: Red; width: 365px; height: 20px; float: left;"
                οnmοusedοwn="down()">
            </div>
            <div style="background-color: Red; width: 35px; height: 20px;">
                <a οnclick="closes()">关闭</a>
            </div>
        </div>

function show()
    {
        document.all.ly.style.display="block";  
        document.all.ly.style.width=document.body.clientWidth+20;
        document.all.ly.style.height=document.body.clientHeight+20;
        document.all.divTest.style.display='block';  
        document.getElementById("divTest").style.visibility="visible";
    }
    function closes()
    {
        if(window.confirm("关闭这个层"))
        {
            document.getElementById("divTest").style.visibility="hidden";
            document.all.ly.style.display='none'
        }
    }

当点击"弹出"时,显示divTest层,并把ly这个蒙层显示出来,锁定它,点关闭时,隐藏divTest,同时,隐藏ly.

这样就完成了弹出蒙层的效果了,接着是拖动弹出的层,这里主要调用以下几个方法:

var px=0;
    var py=0;
    var begin=false;
    var topDiv;
    function down()
    {
        begin=true;
        document.getElementById("divTest").style.cursor= "hand";
        event.srcElement.setCapture();  
        px=document.getElementById("divTest").style.pixelLeft - event.x;
        py=document.getElementById("divTest").style.pixelTop - event.y;
    }
    function document.onmousemove()
    {
        if(begin)
        {
            document.getElementById("divTest").style.pixelLeft = px+event.x;
            document.getElementById("divTest").style.pixelTop = py+event.y;
        }
      
    }
    function document.onmouseup()
    {
        begin=false;
        document.getElementById("divTest").style.cursor= "default";
        event.srcElement.releaseCapture();  
    }

前面的divTest中有οnmοusedοwn="down()" 后面几个方法的写法有点像C#中的匿名方法,第一次使用,还是不错.

下面是整个HTML的代码:

<html>
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div id="divTest" style="position: absolute; z-index: 3; width: 540; height: 170px;
            background-color: Yellow; display: none; top: 100px; left: 100px;">
            <div id="dd" style="background-color: Red; width: 365px; height: 20px; float: left;"
                οnmοusedοwn="down()">
            </div>
            <div style="background-color: Red; width: 35px; height: 20px;">
                <a οnclick="closes()">关闭</a>
            </div>
        </div>
        <div id="ly" style="position: absolute; top: 0px; filter: alpha(opacity=60); background-color: #777;
            z-index: 2; left: 0px; display: none;">
        </div>
        <div id="main" style="background-color: Azure; height:700px;">
            <div>
                <a οnclick="show()">弹出</a>
            </div>
            <div>
                <input type="button" id="t" οnclick="javascript:alert('ads');" value="Value" />
            </div>
            <br />
        </div>
    </form>

    <script type="text/javascript">
    function show()
    {
        document.all.ly.style.display="block";  
        document.all.ly.style.width=document.body.clientWidth+20;
        document.all.ly.style.height=document.body.clientHeight+20;
        document.all.divTest.style.display='block';  
        document.getElementById("divTest").style.visibility="visible";
    }
    function closes()
    {
        if(window.confirm("关闭这个层"))
        {
            document.getElementById("divTest").style.visibility="hidden";
            document.all.ly.style.display='none'
        }
    }
   
   
    var px=0;
    var py=0;
    var begin=false;
    var topDiv;
    function down()
    {
        begin=true;
        document.getElementById("divTest").style.cursor= "hand";
        event.srcElement.setCapture();  
        px=document.getElementById("divTest").style.pixelLeft - event.x;
        py=document.getElementById("divTest").style.pixelTop - event.y;
    }
    function document.onmousemove()
    {
        if(begin)
        {
            document.getElementById("divTest").style.pixelLeft = px+event.x;
            document.getElementById("divTest").style.pixelTop = py+event.y;
        }
      
    }
    function document.onmouseup()
    {
        begin=false;
        document.getElementById("divTest").style.cursor= "default";
        event.srcElement.releaseCapture();  
    }
   
    </script>

</body>
</html>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值