可以拖动的弹出层

/*
  弹出层
*/
//弹出层(divbgId背景层ID,conId显示的内容层ID)
function openLayer(divBgId, divConId) {
    var arrayPageSize   = getPageSize();  //调用getPageSize()函数
    var arrayPageScroll = getPageScroll(); //调用getPageScroll()函数

    //显示背景层
    var bodyBack = document.getElementById(divBgId);
    bodyBack.style.position = "absolute";
    bodyBack.style.width = "100%";
    bodyBack.style.height = (arrayPageSize[1] + 'px');
    bodyBack.style.zIndex = 999;
    bodyBack.style.top = 0;
    bodyBack.style.left = 0;
    bodyBack.style.filter = "alpha(opacity=50)";
    bodyBack.style.opacity = 0.5;
    bodyBack.style.background = "#ddddff";
    bodyBack.style.display = "block";
   
    //显示内容层,让弹出层在页面中居中显示(个性)
    var popObj = document.getElementById(divConId);
    var arrayConSize = getConSize(divConId);
    popObj.style.position = "absolute";
    popObj.style.zIndex = 9999;
    popObj.style.top = arrayPageScroll[1] + (arrayPageSize[3] - arrayConSize[1]) / 2 + 'px';
    popObj.style.left = (arrayPageSize[2] - arrayConSize[0]) / 2 + 'px';
    popObj.style.display = "block";
}

//获取内容层内容原始尺寸
function getConSize(conId)
{
    var conObj = document.getElementById(conId);
    var w = parseInt(conObj.style.width);
    var h = parseInt(conObj.style.height);
    var arrayConSize = [w, h];
    return arrayConSize;
}
//获取滚动条的高度
function getPageScroll(){
    var yScroll;
    if (self.pageYOffset)
    {
        yScroll = self.pageYOffset;
    }
    else if (document.documentElement && document.documentElement.scrollTop)
    {
        yScroll = document.documentElement.scrollTop;
    }
    else if (document.body)
    {
        yScroll = document.body.scrollTop;
    }
    arrayPageScroll = new Array('', yScroll)

    return arrayPageScroll;
}
//获取页面实际大小
function getPageSize(){
    var xScroll,yScroll;
    if (window.innerHeight && window.scrollMaxY)
    {
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    }
    else if (document.body.scrollHeight > document.body.offsetHeight)
    {
        sScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    }
    else {
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    var windowWidth,windowHeight;
    if (self.innerHeight) {
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    }
    else if (document.documentElement  &&  document.documentElement.clientHeight) {
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    }
    else if (document.body) {
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }
    var pageWidth,pageHeight
    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    }
    else {
        pageHeight = yScroll;
    }
    if(xScroll < windowWidth) {
        pageWidth = windowWidth;
    }
    else {
        pageWidth = xScroll;
    }
    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight)

    return arrayPageSize;
}

//关闭弹出层
function closeLayer(divBgId, divConId) {
    document.getElementById(divConId).style.display = "none";
    document.getElementById(divBgId).style.display = "none";
    //document.getElementById(divBgId).style.width = "0px";
    //document.getElementById(divBgId).style.height = "0px";
    return false;
}
   
/*
* 拖拽效果
*/
var oldX, oldY;  // 记录鼠标移动事件发生前鼠标的位置
var dragElem;  // 记录被拖曳的对象
var drag = false;
function $(nodeId) {
    return document.getElementById(nodeId);
}
// 拖动开始
function StartDrag(evt, nodeId) {
    //dragElem = $("divInfo");
    dragElem = $(nodeId);
    evt = evt || event;   // 为了兼容IE和firefox,firefox执行evt,IE则执行evt=event
    oldX = evt.clientX - parseInt(dragElem.style.left);
    oldY = evt.clientY - parseInt(dragElem.style.top);
    drag = true;
    document.onmousemove = Drag;
}
// 拖动的动作
function Drag(evt) {
    if (drag == true) {
        // 为了兼容IE和firefox,firefox执行evt,IE则执行evt=event
        evt = evt || window.event;  
        dragElem.style.top = (evt.clientY - oldY) + 'px';
        dragElem.style.left = (evt.clientX - oldX) + 'px';
    }
}
// 拖曳结束,释放onmousemove事件执行函数
function StopDrag() {
    drag = false;
}

例:

<div id="divBg" style="display: none;"></div>

<div id="divInfo" style="width: 500px; height: 200px; border: 1px solid #ff000; background-color: #ffffff;
            display: none;">
            <table cellpadding="0" cellspacing="0" style="width: 500px; height: 200px; border: 1px solid #c3d9ff;
                border-collapse: collapse;">
                <tr>
                    <td colspan="2" class="formtable_title_td" style="background-color: #c3d9ff;" οnmοuseοver="this.style.cursor='move';"
                        οnmοuseοut="this.style.cursor='';" οnmοusedοwn="StartDrag(event,'divInfo');"
                        οnmοusemοve="Drag(event);" οnmοuseup="StopDrag();">
                        <div style="margin-top: 0px;">
                            修改密码</div>
                        <img id="imgClose" src="../../Images/mainFrame/win_close.gif" alt="关闭" style="width: 15px;
                            height: 15px; float: right; margin-right: 15px; margin-top: -15px; cursor: hand;"
                            οnclick='closeLayer("divBg", "divInfo")' />
                    </td>
                </tr>
                <tr>
                    <td class="formtable_head_td" width="25%">
                        用户名:
                    </td>
                    <td class="formtable_body_content_td" style="height: 34px">
                        <asp:TextBox ID="txtUserName" runat="server" CssClass="formtable_body_input_text0"
                            Width="200px" MaxLength="20"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="formtable_head_td">
                        密&nbsp;&nbsp;码:
                    </td>
                    <td class="formtable_body_content_td" style="height: 29px">
                        <asp:TextBox ID="txtPassword" runat="server" CssClass="formtable_body_input_text0"
                            Width="200px" MaxLength="20"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="formtable_head_td" height="40px">
                    </td>
                    <td class="formtable_body_submit_td" style="background-color: #ffffff;">
                        <asp:Button ID="btnAdd" runat="server" CssClass="btn_3words" Text="保存" OnClick="btnAdd_Click">
                        </asp:Button>&nbsp;
                        <input class="btn_3words" type="button" value="取消" οnclick='closeLayer("divBg", "divInfo")' />
                    </td>
                </tr>
            </table>

显示是调用openLayer('divBg','divInfo');

转载于:https://my.oschina.net/u/235267/blog/109558

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值