3D图片相册效果

[复制链接]
查看1056 | 回复0 | 2010-8-9 11:22:29 | 显示全部楼层 |阅读模式
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4. <title></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: #111;
  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: #000;
  24. }
  25. #screen img {
  26. position: absolute;
  27. cursor: pointer;
  28. visibility: hidden;
  29. width: 0px;
  30. height: 0px;
  31. }
  32. #screen .tvover {
  33. border: solid #876;
  34. opacity: 1;
  35. filter: alpha(opacity=100);
  36. }
  37. #screen .tvout {
  38. border: solid #fff;
  39. opacity: 0.7;
  40. }
  41. #bankImages {
  42. display: none;
  43. }
  44. </style>
  45. <script type="text/javascript">
  46. var Library = {};
  47. Library.ease = function () {
  48. this.target = 0;
  49. this.position = 0;
  50. this.move = function (target, speed)
  51. {
  52. this.position += (target - this.position) * speed;
  53. }
  54. }
  55. var tv = {
  56. O : [],
  57. screen : {},
  58. grid : {
  59. size : 4,
  60. borderSize : 6,
  61. zoomed : false
  62. },
  63. angle : {
  64. x : new Library.ease(),
  65. y : new Library.ease()
  66. },
  67. camera : {
  68. x : new Library.ease(),
  69. y : new Library.ease(),
  70. zoom : new Library.ease(),
  71. focalLength : 750
  72. },
  73. init : function ()
  74. {
  75. this.screen.obj = document.getElementById('screen');
  76. var img = document.getElementById('bankImages').getElementsByTagName('img');
  77. this.screen.obj.onselectstart = function () { return false; }
  78. this.screen.obj.ondrag = function () { return false; }
  79. var ni = 0;
  80. var n = (tv.grid.size / 2) - .5;
  81. for (var y = -n; y <= n; y++)
  82. {
  83. for (var x = -n; x <= n; x++)
  84. {
  85. var o = document.createElement('img');
  86. var i = img[(ni++) % img.length];
  87. o.className = 'tvout';
  88. o.src = i.src;
  89. tv.screen.obj.appendChild(o);
  90. o.point3D = {
  91. x : x,
  92. y : y,
  93. z : new Library.ease()
  94. };
  95. o.point2D = {};
  96. o.ratioImage = 1;
  97. tv.O.push(o);
  98. o.onmouseover = function ()
  99. {
  100. if (!tv.grid.zoomed)
  101. {
  102. if (tv.o)
  103. {
  104. tv.o.point3D.z.target = 0;
  105. tv.o.className = 'tvout';
  106. }
  107. this.className = 'tvover';
  108. this.point3D.z.target = -.5;
  109. tv.o = this;
  110. }
  111. }
  112. o.onclick = function ()
  113. {
  114. if (!tv.grid.zoomed)
  115. {
  116. tv.camera.x.target = this.point3D.x;
  117. tv.camera.y.target = this.point3D.y;
  118. tv.camera.zoom.target = tv.screen.w * 1.25;
  119. tv.grid.zoomed = this;
  120. } else {
  121. if (this == tv.grid.zoomed){
  122. tv.camera.x.target = 0;
  123. tv.camera.y.target = 0;
  124. tv.camera.zoom.target = tv.screen.w / (tv.grid.size + .1);
  125. tv.grid.zoomed = false;
  126. }
  127. }
  128. }
  129. o.calc = function ()
  130. {
  131. this.point3D.z.move(this.point3D.z.target, .5);
  132. var x = (this.point3D.x - tv.camera.x.position) * tv.camera.zoom.position;
  133. var y = (this.point3D.y - tv.camera.y.position) * tv.camera.zoom.position;
  134. var z = this.point3D.z.position * tv.camera.zoom.position;
  135. var xy = tv.angle.cx * y - tv.angle.sx * z;
  136. var xz = tv.angle.sx * y + tv.angle.cx * z;
  137. var yz = tv.angle.cy * xz - tv.angle.sy * x;
  138. var yx = tv.angle.sy * xz + tv.angle.cy * x;
  139. this.point2D.scale = tv.camera.focalLength / (tv.camera.focalLength + yz);
  140. this.point2D.x = yx * this.point2D.scale;
  141. this.point2D.y = xy * this.point2D.scale;
  142. this.point2D.w = Math.round(
  143. Math.max(
  144. 0,
  145. this.point2D.scale * tv.camera.zoom.position * .8
  146. )
  147. );
  148. if (this.ratioImage > 1)
  149. this.point2D.h = Math.round(this.point2D.w / this.ratioImage);
  150. else
  151. {
  152. this.point2D.h = this.point2D.w;
  153. this.point2D.w = Math.round(this.point2D.h * this.ratioImage);
  154. }
  155. }
  156. o.draw = function ()
  157. {
  158. if (this.complete)
  159. {
  160. if (!this.loaded)
  161. {
  162. if (!this.img)
  163. {
  164. this.img = new Image();
  165. this.img.src = this.src;
  166. }
  167. if (this.img.complete)
  168. {
  169. this.style.visibility = 'visible';
  170. this.ratioImage = this.img.width / this.img.height;
  171. this.loaded = true;
  172. this.img = false;
  173. }
  174. }
  175. this.style.left = Math.round(
  176. this.point2D.x * this.point2D.scale +
  177. tv.screen.w - this.point2D.w * .5
  178. ) + 'px';
  179. this.style.top = Math.round(
  180. this.point2D.y * this.point2D.scale +
  181. tv.screen.h - this.point2D.h * .5
  182. ) + 'px';
  183. this.style.width = this.point2D.w + 'px';
  184. this.style.height = this.point2D.h + 'px';
  185. this.style.borderWidth = Math.round(
  186. Math.max(
  187. this.point2D.w,
  188. this.point2D.h
  189. ) * tv.grid.borderSize * .01
  190. ) + 'px';
  191. this.style.zIndex = Math.floor(this.point2D.scale * 100);
  192. }
  193. }
  194. }
  195. }
  196. tv.resize();
  197. mouse.y = tv.screen.y + tv.screen.h;
  198. mouse.x = tv.screen.x + tv.screen.w;
  199. tv.run();
  200. },
  201. resize : function ()
  202. {
  203. var o = tv.screen.obj;
  204. tv.screen.w = o.offsetWidth / 2;
  205. tv.screen.h = o.offsetHeight / 2;
  206. tv.camera.zoom.target = tv.screen.w / (tv.grid.size + .1);
  207. for (tv.screen.x = 0, tv.screen.y = 0; o != null; o = o.offsetParent)
  208. {
  209. tv.screen.x += o.offsetLeft;
  210. tv.screen.y += o.offsetTop;
  211. }
  212. },
  213. run : function ()
  214. {
  215. tv.angle.x.move(-(mouse.y - tv.screen.h - tv.screen.y) * .0025, .1);
  216. tv.angle.y.move( (mouse.x - tv.screen.w - tv.screen.x) * .0025, .1);
  217. tv.camera.x.move(tv.camera.x.target, tv.grid.zoomed ? .25 : .025);
  218. tv.camera.y.move(tv.camera.y.target, tv.grid.zoomed ? .25 : .025);
  219. tv.camera.zoom.move(tv.camera.zoom.target, .05);
  220. tv.angle.cx = Math.cos(tv.angle.x.position);
  221. tv.angle.sx = Math.sin(tv.angle.x.position);
  222. tv.angle.cy = Math.cos(tv.angle.y.position);
  223. tv.angle.sy = Math.sin(tv.angle.y.position);
  224. for (var i = 0, o; o = tv.O[i]; i++)
  225. {
  226. o.calc();
  227. o.draw();
  228. }
  229. setTimeout(tv.run, 32);
  230. }
  231. }
  232. var mouse = {
  233. x : 0,
  234. y : 0
  235. }
  236. document.onmousemove = function(e)
  237. {
  238. if (window.event) e = window.event;
  239. mouse.x = e.clientX;
  240. mouse.y = e.clientY;
  241. return false;
  242. }
  243. </script>
  244. </head>
  245. <body>
  246. <div id="screen"></div>
  247. <div id="bankImages">
  248. <img alt="" src="http://www.cssrain.cn/skins/tim/isll_t.gif">
  249. <img alt="" src="http://www.cssrain.cn/skins/tim/isll_t.gif">
  250. <img alt="" src="http://www.cssrain.cn/skins/tim/isll_t.gif">
  251. <img alt="" src="http://www.cssrain.cn/skins/tim/logo-jq.gif">
  252. <img alt="" src="http://www.cssrain.cn/skins/tim/logo-jq.gif">
  253. <img alt="" src="http://www.cssrain.cn/skins/tim/logo-jq.gif">
  254. <img alt="" src="http://www.cssrain.cn/demo/JQuery+API/logo-json.gif">
  255. <img alt="" src="http://www.cssrain.cn/demo/JQuery+API/logo-json.gif">
  256. <img alt="" src="http://www.cssrain.cn/skins/tim/isll_t.gif">
  257. <img alt="" src="http://www.cssrain.cn/skins/tim/isll_t.gif">
  258. <img alt="" src="http://www.cssrain.cn/skins/tim/isll_t.gif">
  259. <img alt="" src="http://www.cssrain.cn/skins/tim/isll_t.gif">
  260. <img alt="" src="http://www.cssrain.cn/demo/JQuery+API/logo-json.gif">
  261. <img alt="" src="http://www.cssrain.cn/demo/JQuery+API/logo-json.gif">
  262. <img alt="" src="http://www.cssrain.cn/demo/JQuery+API/logo-json.gif">
  263. <img alt="" src="http://www.cssrain.cn/demo/JQuery+API/logo-json.gif">
  264. </div>
  265. <script type="text/javascript">
  266. onresize = tv.resize;
  267. tv.init();
  268. </script>
  269. </body>
  270. </html>
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则