【薦】超絢的JavaScript立體圖片展示代碼

 

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
  2. <html> 
  3. <head> 
  4. <title>【薦】超絢的JavaScript立體圖片展示代碼 - www.webdm.cn</title> 
  5. <style type="text/css"> 
  6.     html {  
  7.         overflow: hidden;  
  8.     }  
  9.     body {  
  10.         position: absolute;  
  11.         margin: 0px;  
  12.         padding: 0px;  
  13.         background: #fff;  
  14.         width: 100%;  
  15.         height: 100%;  
  16.     }  
  17.     #screen {  
  18.         position: absolute;  
  19.         left: 10%;  
  20.         top: 10%;  
  21.         width: 80%;  
  22.         height: 80%;  
  23.         background: #fff;  
  24.     }  
  25.     #screen img {  
  26.         position: absolute;  
  27.         cursor: pointer;  
  28.         width: 0px;  
  29.         height: 0px;  
  30.         -ms-interpolation-mode:nearest-neighbor;  
  31.     }  
  32.     #bankImages {  
  33.         visibility: hidden;  
  34.     }  
  35.     #FPS {  
  36.         position: absolute;  
  37.         right: 5px;  
  38.         bottom: 5px;  
  39.         font-size: 10px;  
  40.         color: #666;  
  41.         font-family: verdana;  
  42.     }  
  43.  
  44. </style> 
  45.  
  46. <script type="text/javascript"> 
  47. var Library = {};  
  48. Library.ease = function () {  
  49.     this.target = 0;  
  50.     this.position = 0;  
  51.     this.move = function (target, speed) {  
  52.         this.position += (target - this.position) * speed;  
  53.     }  
  54. }  
  55.  
  56. var tv = {  
  57.     O : [],  
  58.     fps : 0,  
  59.     screen : {},  
  60.     angle : {  
  61.         x : new Library.ease(),  
  62.         y : new Library.ease()  
  63.     },  
  64.     camera : {  
  65.         x    : new Library.ease(),  
  66.         y    : new Library.ease()  
  67.     },  
  68.     create3DHTML : function (i, x, y, z, sw, sh) {  
  69.         var o = document.createElement('img');  
  70.         o.src = i.src;  
  71.         tv.screen.obj.appendChild(o);  
  72.         o.point3D = {  
  73.             x  : x,  
  74.             y  : y,  
  75.             z  : new Library.ease(),  
  76.             sw : sw,  
  77.             sh : sh,  
  78.             w  : i.width,  
  79.             h  : i.height  
  80.         };  
  81.         o.point3D.z.target = z;  
  82.         o.point2D = {};  
  83.         tv.O.push(o);  
  84.         o.onmouseover = function () {  
  85.             if (this != tv.o) {  
  86.                 this.point3D.z.target = tv.mouseZ;  
  87.                 tv.camera.x.target = this.point3D.x;  
  88.                 tv.camera.y.target = this.point3D.y;  
  89.                 if (tv.o) tv.o.point3D.z.target = 0;  
  90.                 tv.o = this;  
  91.             }  
  92.             return false;  
  93.         }  
  94.         o.onmousedown = function () {  
  95.             if (this == tv.o) {  
  96.                 if (this.point3D.z.target == tv.mouseZ) this.point3D.z.target = 0;  
  97.                 else {  
  98.                     tv.o = false;  
  99.                     this.  
  100.                 }  
  101.             }  
  102.         }  
  103.         o.animate = function () {  
  104.             var x = this.point3D.x - tv.camera.x.position;  
  105.             var y = this.point3D.y - tv.camera.y.position;  
  106.             this.point3D.z.move(this.point3D.z.target, this.point3D.z.target ? .15 : .08);  
  107.             var xy = tv.angle.cx * y  - tv.angle.sx * this.point3D.z.position;  
  108.             var xz = tv.angle.sx * y  + tv.angle.cx * this.point3D.z.position;  
  109.             var yz = tv.angle.cy * xz - tv.angle.sy * x;  
  110.             var yx = tv.angle.sy * xz + tv.angle.cy * x;  
  111.             var scale = tv.camera.focalLength / (tv.camera.focalLength + yz);  
  112.             x = yx * scale;  
  113.             y = xy * scale;  
  114.             var w = Math.round(Math.max(0, this.point3D.w * scale * this.point3D.sw));  
  115.             var h = Math.round(Math.max(0, this.point3D.h * scale * this.point3D.sh));  
  116.             var o    = this.style;  
  117.             o.left   = Math.round(x + tv.screen.w - w * .5) + 'px';  
  118.             o.top    = Math.round(y + tv.screen.h - h * .5) + 'px';  
  119.             o.width  = w + 'px';  
  120.             o.height = h + 'px';  
  121.             o.zIndex = 10000 + Math.round(scale * 1000);  
  122.         }  
  123.     },  
  124.  
  125.     /* ==== http://www.webdm.cn ==== */  
  126.     init : function (structure, FL, mouseZ, rx, ry) {  
  127.         this.screen.obj = document.getElementById('screen');  
  128.         this.screen.obj.onselectstart = function () { return false; }  
  129.         this.screen.obj.ondrag        = function () { return false; }  
  130.         this.mouseZ = mouseZ;  
  131.         this.camera.focalLength = FL;  
  132.         this.angle.rx = rx;  
  133.         this.angle.ry = ry;  
  134.         var i = 0, o;  
  135.         while( o = structure[i++] )  
  136.             this.create3DHTML(o.img, o.x, o.y, o.z, o.sw, o.sh);  
  137.         this.resize();  
  138.         mouse.y = this.screen.y + this.screen.h;  
  139.         mouse.x = this.screen.x + this.screen.w;  
  140.         setInterval(tv.run, 16);  
  141.         setInterval(tv.dFPS, 1000);  
  142.     },  
  143.     resize : function () {  
  144.         var o = tv.screen.obj;  
  145.         if (o) {  
  146.             tv.screen.w = o.offsetWidth / 2;  
  147.             tv.screen.h = o.offsetHeight / 2;  
  148.             for (tv.screen.x = 0tv.screen.y = 0; o != null; oo = o.offsetParent) {  
  149.                 tv.screen.x += o.offsetLeft;  
  150.                 tv.screen.y += o.offsetTop;  
  151.             }  
  152.         }  
  153.     },  
  154.     run : function () {  
  155.         tv.fps++;  
  156.         tv.angle.x.move(-(mouse.y - tv.screen.h - tv.screen.y) * tv.angle.rx, .1);  
  157.         tv.angle.y.move( (mouse.x - tv.screen.w - tv.screen.x) * tv.angle.ry, .1);  
  158.         tv.camera.x.move(tv.camera.x.target, .025);  
  159.         tv.camera.y.move(tv.camera.y.target, .025);  
  160.         tv.angle.cx = Math.cos(tv.angle.x.position);  
  161.         tv.angle.sx = Math.sin(tv.angle.x.position);  
  162.         tv.angle.cy = Math.cos(tv.angle.y.position);  
  163.         tv.angle.sy = Math.sin(tv.angle.y.position);  
  164.         var i = 0, o;  
  165.         while( o = tv.O[i++] ) o.animate();  
  166.     },  
  167.     dFPS : function () {  
  168.         document.getElementById('FPS').innerHTML = tv.fps + ' FPS';  
  169.         tv.fps = 0;  
  170.     }  
  171. }  
  172. var mouse = {  
  173.     x : 0,  
  174.     y : 0  
  175. }  
  176. document.onmousemove = function(e) {  
  177.     if (window.event) e = window.event;  
  178.     mouse.x = e.clientX;  
  179.     mouse.y = e.clientY;  
  180.     return false;  
  181. }  
  182. onload = function() {  
  183.     onresize = tv.resize;  
  184.     var img = document.getElementById('bankImages').getElementsByTagName('img');  
  185.     var structure = [];  
  186.     for (var i = -300; i <= 300; i += 120)  
  187.         for (var j = -300; j <= 300; j += 120)  
  188.             structure.push({ img:img[0], x:i, y:j, z:0, sw:.5, sh:.5 });  
  189.     tv.init(structure, 350, -200, .005, .0025);  
  190. }  
  191.  
  192. </script> 
  193. </head> 
  194. <body> 
  195. <div id="screen"></div> 
  196. <div id="bankImages"> 
  197.     <img alt="" src="http://www.webdm.cn/images/wall1_s.jpg"> 
  198. </div> 
  199. <div id="FPS"></div> 
  200. </body> 
  201. </html> 
  202.  
  203. <br> 
  204. <a href="http://www.webdm.cn">網頁代碼站</a> - 最專業的代碼下載網站 - 致力爲中國站長提供有質量的代碼! 

圖片3D展示,鼠標晃動的時候圖片跟着就動了,點擊的時候會稍微放大一些,不同的角度會出現不一樣的效果,JavaScript與CSS結合共同形成的效果。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章