代码改变世界

LightBox

2009-09-19 13:47  BlueDream  阅读(521)  评论(0编辑  收藏  举报
<!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>
  <title> LightBox </title>
  <style type="text/css">
  #box {
    text-align:right;
    background-color: #fff;
    border: 20px solid #000000;
  }
  </style>
 </head>
 <body>
 <script type="text/javascript">
    var $ = function(id){
        return 'string' == typeof id ? document.getElementById(id) : id;
    }

    var $d = (document.compatMode == 'CSS1Compat') ? document.documentElement : document.body;

    var isIE = navigator.userAgent.indexOf('MSIE') != -1;
    var isIE6 = navigator.userAgent.indexOf('MSIE 6.0') != -1;
    isIE6 && document.execCommand('BackgroundImageCache', false, true);

    var Extend = function(destination, source){
        for(var pro in source){
            destination[pro] = source[pro];
        }
        return destination;
    }

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

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

    function setOpacity(obj, o){
        if(!obj.currentStyle || !obj.currentStyle.hasLayout) obj.style.zoom = 1;
        if(!!isIE)obj.style.filter = 'alpha(opacity=' + Math.round(o) + ')';
        else obj.style.opacity = o / 100;
    }

    var Bind = function(object, fun){
        return function(){
            fun.apply(object, arguments);
        }
    }

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

    var OverLay = Class.create();
    OverLay.prototype = {
        initialize: function(options){
            this.SetOptions(options);
            Extend(this, this.options);
            this.Lay = document.body.insertBefore(document.createElement('div'), document.body.childNodes[0]);
            with(this.Lay.style){
                position = 'fixed';left = top = '0px';width = height = '100%';zIndex = parseInt(this.zIndex);
                backgroundColor = this.bgColor;display = 'none';
            }
            if(isIE6){
                this.Lay.style.position = 'absolute';
                this._resize = Bind(this, function(){
                    this.Lay.style.width = Math.max($d.clientWidth, $d.scrollWidth) + "px";
                    this.Lay.style.height = Math.max($d.clientHeight, $d.scrollHeight) + "px";
                });
                this.Lay.innerHTML += "<iframe style='position:absolute;left:0px;top:0px;widht:100%;height:100%;filter:alpha(opacity=0);z-index:-1'></iframe>";
            }
        },

        Show: function(){
            if(isIE6){this._resize();addEvent(window,'resize',Bind(this, this._resize))};
            setOpacity(this.Lay, this.opacity);
            this.Lay.style.display = "block";                    
        },

        SetOptions: function(options){
            this.options = {
                bgColor: "#000000",
                opacity: 80,
                zIndex: 1000
            };
            Extend(this.options, options || {});
        },

        Close: function(){            
            this.Lay.style.display = "none";
            isIE6 && removeEvent(window,'resize',Bind(this,this._resize));
        }
    }

    var LightBox = Class.create();
    LightBox.prototype = {
        initialize: function(boxId, options){
            this.Box = $(boxId);
            this.Lay = new OverLay();
            this.SetOptions(options);
            Extend(this, this.options);

            this.Box.style.display = "none";
            this.Box.style.zIndex = parseInt(this.Lay.zIndex) + 1;
            if(isIE6){
                this._top = this._left = 0; 
                this._fixed = Bind(this, function(){ this.isCenter ? this.SetCenter() : this.FixScroll(); });
            }
        },

        SetCenter: function(){
            this.Box.style.marginLeft = $d.scrollLeft - this.Box.offsetWidth / 2 + "px";
            this.Box.style.marginTop = $d.scrollTop  - this.Box.offsetHeight / 2 + "px";
        },

        FixScroll: function(){
            this.Box.style.top = $d.scrollTop - this._top + this.Box.offsetTop + "px";
            this.Box.style.left = $d.scrollLeft - this._left + this.Box.offsetLeft + "px";
            this._top = $d.scrollTop; this._left = $d.scrollLeft;
        },

        Show: function(){
            this.hasLayer && this.Lay.Show();
            this.Box.style.position = this.isScroll && !isIE6 ? 'fixed' : 'absolute';    
            this.Box.style.display = "block";
            if(this.isCenter){
                this.Box.style.left = "50%"; this.Box.style.top = "50%";
                if(this.isScroll){
                    this.Box.style.marginLeft = -this.Box.offsetWidth / 2 + "px";
                    this.Box.style.marginTop =  - this.Box.offsetHeight / 2 + "px";
                }else {
                    this.SetCenter();
                }
            }else{
                this.Box.style.left = "20%"; this.Box.style.top = "10%";
            }
            if(isIE6){
                this.isCenter ? this.SetCenter() : this.isScroll && this.FixScroll();
                this.isScroll && addEvent(window,'scroll',this._fixed);        
            }

        },

        SetOptions: function(options){
            this.options = {
                hasLayer: true,
                isCenter: true,
                isScroll: true
            };
            Extend(this.options, options || {});
        },

        Close: function(){
            this.Box.style.display = "none";
            this.Lay.Close();
        }
    }
 </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 id="openbtn"><img src="http://images.cnblogs.com/cnblogs_com/s_liangchao1s/201609/o_28t.jpg"/></div>
  <div id="box">
    <img src="http://images.cnblogs.com/cnblogs_com/s_liangchao1s/201609/o_28.jpg"/></br>
    <span id="clobtn">
        <img src='http://images.cnblogs.com/cnblogs_com/s_liangchao1s/201609/o_closelabel.gif'/>
    </span>
  </div>
 <select style="width:100px;">
    <option value="" selected="selected">Test IE6</option>
 </select>
 <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
 <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
 <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
 <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
  <script type="text/javascript">
    var box = new LightBox('box');
    $('openbtn').onclick = function(){
        box.Show();
    }
    $('clobtn').onclick = function(){
        box.Close();
    }
  </script>
 </body>
</html>
【源码下载】 

lightBox