1. 程式人生 > >Javascript應用--窗戶裡的圖片展示效果(圖片換成自己的即可執行)

Javascript應用--窗戶裡的圖片展示效果(圖片換成自己的即可執行)

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