当前位置:Gxlcms > css > 利用CSS3怎么做出不规则图片的切换特效制作

利用CSS3怎么做出不规则图片的切换特效制作

时间:2021-07-01 10:21:17 帮助过:13人阅读

给大家带来一份源码,用CSS3怎么做出不规则图片的切换特效制作,有需要的朋友可以拿去用一下。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>TweenMax不规则图片切换特效</title>
  6. <link rel="stylesheet" type="text/css" href="css/style.css" />
  7. </head>
  8. <body>
  9. <div id="container"> </div>
  10. <script src='js/delaunay.js'></script>
  11. <script src='js/TweenMax.js'></script>
  12. <script>
  13. const TWO_PI = Math.PI * 2;
  14. var images = [],
  15. imageIndex = 0;
  16. var image,
  17. imageWidth = 768,
  18. imageHeight = 485;
  19. var vertices = [],
  20. indices = [],
  21. prevfrag = [],
  22. fragments = [];
  23. var margin = 50;
  24. var container = document.getElementById('container');
  25. var clickPosition = [imageWidth * 0.5, imageHeight * 0.5];
  26. window.onload = function() {
  27. TweenMax.set(container, {perspective:500});
  28. // images from http://www.hdwallpapers.in
  29. var urls = [
  30. 'images/1.jpg',
  31. 'images/2.jpg',
  32. 'images/3.jpg',
  33. 'images/4.jpg'
  34. ],
  35. image,
  36. loaded = 0;
  37. // very quick and dirty hack to load and display the first image asap
  38. images[0] = image = new Image();
  39. image.onload = function() {
  40. if (++loaded === 1) {
  41. for (var i = 1; i < 4; i++) {
  42. images[i] = image = new Image();
  43. image.src = urls[i];
  44. }
  45. placeImage();
  46. }
  47. };
  48. image.src = urls[0];
  49. };
  50. function placeImage(transitionIn) {
  51. image = images[imageIndex];
  52. if (++imageIndex === images.length) imageIndex = 0;
  53. var num = Math.random();
  54. if(num < .25) {
  55. image.direction = "left";
  56. } else if(num < .5) {
  57. image.direction = "top";
  58. } else if(num < .75) {
  59. image.direction = "bottom";
  60. } else {
  61. image.direction = "right";
  62. }
  63. container.appendChild(image);
  64. image.style.opacity = 0;
  65. if (transitionIn !== false) {
  66. triangulateIn();
  67. }
  68. }
  69. function triangulateIn(event) {
  70. var box = image.getBoundingClientRect(),
  71. top = box.top,
  72. left = box.left;
  73. if(image.direction == "left") {
  74. clickPosition[0] = 0;
  75. clickPosition[1] = imageHeight / 2;
  76. } else if(image.direction == "top") {
  77. clickPosition[0] = imageWidth / 2;
  78. clickPosition[1] = 0;
  79. } else if(image.direction == "bottom") {
  80. clickPosition[0] = imageWidth / 2;
  81. clickPosition[1] = imageHeight;
  82. } else if(image.direction == "right") {
  83. clickPosition[0] = imageWidth;
  84. clickPosition[1] = imageHeight / 2;
  85. }
  86. triangulate();
  87. build();
  88. }
  89. function triangulate() {
  90. for(var i = 0; i < 40; i++) {
  91. x = -margin + Math.random() * (imageWidth + margin * 2);
  92. y = -margin + Math.random() * (imageHeight + margin * 2);
  93. vertices.push([x, y]);
  94. }
  95. vertices.push([0,0]);
  96. vertices.push([imageWidth,0]);
  97. vertices.push([imageWidth, imageHeight]);
  98. vertices.push([0, imageHeight]);
  99. vertices.forEach(function(v) {
  100. v[0] = clamp(v[0], 0, imageWidth);
  101. v[1] = clamp(v[1], 0, imageHeight);
  102. });
  103. indices = Delaunay.triangulate(vertices);
  104. }
  105. function build() {
  106. var p0, p1, p2,
  107. fragment;
  108. var tl0 = new TimelineMax({onComplete:buildCompleteHandler});
  109. for (var i = 0; i < indices.length; i += 3) {
  110. p0 = vertices[indices[i + 0]];
  111. p1 = vertices[indices[i + 1]];
  112. p2 = vertices[indices[i + 2]];
  113. fragment = new Fragment(p0, p1, p2);
  114. var dx = fragment.centroid[0] - clickPosition[0],
  115. dy = fragment.centroid[1] - clickPosition[1],
  116. d = Math.sqrt(dx * dx + dy * dy),
  117. rx = 30 * sign(dy),
  118. ry = 90 * -sign(dx),
  119. delay = d * 0.003 * randomRange(0.9, 1.1);
  120. fragment.canvas.style.zIndex = Math.floor(d).toString();
  121. var tl1 = new TimelineMax();
  122. if(image.direction == "left") {
  123. rx = Math.abs(rx);
  124. ry = 0;
  125. } else if(image.direction == "top") {
  126. rx = 0;
  127. ry = Math.abs(ry);
  128. } else if(image.direction == "bottom") {
  129. rx = 0;
  130. ry = - Math.abs(ry);
  131. } else if(image.direction == "right") {
  132. rx = - Math.abs(rx);
  133. ry = 0;
  134. }
  135. tl1.from(fragment.canvas, 1, {
  136. z:-50,
  137. rotationX:rx,
  138. rotationY:ry,
  139. scaleX:0,
  140. scaleY:0,
  141. ease:Cubic.easeIn
  142. });
  143. tl1.from(fragment.canvas, 0.4,{alpha:0}, 0.6);
  144. tl0.insert(tl1, delay);
  145. fragments.push(fragment);
  146. container.appendChild(fragment.canvas);
  147. }
  148. }
  149. function buildCompleteHandler() {
  150. // add pooling?
  151. image.style.opacity = 1;
  152. image.addEventListener('transitionend', function catchTrans() {
  153. fragments.forEach(function(f) {
  154. container.removeChild(f.canvas);
  155. });
  156. fragments.length = 0;
  157. vertices.length = 0;
  158. indices.length = 0;
  159. placeImage();
  160. this.removeEventListener('transitionend',catchTrans,false);
  161. }, false);
  162. }
  163. //////////////
  164. // MATH UTILS
  165. //////////////
  166. function randomRange(min, max) {
  167. return min + (max - min) * Math.random();
  168. }
  169. function clamp(x, min, max) {
  170. return x < min ? min : (x > max ? max : x);
  171. }
  172. function sign(x) {
  173. return x < 0 ? -1 : 1;
  174. }
  175. //////////////
  176. // FRAGMENT
  177. //////////////
  178. Fragment = function(v0, v1, v2) {
  179. this.v0 = v0;
  180. this.v1 = v1;
  181. this.v2 = v2;
  182. this.computeBoundingBox();
  183. this.computeCentroid();
  184. this.createCanvas();
  185. this.clip();
  186. };
  187. Fragment.prototype = {
  188. computeBoundingBox:function() {
  189. var xMin = Math.min(this.v0[0], this.v1[0], this.v2[0]),
  190. xMax = Math.max(this.v0[0], this.v1[0], this.v2[0]),
  191. yMin = Math.min(this.v0[1], this.v1[1], this.v2[1]),
  192. yMax = Math.max(this.v0[1], this.v1[1], this.v2[1]);
  193. this.box = {
  194. x:Math.round(xMin),
  195. y:Math.round(yMin),
  196. w:Math.round(xMax - xMin),
  197. h:Math.round(yMax - yMin)
  198. };
  199. },
  200. computeCentroid:function() {
  201. var x = (this.v0[0] + this.v1[0] + this.v2[0]) / 3,
  202. y = (this.v0[1] + this.v1[1] + this.v2[1]) / 3;
  203. this.centroid = [x, y];
  204. },
  205. createCanvas:function() {
  206. this.canvas = document.createElement('canvas');
  207. this.canvas.width = this.box.w;
  208. this.canvas.height = this.box.h;
  209. this.canvas.style.width = this.box.w + 'px';
  210. this.canvas.style.height = this.box.h + 'px';
  211. this.canvas.style.left = this.box.x + 'px';
  212. this.canvas.style.top = this.box.y + 'px';
  213. this.ctx = this.canvas.getContext('2d');
  214. },
  215. clip:function() {
  216. this.ctx.save();
  217. this.ctx.translate(-this.box.x, -this.box.y);
  218. this.ctx.beginPath();
  219. this.ctx.moveTo(this.v0[0], this.v0[1]);
  220. this.ctx.lineTo(this.v1[0], this.v1[1]);
  221. this.ctx.lineTo(this.v2[0], this.v2[1]);
  222. this.ctx.closePath();
  223. this.ctx.clip();
  224. this.ctx.drawImage(image, 0, 0);
  225. this.ctx.restore();
  226. }
  227. };//@ sourceURL=pen.js
  228. </script>
  229. <div style="text-align:center;margin:10px 0; font:normal 14px/24px 'MicroSoft YaHei';">
  230. </div>
  231. </body>
  232. </html>

不规则图片的切换特效制作的代码就是这些了,更多精彩请关注Gxl网其它相关文章!

相关阅读:

怎么用CSS3媒体查询

在HTML里web标准是什么

CSS3怎么做出响应式布局

以上就是利用CSS3怎么做出不规则图片的切换特效制作的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行