`
JaHunter
  • 浏览: 92856 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

css_js_层窗口

    博客分类:
  • js
阅读更多

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<br /><br /><br /><br />


<script>

var isIE = (document.all) ? true : false;

var isIE6 = isIE && ([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 6);


function Each(list, fun){
    for (var i = 0, len = list.length; i < len; i++) { fun(list[i], i); }
};

var $ = function (id) {
    return "string" == typeof id ? document.getElementById(id) : id;
};

var Class = {
create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    }
}
}

Object.extend = function(destination, source) {
for (var property in source) {
    destination[property] = source[property];
}
return destination;
}

Function.prototype.bind = function(object) {
var __method = this, args = Array.apply(null, arguments); args.shift();
return function() {
    return __method.apply(object, args);
}
}

var OverLay = Class.create();
OverLay.prototype = {
initialize: function(overlay, options) {
    this.Lay = $(overlay);//覆盖层
   
    //ie6设置覆盖层大小程序
    this._size = function(){};
   
    this.SetOptions(options);
   
    this.Color = this.options.Color;
    this.Opacity = parseInt(this.options.Opacity);
    this.zIndex = parseInt(this.options.zIndex);
   
    this.Set();
},
//设置默认属性
SetOptions: function(options) {
    this.options = {//默认值
        Color:        "#fff",//背景色
        Opacity:    50,//透明度(0-100)
        zIndex:        1000//层叠顺序
    };
    Object.extend(this.options, options || {});
},
//创建
Set: function() {
    this.Lay.style.display = "none";
    this.Lay.style.zIndex = this.zIndex;
    this.Lay.style.left = this.Lay.style.top = 0;
   
    if(isIE6){
        this.Lay.style.position = "absolute";
        this._size = function(){
            this.Lay.style.width = Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth) + "px";
            this.Lay.style.height = Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight) + "px";
        }.bind(this);
        //遮盖select
        this.Lay.innerHTML = '<iframe style="position:absolute;top:0;left:0;width:100%;height:100%;filter:alpha(opacity=0);"></iframe>'
    } else {
        this.Lay.style.position = "fixed";
        this.Lay.style.width = this.Lay.style.height = "100%";
    }
},
//显示
Show: function() {
    //设置样式
    this.Lay.style.backgroundColor = this.Color;
    //设置透明度
    if(isIE){
        this.Lay.style.filter = "alpha(opacity:" + this.Opacity + ")";
    } else {
        this.Lay.style.opacity = this.Opacity / 100;
    }
    //兼容ie6
    if(isIE6){ this._size(); window.attachEvent("onresize", this._size); }
   
    this.Lay.style.display = "block";
},
//关闭
Close: function() {
    this.Lay.style.display = "none";
    if(isIE6){ window.detachEvent("onresize", this._size); }
}
};


var LightBox = Class.create();
LightBox.prototype = {
initialize: function(box, overlay, options) {
   
    this.Box = $(box);//显示层
   
    this.OverLay = new OverLay(overlay, options);//覆盖层
   
    this.SetOptions(options);
   
    this.Fixed = !!this.options.Fixed;
    this.Over = !!this.options.Over;
    this.Center = !!this.options.Center;
    this.onShow = this.options.onShow;
   
    this.Box.style.zIndex = this.OverLay.zIndex + 1;
    this.Box.style.display = "none";
   
    //兼容ie6用的属性
    if(isIE6){ this._top = this._left = 0; this._select = []; }
},
//设置默认属性
SetOptions: function(options) {
    this.options = {//默认值
        Fixed:        false,//是否固定定位
        Over:        true,//是否显示覆盖层
        Center:        false,//是否居中
        onShow:        function(){}//显示时执行
    };
    Object.extend(this.options, options || {});
},
//兼容ie6的固定定位程序
_fixed: function(){
    var iTop = document.documentElement.scrollTop - this._top + this.Box.offsetTop, iLeft = document.documentElement.scrollLeft - this._left + this.Box.offsetLeft;
    //居中时需要修正
    if(this.Center){ iTop += this.Box.offsetHeight / 2; iLeft += this.Box.offsetWidth / 2; }
   
    this.Box.style.top = iTop + "px"; this.Box.style.left = iLeft + "px";
   
    this._top = document.documentElement.scrollTop; this._left = document.documentElement.scrollLeft;
},
//显示
Show: function(options) {
    //固定定位
    if(this.Fixed){
        if(isIE6){
            //兼容ie6
            this.Box.style.position = "absolute";
            this._top = document.documentElement.scrollTop; this._left = document.documentElement.scrollLeft;
            window.attachEvent("onscroll", this._fixed.bind(this));
        } else {
            this.Box.style.position = "fixed";
        }
    } else {
        this.Box.style.position = "absolute";
    }
    //覆盖层
    if(this.Over){
        //显示覆盖层,覆盖层自带select隐藏
        this.OverLay.Show();
    } else {
        //ie6需要把不在Box上的select隐藏
        if(isIE6){
            this._select = [];
            var oThis = this;
            Each(document.getElementsByTagName("select"), function(o){
                if(oThis.Box.contains ? !oThis.Box.contains(o) : !(oThis.Box.compareDocumentPosition(o) & 16)){
                    o.style.visibility = "hidden"; oThis._select.push(o);
                }
            })
        }
    }
   
    this.Box.style.display = "block";
   
    //居中
    if(this.Center){
        this.Box.style.top = this.Box.style.left = "50%";
        //显示后才能获取
        var iTop = - this.Box.offsetHeight / 2, iLeft = - this.Box.offsetWidth / 2;
        //ie6或不是固定定位要修正
        if(!this.Fixed || isIE6){ iTop += document.documentElement.scrollTop; iLeft += document.documentElement.scrollLeft; }
        this.Box.style.marginTop = iTop + "px"; this.Box.style.marginLeft = iLeft + "px";
    }
   
    this.onShow();
},
//关闭
Close: function() {
    this.Box.style.display = "none";
    this.OverLay.Close();
    if(isIE6){ window.detachEvent("onscroll", this._fixed); Each(this._select, function(o){ o.style.visibility = "visible"; }); }
}
};


function addEventHandler(oTarget, sEventType, fnHandler) {
    if (oTarget.addEventListener) {
        oTarget.addEventListener(sEventType, fnHandler, false);
    } else if (oTarget.attachEvent) {
        oTarget.attachEvent("on" + sEventType, fnHandler);
    } else {
        oTarget["on" + sEventType] = fnHandler;
    }
};

function removeEventHandler(oTarget, sEventType, fnHandler) {
    if (oTarget.removeEventListener) {
        oTarget.removeEventListener(sEventType, fnHandler, false);
    } else if (oTarget.detachEvent) {
        oTarget.detachEvent("on" + sEventType, fnHandler);
    } else {
        oTarget["on" + sEventType] = null;
    }
};

if(!isIE){
    HTMLElement.prototype.__defineGetter__("currentStyle", function () {
        return this.ownerDocument.defaultView.getComputedStyle(this, null);
    });
}

//拖放程序
var Drag = Class.create();
Drag.prototype = {
//拖放对象,触发对象
initialize: function(obj, drag, options) {
    var oThis = this;
   
    this._obj = $(obj);//拖放对象
    this.Drag = $(drag);//触发对象
    this._x = this._y = 0;//记录鼠标相对拖放对象的位置
    //事件对象(用于移除事件)
    this._fM = function(e){ oThis.Move(window.event || e); }
    this._fS = function(){ oThis.Stop(); }
   
    this.SetOptions(options);
   
    this.Limit = !!this.options.Limit;
    this.mxLeft = parseInt(this.options.mxLeft);
    this.mxRight = parseInt(this.options.mxRight);
    this.mxTop = parseInt(this.options.mxTop);
    this.mxBottom = parseInt(this.options.mxBottom);
    this.mxContainer = this.options.mxContainer;
   
    this.onMove = this.options.onMove;
    this.Lock = !!this.options.Lock;
   
    this._obj.style.position = "absolute";
    addEventHandler(this.Drag, "mousedown", function(e){ oThis.Start(window.event || e); });
},
//设置默认属性
SetOptions: function(options) {
    this.options = {//默认值
        Limit:            false,//是否设置限制(为true时下面参数有用,可以是负数)
        mxLeft:            0,//左边限制
        mxRight:        0,//右边限制
        mxTop:            0,//上边限制
        mxBottom:        0,//下边限制
        mxContainer:    null,//指定限制在容器内
        onMove:            function(){},//移动时执行
        Lock:            false//是否锁定
    };
    Object.extend(this.options, options || {});
},
//准备拖动
Start: function(oEvent) {
    if(this.Lock){ return; }
    //记录鼠标相对拖放对象的位置
    this._x = oEvent.clientX - this._obj.offsetLeft;
    this._y = oEvent.clientY - this._obj.offsetTop;
    //mousemove时移动 mouseup时停止
    addEventHandler(document, "mousemove", this._fM);
    addEventHandler(document, "mouseup", this._fS);
    //使鼠标移到窗口外也能释放
    if(isIE){
        addEventHandler(this.Drag, "losecapture", this._fS);
        this.Drag.setCapture();
    }else{
        addEventHandler(window, "blur", this._fS);
    }
},
//拖动
Move: function(oEvent) {
    //判断是否锁定
    if(this.Lock){ this.Stop(); return; }
    //清除选择(ie设置捕获后默认带这个)
    window.getSelection && window.getSelection().removeAllRanges();
    //当前鼠标位置减去相对拖放对象的位置得到offset位置
    var iLeft = oEvent.clientX - this._x, iTop = oEvent.clientY - this._y;
    //设置范围限制
    if(this.Limit){
        //如果设置了容器,因为容器大小可能会变化所以每次都要设
        if(!!this.mxContainer){
            this.mxLeft = this.mxTop = 0;
            this.mxRight = this.mxContainer.clientWidth;
            this.mxBottom = this.mxContainer.clientHeight;
        }
        //获取超出长度
        var iRight = iLeft + this._obj.offsetWidth - this.mxRight, iBottom = iTop + this._obj.offsetHeight - this.mxBottom;
        //这里是先设置右边下边再设置左边上边,可能会不准确
        if(iRight > 0) iLeft -= iRight;
        if(iBottom > 0) iTop -= iBottom;
        if(this.mxLeft > iLeft) iLeft = this.mxLeft;
        if(this.mxTop > iTop) iTop = this.mxTop;
    }
    //设置位置
    //由于offset是把margin也算进去的所以要减去
    this._obj.style.left = iLeft - (parseInt(this._obj.currentStyle.marginLeft) || 0) + "px";
    this._obj.style.top = iTop - (parseInt(this._obj.currentStyle.marginTop) || 0) + "px";   
    //附加程序
    this.onMove();
},
//停止拖动
Stop: function() {
    //移除事件
    removeEventHandler(document, "mousemove", this._fM);
    removeEventHandler(document, "mouseup", this._fS);
    if(isIE){
        removeEventHandler(this.Drag, "losecapture", this._fS);
        this.Drag.releaseCapture();
    }else{
        removeEventHandler(window, "blur", this._fS);
    }
}
};

</script>


<style>
.lightbox{width:300px;background:#FFFFFF; top:20%; left:20%;border:1px solid #ccc;line-height:25px; margin:0;}
.lightbox dt{background:#f4f4f4; padding:5px; cursor:move;}
</style>

<dl id="idBox" class="lightbox">
<dt id="idBoxHead"><b>LightBox</b> </dt>
<dd>
    选择效果显示
    <br /><br />
    <input name="" type="button" value=" 关闭 " id="idBoxCancel" /><br /><br />
</dd>
</dl>


<div id="idOverlay"></div>

 

<div style="margin:0 auto; width:700px; border:1px solid #000000;">


<input name="" type="button" value="关闭覆盖层" id="btnOverlay" />
<input name="" type="button" value="黑色覆盖层" id="btnOverColor" />
<input name="" type="button" value="全透覆盖层" id="btnOverOpacity" />
<input name="" type="button" value="取消居中" id="btnCenter" />
<br /><br />
选择效果:<br />
<select id="idEffect">
<option value="0">还原效果</option>
<option value="4">定位效果</option>
<option value="2">设置拖放</option>
<option value="6">视框定位拖放</option>
</select>
<input name="" type="button" value=" 打开 " id="idBoxOpen" />

<script>


var box = new LightBox("idBox", "idOverlay", { Center: true });


var drag = new Drag("idBox", "idBoxHead", { mxContainer: document.documentElement, Lock: true });


$("idBoxCancel").onclick = function(){ box.Close(); }
$("idBoxOpen").onclick = function(){ box.Show(); }

$("idEffect").onchange = function(){
    box.Close();
   
    switch (parseInt(this.value)) {
        case 4 :
            box.Fixed = true;
            drag.Lock = true;
            drag.Limit = false;
            break;
        case 2 :
            box.Fixed = false;
            drag.Lock = false;
            drag.Limit = false;
            break;
        case 6 :
            box.Fixed = true;;
            drag.Lock = false;
            drag.Limit = true;
            break;
        case 0 :
        default :
            box.Fixed = false;
            drag.Lock = true;
            drag.Limit = false;
    }
}


$("btnOverlay").onclick = function(){
    box.Close();
    if(box.Over){
        box.Over = false;
        this.value = "打开覆盖层";
    } else {
        box.Over = true;
        this.value = "关闭覆盖层";
    }
}

$("btnOverColor").onclick = function(){
    box.Close();
    box.Over = true;
    if(box.OverLay.Color == "#fff"){
        box.OverLay.Color = "#000";
        this.value = "白色覆盖层";
    } else {
        box.OverLay.Color = "#fff"
        this.value = "黑色覆盖层";
    }
}

$("btnOverOpacity").onclick = function(){
    box.Close();
    box.Over = true;
    if(box.OverLay.Opacity == 0){
        box.OverLay.Opacity = 50;
        this.value = "全透覆盖层";
    } else {
        box.OverLay.Opacity = 0;
        this.value = "半透覆盖层";
    }
}

$("btnCenter").onclick = function(){
    box.Close();
    if(box.Center){
        box.Center = false;
        this.value = "设置居中";
    } else {
        box.Center = true;
        this.value = "取消居中";
    }
}


</script>

 


<br /><br />

 

<br />
<br /><br />
<br /><br />
<br /><br /><br /><br />
<br /><br />
<br /><br />
<br /><br />
<br /><br />
<br /><br />
<br /><br />
<br />


</div>

</html>

来自:http://hi.baidu.com/bing2liuliu/blog/item/cff28af82c850d53242df2dd.html

分享到:
评论

相关推荐

    div_css_js弹出层有遮罩

    在网页设计中,"div_css_js弹出层有遮罩"是一个常见的交互设计技术,用于创建弹出式窗口或对话框,通常用于显示通知、表单、广告等。这一技术结合了HTML的`&lt;div&gt;`元素、CSS样式以及JavaScript/jQuery的动态效果,以...

    HTML_CSS_JavaScript网页制作(新).pdf

    标题《HTML_CSS_JavaScript网页制作(新).pdf》所指向的知识点涵盖了网页设计与开发中使用的三种核心技术:HTML、CSS和JavaScript。这三个技术构成了现代网页制作的基石,是任何希望深入了解和从事前端开发的人都...

    Interaktivny_CSS_css_

    10. **CSS-in-JS**:这是一种将CSS样式写入JavaScript中的趋势,如styled-components,它提供了更灵活的样式管理,并能更好地结合组件化开发。 文件名列表中提到了“3.png”、“style3.css”和两个HTML文件,这可能...

    moban4435_js_css_web_

    在这个名为"moban4435"的压缩包中,我们可能找到了与JavaScript(js)、CSS(层叠样式表)和WEB相关的网页设计模板或资源。这三者是构建现代网页的核心技术。 JavaScript是一种强大的编程语言,主要负责网页的动态...

    css_js_jquery语言手册.zip

    《CSS、JavaScript与jQuery语言手册》是一套全面深入学习前端开发核心技术的重要参考资料。这份压缩包包含三本电子手册,分别是“CSS 3.0.chm”、“Javascipt语言手册.chm”以及“jQuery1.11.0_20140330.chm”,它们...

    html_css_javascript初级

    6. **多窗口**:`&lt;frameset&gt;`和`&lt;frame&gt;`可以创建多窗口或框架布局,但现在已较少使用,更推荐使用CSS布局。 7. **特殊字符**:HTML提供预定义的实体(如`&amp;`表示"&",`&lt;`表示",`&gt;`表示"&gt;")来插入特殊...

    在电子中运行后台任务的示例_JavaScript_CSS_下载.zip

    这个名为“在电子中运行后台任务的示例_JavaScript_CSS_下载.zip”的压缩包可能包含了一个示例项目,用于演示如何在Electron应用中执行后台任务,如数据同步、文件下载等。下面将详细介绍相关知识点: 1. **...

    OSS-通用参考流程_CSS_JavaScript_下载.zip

    - 使用JS SDK:学习如何在JavaScript中调用OSS的API,进行文件的上传、删除、列举等操作。 - 文件预览:实现客户端的文件预览功能,特别是图片。 - 上传进度显示:展示文件上传的进度,提升用户体验。 - 错误...

    vue-im-master_html_js_css_vue_

    在这个项目中,HTML文件可能包含了聊天窗口的基本结构,如对话框、输入框、发送按钮等元素,通过特定的ID或class与JavaScript和CSS进行交互。 接着,JavaScript(JS)负责项目的动态功能。在Vue.js框架下,JS代码...

    unigui_调用外部js与css_左侧导航栏

    在这个主题中,我们将深入探讨如何在Unigui项目中调用外部JavaScript库和CSS样式表,以及如何创建一个功能完备的左侧导航栏。 首先,调用外部JavaScript库是Unigui应用扩展功能的关键步骤。这通常涉及到在HTML页面...

    sublime3工具+JS代码校验+HTML_CSS_JS格式化

    2. **分割编辑**:你可以将编辑器窗口分成多个部分,同时查看和编辑不同的文件,这对于对比或合并代码非常有用。 3. **快捷命令**:通过`Cmd+P`(Mac)或`Ctrl+P`(Windows/Linux)快速打开“Go to Anything”面板,...

    Vanilla JS工具,用于在干净的叠加框中显示图像和视频_JavaScript_CSS_代码_下载

    "Vanilla JS工具,用于在干净的叠加框中显示图像和视频" 指的是一款基于纯JavaScript(Vanilla JS)开发的轻量级工具,它的主要功能是在网页上以干净、简洁的弹出层(叠加框)形式展示图像和视频内容。这种工具通常...

    EditPlus_智能格式化工具(css,JS)

    在编写HTML、CSS或JavaScript时,可以直接在内置的浏览器预览窗口查看效果,无需频繁切换到外部浏览器进行测试。 【CSS】是层叠样式表(Cascading Style Sheets)的简称,用于控制网页元素的样式,包括颜色、字体、...

    响应式布局开发代码资料_html_js_css_

    JavaScript(JS)常用于增加页面的交互性。在响应式布局中,JS可以用来检测设备特征,动态改变布局,或者在用户滚动页面时触发某些效果。例如,你可以使用`window.innerWidth`来获取浏览器窗口的宽度,据此调整元素...

    js+css_点击大图片悬停显示

    一、JavaScript与CSS简介 JavaScript是一种广泛用于网页动态效果的编程语言,负责处理用户的交互行为,如点击事件。CSS(层叠样式表)则用于控制网页的布局和视觉样式,如颜色、大小、位置等。 二、实现点击显示大...

    Jdbc_xml_html_css_js选择题(含答案).doc

    正确答案缺少结束标签,应该是`&lt;script src="myJs.js" type="text/javascript"&gt;&lt;/script&gt;`。 以上是各个问题的详细解答,涵盖了JDBC的API使用、PreparedStatement的优势、DAO模式、XML解析、HTML标签属性以及CSS和...

    js_css_模仿_mac_Dock

    在网页设计中,为了提供类似桌面操作系统用户体验,开发者有时会使用JavaScript(JS)和层叠样式表(CSS)来模仿Mac操作系统的Dock栏效果。Mac Dock是苹果操作系统的一个标志性功能,它允许用户快速访问常用的应用...

    根据屏幕分辨率调用css_asp.net_

    这通常涉及到JavaScript或服务器端的C#代码来检测浏览器窗口的大小,并据此作出响应。 描述中提到,只需几行判断语句即可实现这一功能。下面将详细介绍如何在ASP.NET中根据屏幕分辨率加载不同的CSS文件。 1. **...

    将彩色位图图像转换为彩色SVG矢量图像_JavaScript_html_css_代码_下载

    SVGcode 是一个渐进式 Web 应用程序,可让您将 JPG、PNG、GIF、WebP、AVIF 等光栅图像转换为 SVG 格式的矢量图形。它使用文件系统访问 API、异步剪贴板 API、文件处理 API 和窗口控件覆盖自定义

    一个简单的JavaScript聊天机器人,带有指向智能API的链接。_JavaScript_CSS_下载.zip

    - `script.js` - JavaScript文件,包含聊天机器人的逻辑。 - `api-keys.json` - 可能用于存储API访问密钥的配置文件。 - `data.json`或`.sqlite` - 用于存储聊天历史的数据库文件。 这个项目对于初学者来说是一个很...

Global site tag (gtag.js) - Google Analytics