Javascript应用--窗户里的图片展示效果(图片换成自己的即可运行)

来源:互联网 发布:辛亥革命 知乎 编辑:程序博客网 时间:2024/04/29 05:38
Code:
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">  
  2. <html>  
  3. <head>  
  4. <title>4</title>  
  5. <meta http-equiv="imagetoolbar" content="no">  
  6. <style type="text/css">  
  7.     body {   
  8.         background: #222;   
  9.         overflow: hidden;   
  10.         left: 0;   
  11.         top: 0;   
  12.         width: 100%;   
  13.         height: 100%;   
  14.         margin: 0;   
  15.         padding: 0;   
  16.     }   
  17.   
  18.     #screen img {   
  19.         position: absolute;   
  20.         visibility: hidden;   
  21.     }   
  22.   
  23.     #screen span {   
  24.         position: absolute;   
  25.         background: #fff;   
  26.         cursor: pointer;   
  27.         visibility: hidden;   
  28.         filter: alpha(opacity=80);   
  29.         -moz-opacity: 0.8;   
  30.         opacity: 0.8;   
  31.     }   
  32. </style>  
  33.   
  34. <script type="text/javascript"><!--   
  35. var Oz, Ov;   
  36. var obj = [];   
  37. var K = 0;   
  38. var img, scr, W, H;   
  39. var SP = 40; /* speed */   
  40. var dz = false;   
  41.   
  42. /* html positioning */   
  43. position = function(obj, x, y, w, h)   
  44. {   
  45.     with(obj.style){   
  46.         left = Math.round(x) + "px";   
  47.         top = Math.round(y) + "px";   
  48.         width = Math.round(w) + "px";   
  49.         height = Math.round(h) + "px";   
  50.     }   
  51. }   
  52.   
  53. /* create object instances */   
  54. function Cobj(parent, N, x, y, w, h)   
  55. {   
  56.     this.zoomed = (parent ? 0 : 1);   
  57.     obj[K] = this;   
  58.     this.K = K ++;   
  59.     this.parent = parent;   
  60.     this.children = [];   
  61.     this.x = x;   
  62.     this.y = y;   
  63.     this.w = w;   
  64.     this.h = h;   
  65.     this.vx = 0;   
  66.     this.vy = 0;   
  67.     this.vw = 0;   
  68.     this.vh = 0;   
  69.     this.xi = 0;   
  70.     this.yi = 0;   
  71.     this.wi = 0;   
  72.     this.hi = 0;   
  73.     this.x1 = 0;   
  74.     this.y1 = 0;   
  75.     this.w1 = 0;   
  76.     this.h1 = 0;   
  77.     this.x0 = 0;   
  78.     this.y0 = 0;   
  79.     this.w0 = 0;   
  80.     this.h0 = 0;   
  81.     this.imgsrc = img[N];   
  82.   
  83.     /* create HTML elements */   
  84.     this.img = document.createElement("img");   
  85.     thisthis.img.src = this.imgsrc.src;   
  86.     thisthis.img.obj = this;   
  87.     scr.appendChild(this.img);   
  88.     this.spa = document.createElement("span");   
  89.     this.spa.style.cursor = "pointer";   
  90.     thisthis.spa.obj = this;   
  91.     scr.appendChild(this.spa);   
  92.     if (parent) parent.children.push(this);   
  93.   
  94.     /* Flickr */   
  95.     this.blink = function()   
  96.     {   
  97.         with(this)   
  98.         {   
  99.             position(spa, x0, y0, w0, h0);   
  100.             spa.style.visibility = "visible";   
  101.             img.style.cursor = "pointer";   
  102.             setTimeout("obj[" + K + "].spa.style.visibility='hidden'", 128);   
  103.         }   
  104.     }   
  105.   
  106.     /* display image & children */   
  107.     this.display = function(zoomed)   
  108.     {   
  109.         with(this)   
  110.         {   
  111.             position(img, x0, y0, w0, h0);   
  112.             img.style.visibility = "visible";   
  113.             if (parent &&  ! zoomed)   
  114.             {   
  115.                 blink();   
  116.                 Ov = img;   
  117.             }   
  118.         }   
  119.     }   
  120.   
  121.     /* init zoom */   
  122.     this.init_zoom = function(d)   
  123.     {   
  124.         with(this)   
  125.         {   
  126.             w1 = imgsrc.width;   
  127.             h1 = imgsrc.height;   
  128.             x1 = (W - w1) / 2;   
  129.             y1 = (H - h1) / 2;   
  130.             x0 = (parent ? x + parent.x1 : (W - w1) / 2);   
  131.             y0 = (parent ? y + parent.y1 : (H - h1) / 2);   
  132.             w0 = (parent ? w : w1);   
  133.             h0 = (parent ? h : h1);   
  134.             xi = d > 0 ? x0 : x1;   
  135.             yi = d > 0 ? y0 : y1;   
  136.             wi = d > 0 ? w0 : w1;   
  137.             hi = d > 0 ? h0 : h1;   
  138.             vx = d * (x1 - x0) / SP;   
  139.             vy = d * (y1 - y0) / SP;   
  140.             vw = d * (w1 - w0) / SP;   
  141.             vh = d * (h1 - h0) / SP;   
  142.             parent.vx = d * ((x1 - (x * w1 / w0)) - parent.x1) / SP;   
  143.             parent.vy = d * ((y1 - (y * h1 / h0)) - parent.y1) / SP;   
  144.             parent.vw = vw * (parent ? (parent.w1 / w) : 0);   
  145.             parent.vh = vh * (parent ? (parent.h1 / h) : 0);   
  146.         }   
  147.     }   
  148.   
  149.     /* animate zoom in - out */   
  150.     this.zoom = function()   
  151.     {   
  152.         with(this)   
  153.         {   
  154.             xi += vx;   
  155.             yi += vy;   
  156.             wi += vw;   
  157.             hi += vh;   
  158.   
  159.             parent.xi += parent.vx;   
  160.             parent.yi += parent.vy;   
  161.             parent.wi += parent.vw;   
  162.             parent.hi += parent.vh;   
  163.   
  164.             position(img, xi, yi, wi, hi);   
  165.             position(parent.img, parent.xi, parent.yi, parent.wi, parent.hi);   
  166.   
  167.             if ((zoomed == 0 && wi > w0 - vw) || (zoomed == 1 && wi < w1 - vw))   
  168.             {   
  169.                 setTimeout("obj[" + K + "].zoom()", 16);   
  170.             }   
  171.             else   
  172.             {   
  173.                 dz = false;   
  174.                 for (var i in Oz.children)   
  175.                 {   
  176.                     Oz.children[i].init_zoom();   
  177.                     Oz.children[i].display(false, false);   
  178.                 }   
  179.             }   
  180.         }   
  181.     }   
  182.   
  183.     /* mouse events */   
  184.     this.img.onmouseover = function()   
  185.     {   
  186.         if (this != Ov) with(this.obj) if (parent &&  ! zoomed &&  ! dz) blink();   
  187.         Ov = this;   
  188.         return false;   
  189.     }   
  190.   
  191.     this.img.onclick = function()   
  192.     {   
  193.         with(this.obj)   
  194.         {   
  195.             if (parent && !dz && imgsrc.complete)   
  196.             {   
  197.                 if (zoomed == 1)   
  198.                 {   
  199.                     /* zoom out */   
  200.                     Oz = this.obj.parent;   
  201.                     zoomed = 0;   
  202.                     init_zoom( -1);   
  203.                     for (var i in children) children[i].img.style.visibility = "hidden";   
  204.                     parent.zoomed = 1;   
  205.                     dz = true;   
  206.                     zoom();   
  207.                 }   
  208.                 else if (zoomed == 0)   
  209.                 {   
  210.                     /* zoom in */   
  211.                     Oz = this.obj;   
  212.                     img.style.cursor = "crosshair";   
  213.                     zoomed = 1;   
  214.                     init_zoom(1);   
  215.                     for (var i in parent.children)   
  216.                     {   
  217.                         if (this.obj != parent.children[i])   
  218.                             parent.children[i].img.style.visibility = "hidden";   
  219.                     }   
  220.                     parent.zoomed =  -1;   
  221.                     dz = true;   
  222.                     zoom();   
  223.                 }   
  224.             }   
  225.         }   
  226.         return false;   
  227.     }   
  228.   
  229.     this.spa.onmousedown = function()   
  230.     {   
  231.         this.style.visibility="hidden";   
  232.         this.obj.img.onclick();   
  233.         return false;   
  234.     }   
  235. }   
  236.   
  237. /* initialization */   
  238. /* must start after the first image is loaded */   
  239. function starter()   
  240. {   
  241.     scr = document.getElementById("screen");   
  242.     img = document.getElementById("images").getElementsByTagName("img");   
  243.     W = parseInt(scr.style.width);   
  244.     H = parseInt(scr.style.height);   
  245.   
  246.     /* ==== tree-zoom ==== */   
  247.     //  new Cobj(parent, image, x, y, w, h)   
  248.     O = new Cobj(0, 0, 0, 0, 0, 0);   
  249.         O0 = new Cobj(O, 1, 127, 98, 181, 134);   
  250.             O1 = new Cobj(O0, 2, 158, 150, 85, 155);   
  251.                 O11 = new Cobj(O1, 4, 136, 98, 80, 196);   
  252.                     O111 = new Cobj(O11, 5, 20, 154, 70, 57);   
  253.                         O1111 = new Cobj(O111, 6, 161, 137, 154, 76);   
  254.                     O112 = new Cobj(O11, 11, 155, 154, 70, 57);   
  255.                         O1121 = new Cobj(O112, 12, 273, 116, 49, 72);   
  256.             O2 = new Cobj(O0, 3, 281, 150, 90, 154);   
  257.                 O21 = new Cobj(O2, 7, 35, 295, 133, 82);   
  258.                     O211 = new Cobj(O21, 15, 316, 183, 20, 36);   
  259.                 O22 = new Cobj(O2, 8, 179, 295, 127, 79);   
  260.                     O221 = new Cobj(O22, 13, 132, 84, 54, 102);   
  261.                         O2211 = new Cobj(O221, 14, 6, 234, 69, 50);   
  262.                             O22111 = new Cobj(O2211, 14, 267, 90, 135, 98);   
  263.                 O23 = new Cobj(O2, 9, 92, 148, 138, 76);   
  264.                     O231 = new Cobj(O23, 10, 249, 106, 83, 65);   
  265.                         O2311 = new Cobj(O231, 0, 120, 87, 57, 59);   
  266.   
  267.     /* display */   
  268.     O.init_zoom(1);   
  269.     O.display(true);   
  270.     for (var i in O.children)   
  271.     {   
  272.         O.children[i].init_zoom(1);   
  273.         O.children[i].display(true);   
  274.     }   
  275. }   
  276. //-->  
  277. </script>  
  278. </head>  
  279.   
  280. <body>  
  281.   
  282.     <div style="position:absolute;left:50%;top:50%">  
  283.         <div style="position:absolute;left:-220px;top:-170px;width:440px;height:340px;background:#000"></div>  
  284.         <div id="screen" style="position:absolute;left:-200px;top:-150px;width:400px;height:300px;overflow:hidden">  
  285.   
  286.         </div>  
  287.     </div>  
  288.   
  289.     <div id="images" style="position:absolute;left:-10000px;top:-10000px;">  
  290.         <img onload="setTimeout('starter();',500)" src="wi37.jpg">  
  291.         <img src="wi2.jpg">  
  292.         <img src="wi7.jpg">  
  293.         <img src="wi20.jpg">  
  294.         <img src="wi3.jpg">  
  295.         <img src="wi10.jpg">  
  296.         <img src="wi30.jpg">  
  297.         <img src="wi11.jpg">  
  298.         <img src="wi21.jpg">  
  299.         <img src="wi29.jpg">  
  300.         <img src="wi19.jpg">  
  301.         <img src="wi27.jpg">  
  302.         <img src="wi33.jpg">  
  303.         <img src="wi35.jpg">  
  304.         <img src="wi42.jpg">  
  305.         <img src="wi15.jpg">  
  306.     </div>  
  307.   
  308. </body>  
  309. </html>