Chinaunix首页 | 论坛 | 博客
  • 博客访问: 29898781
  • 博文数量: 708
  • 博客积分: 12163
  • 博客等级: 上将
  • 技术积分: 8240
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-04 20:59
文章分类

全部博文(708)

分类: Java

2011-11-14 11:45:41

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
  2. <html xmlns="">
  3.  <head>
  4.   <title> LightBox </title>
  5.   <style type="text/css">
  6.   #box {
  7.     text-align:right;
  8.     background-color: #fff;
  9.     border: 20px solid #000000;
  10.   }
  11.   </style>
  12.  </head>
  13.  <body>
  14.  <script type="text/javascript">
  15.     var $ = function(id){
  16.         return 'string' == typeof id ? document.getElementById(id) : id;
  17.     }

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

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

  22.     var Extend = function(destination, source){
  23.         for(var pro in source){
  24.             destination[pro] = source[pro];
  25.         }
  26.         return destination;
  27.     }

  28.     function addEvent(oTarget, sType, fnHandler){
  29.         if(window.attachEvent){
  30.             oTarget.attachEvent("on"+sType, fnHandler);
  31.         }else if(window.addEventListener){
  32.             oTarget.addEventListener(sType, fnHandler, false);
  33.         }else{
  34.             oTarget["on"+sType] = fnHandler;
  35.         }
  36.     }

  37.     function removeEvent(oTarget, sType, fnHandler){
  38.         if(window.detachEvent){
  39.             oTarget.detachEvent("on"+sType, fnHandler);
  40.         }else if(window.removeEventListener){
  41.             oTarget.removeEventListener(sType, fnHandler, false);
  42.         }else{
  43.             oTarget["on"+sType] = null;
  44.         }
  45.     }

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

  51.     var Bind = function(object, fun){
  52.         return function(){
  53.             fun.apply(object, arguments);
  54.         }
  55.     }

  56.     var Class = {
  57.         create: function(){
  58.             return function(){
  59.                 this.initialize.apply(this, arguments);
  60.             }
  61.         }
  62.     }

  63.     var OverLay = Class.create();
  64.     OverLay.prototype = {
  65.         initialize: function(options){
  66.             this.SetOptions(options);
  67.             Extend(this, this.options);
  68.             this.Lay = document.body.insertBefore(document.createElement('div'), document.body.childNodes[0]);
  69.             with(this.Lay.style){
  70.                 position = 'fixed';left = top = '0px';width = height = '100%';zIndex = parseInt(this.zIndex);
  71.                 backgroundColor = this.bgColor;display = 'none';
  72.             }
  73.             if(isIE6){
  74.                 this.Lay.style.position = 'absolute';
  75.                 this._resize = Bind(this, function(){
  76.                     this.Lay.style.width = Math.max($d.clientWidth, $d.scrollWidth) + "px";
  77.                     this.Lay.style.height = Math.max($d.clientHeight, $d.scrollHeight) + "px";
  78.                 });
  79.                 this.Lay.innerHTML += "";
  80.             }
  81.         },

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

  87.         SetOptions: function(options){
  88.             this.options = {
  89.                 bgColor: "#000000",
  90.                 opacity: 80,
  91.                 zIndex: 1000
  92.             };
  93.             Extend(this.options, options || {});
  94.         },

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

  100.     var LightBox = Class.create();
  101.     LightBox.prototype = {
  102.         initialize: function(boxId, options){
  103.             this.Box = $(boxId);
  104.             this.Lay = new OverLay();
  105.             this.SetOptions(options);
  106.             Extend(this, this.options);

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

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

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

  123.         Show: function(){
  124.             this.hasLayer && this.Lay.Show();
  125.             this.Box.style.position = this.isScroll && !isIE6 ? 'fixed' : 'absolute';
  126.             this.Box.style.display = "block";
  127.             if(this.isCenter){
  128.                 this.Box.style.left = "50%"; this.Box.style.top = "50%";
  129.                 if(this.isScroll){
  130.                     this.Box.style.marginLeft = -this.Box.offsetWidth / 2 + "px";
  131.                     this.Box.style.marginTop = - this.Box.offsetHeight / 2 + "px";
  132.                 }else {
  133.                     this.SetCenter();
  134.                 }
  135.             }else{
  136.                 this.Box.style.left = "20%"; this.Box.style.top = "10%";
  137.             }
  138.             if(isIE6){
  139.                 this.isCenter ? this.SetCenter() : this.isScroll && this.FixScroll();
  140.                 this.isScroll && addEvent(window,'scroll',this._fixed);
  141.             }

  142.         },

  143.         SetOptions: function(options){
  144.             this.options = {
  145.                 hasLayer: true,
  146.                 isCenter: true,
  147.                 isScroll: true
  148.             };
  149.             Extend(this.options, options || {});
  150.         },

  151.         Close: function(){
  152.             this.Box.style.display = "none";
  153.             this.Lay.Close();
  154.         }
  155.     }
  156.  </script>
  157.   <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
  158.  <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
  159.   <div id="openbtn"><img src="/images/forpopup.jpg"/></div>
  160.   <div id="box">
  161.     <img src="/images/forpopup.jpg"/></br>
  162.     <span id="clobtn">
  163.         <img src='/images/close.gif'/>
  164.     </span>
  165.   </div>
  166.  <select style="width:100px;">
  167.     <option value="" selected="selected">Test IE6</option>
  168.  </select>
  169.  <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
  170.  <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
  171.  <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
  172.  <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
  173.   <script type="text/javascript">
  174.     var box = new LightBox('box');
  175.     $('openbtn').onclick = function(){
  176.         box.Show();
  177.     }
  178.     $('clobtn').onclick = function(){
  179.         box.Close();
  180.     }
  181.   </script>
  182.  </body>
  183. </html>
阅读(1178) | 评论(0) | 转发(0) |
0

上一篇:千位格式化

下一篇:DragDrop

给主人留下些什么吧!~~