shutter.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /**
  2. * Coder: EzrealY
  3. * Time: 2017.07.31
  4. * Mail: 1005526074@qq.com
  5. * 效果原作者: https://oss.so/article/71
  6. */
  7. ;
  8. (function($, window, document, undefined) {
  9. var Shutter = function(elem, options) {
  10. this.defaults = {
  11. // shutterW: 1200,
  12. // shutterH: 500,
  13. isAutoPlay: false,
  14. playInterval: 3000,
  15. curDisplay: 0,
  16. fullPage: false
  17. };
  18. this.opts = $.extend({}, this.defaults, options);
  19. this.inital(elem);
  20. };
  21. Shutter.prototype = {
  22. play: function() {
  23. var self = this;
  24. if (this.opts.isAutoPlay) {
  25. clearInterval(this.playTime);
  26. this.playTime = setInterval(function() {
  27. self.$nextBtn.click();
  28. }, this.opts.playInterval);
  29. }
  30. },
  31. moveSwitch: function(randomNum, command, index) {
  32. switch (randomNum) {
  33. // case 0:
  34. // this.gridWhole(index, 0);
  35. // break;
  36. // case 1:
  37. // this.gridWhole(index, 1);
  38. // break;
  39. // case 2:
  40. // this.gridWhole(index, 2);
  41. // break;
  42. // case 3:
  43. // this.gridWhole(index, 3);
  44. // break;
  45. // case 4:
  46. // this.gridTop(index, 0);
  47. // break;
  48. case 5:
  49. this.gridTop(index, 1);
  50. break;
  51. case 6:
  52. this.gridTop(index, 2);
  53. break;
  54. case 7:
  55. this.gridLeft(index, 0);
  56. break;
  57. case 8:
  58. this.gridLeft(index, 1);
  59. break;
  60. case 9:
  61. this.gridLeft(index, 2);
  62. break;
  63. case 10:
  64. this.gridOpacity(index);
  65. break;
  66. case 11:
  67. this.gridAccordion(index);
  68. break;
  69. case 12:
  70. this.gridLittle(index);
  71. break;
  72. case 13:
  73. this.gridSwitch(index);
  74. break;
  75. default:
  76. this.gridTop(index, 0);
  77. break;
  78. }
  79. },
  80. toggleMove: function(command, index) {
  81. if (!command) {
  82. if (this.curDisplay === index) {
  83. return;
  84. } else if (this.curDisplay === 0 && index === this.shutterItem_len - 1 ||
  85. index < this.curDisplay) {
  86. command = 'prev';
  87. } else {
  88. command = 'next';
  89. }
  90. }
  91. if (!index) {
  92. if (command === 'prev') {
  93. index = this.curDisplay - 1;
  94. if (this.curDisplay === 0) {
  95. index = this.shutterItem_len - 1;
  96. }
  97. } else {
  98. index = this.curDisplay + 1;
  99. if (this.curDisplay === this.shutterItem_len - 1) {
  100. index = 0;
  101. }
  102. }
  103. }
  104. this.$shutterDesc.animate({ bottom: -36 });
  105. var random = function(min, max) {
  106. return Math.floor(Math.random() * (max + 1) - min);
  107. };
  108. this.moveSwitch(random(0, 13), command, index);
  109. this.shutterTitle = this.$shutterItem.eq(index).attr('data-shutter-title');
  110. },
  111. initalShutter: function() {
  112. var $curElem = this.$shutterItem.eq(this.curDisplay);
  113. var $nearlyElem = this.$shutterItem.not($curElem);
  114. $curElem.css('zIndex', 20);
  115. $nearlyElem.each(function(i) {
  116. $(this).css('zIndex', ++i);
  117. });
  118. this.$shutter.css({ width: this.opts.shutterW, height: this.opts.shutterH });
  119. if (this.opts.fullPage) {
  120. this.$shutter.css({
  121. width: $(window).width(),
  122. height: $(window).height(),
  123. margin: 0,
  124. borderRadius: 0,
  125. border: 'none'
  126. });
  127. }
  128. },
  129. inital: function(elem) {
  130. var self = this;
  131. this.$shutter = elem;
  132. this.$shutterItem = this.$shutter.find('.shutter-img a');
  133. this.$prevBtn = this.$shutter.find('.shutter-btn .prev');
  134. this.$nextBtn = this.$shutter.find('.shutter-btn .next');
  135. this.$shutterNav = this.$shutter.find('.shutter-nav li');
  136. this.$shutterDesc = this.$shutter.find('.shutter-desc');
  137. this.shutterItem_len = this.$shutterItem.length;
  138. this.curDisplay = this.opts.curDisplay > this.shutterItem_len - 1 ? this.opts.curDisplay = this.shutterItem_len - 1 : this.opts.curDisplay;
  139. this.b_stop = true;
  140. this.shutterTitle = '';
  141. this.playTime = null;
  142. this.initalShutter();
  143. this.shutterW = this.$shutter.width();
  144. this.shutterH = this.$shutter.height();
  145. this.$prevBtn.bind('click', function() {
  146. if (self.b_stop) {
  147. self.b_stop = false;
  148. self.toggleMove('prev');
  149. }
  150. });
  151. this.$nextBtn.bind('click', function() {
  152. if (self.b_stop) {
  153. self.b_stop = false;
  154. self.toggleMove('next');
  155. self.$shutterDesc.animate({ bottom: -36 });
  156. }
  157. });
  158. if (this.opts.fullPage) {
  159. $(window).resize(function() {
  160. setTimeout(function() {
  161. self.$shutter.css({ width: $(this).width(), height: $(this).height() });
  162. self.shutterW = self.$shutter.width();
  163. self.shutterH = self.$shutter.height();
  164. }, 30);
  165. });
  166. }
  167. this.play();
  168. // this.$shutter.hover(function() {
  169. // clearInterval(self.playTime);
  170. // }, function() {
  171. // self.play();
  172. // });
  173. },
  174. // 图片切换方法
  175. recovery: function(target, cur, index, backup, interval) {
  176. var self = this;
  177. setTimeout(function() {
  178. target.css('zIndex', 20);
  179. cur.css('zIndex', self.curDisplay).html(backup);
  180. self.curDisplay = index;
  181. self.$shutterDesc.animate({ bottom: 0 }).find('p').text(self.shutterTitle);
  182. self.b_stop = true;
  183. }, interval);
  184. },
  185. gridWhole: function(index, showNum) {
  186. var self = this;
  187. var $curElem = this.$shutterItem.eq(this.curDisplay);
  188. var $targetElem = this.$shutterItem.eq(index);
  189. var backup = $curElem.html();
  190. var $createElem = $('<div class="created"></div>');
  191. var movingVal = 0;
  192. $targetElem.css('zIndex', 19);
  193. $curElem.find('.animate-img').fadeOut();
  194. $createElem.html(backup).css({
  195. position: 'absolute',
  196. zIndex: 20,
  197. left: 0,
  198. top: 0,
  199. overflow: 'hidden',
  200. width: this.shutterW,
  201. height: this.shutterH
  202. });
  203. $curElem.append($createElem);
  204. if (showNum === 0) {
  205. movingVal = this.shutterW;
  206. $createElem.velocity({ left: movingVal }, { duration: 1000 });
  207. } else if (showNum === 1) {
  208. movingVal = -this.shutterW;
  209. $createElem.velocity({ left: movingVal }, { duration: 1000 });
  210. } else if (showNum === 2) {
  211. movingVal = this.shutterH;
  212. $createElem.velocity({ top: movingVal }, { duration: 1000 });
  213. } else if (showNum === 3) {
  214. movingVal = -this.shutterH;
  215. $createElem.velocity({ top: movingVal }, { duration: 1000 });
  216. }
  217. $createElem.find('.animate-img').css({
  218. display: 'block',
  219. width: this.shutterW,
  220. height: this.shutterH
  221. });
  222. this.recovery($targetElem, $curElem, index, backup, 1200);
  223. },
  224. gridTop: function(index, showNum) {
  225. var self = this;
  226. var $curElem = this.$shutterItem.eq(this.curDisplay);
  227. var $targetElem = this.$shutterItem.eq(index);
  228. var backup = $curElem.html();
  229. var speed = 0;
  230. $targetElem.css('zIndex', 19);
  231. $curElem.find('.animate-img').fadeOut();
  232. for (var i = 0; i < 12; i++) {
  233. var $createElem = $('<div class="created"></div>');
  234. $createElem.html(backup).css({
  235. position: 'absolute',
  236. zIndex: 20,
  237. left: this.shutterW / 12 * i,
  238. top: 0,
  239. overflow: 'hidden',
  240. width: this.shutterW / 12,
  241. height: this.shutterH
  242. });
  243. $curElem.append($createElem);
  244. $createElem.find('.animate-img').css({
  245. display: 'block',
  246. width: this.shutterW,
  247. height: this.shutterH,
  248. marginLeft: this.shutterW / -12 * i
  249. });
  250. }
  251. if (showNum === 0) {
  252. var movingVal = 0;
  253. $curElem.find('.created').each(function(i) {
  254. if (i % 2 === 0) {
  255. movingVal = self.shutterH;
  256. } else {
  257. movingVal = -self.shutterH;
  258. }
  259. $(this).velocity({ top: movingVal }, { duration: 1000 });
  260. });
  261. } else if (showNum === 1) {
  262. $curElem.find('.created').each(function(i) {
  263. speed = 80 * i;
  264. $(this).velocity({ top: $(this).height() }, { duration: 120 + speed });
  265. });
  266. } else if (showNum === 2) {
  267. $curElem.find('.created').each(function(i) {
  268. speed = 80 * i;
  269. $(this).velocity({ top: -$(this).height() }, { duration: 120 + speed });
  270. });
  271. }
  272. this.recovery($targetElem, $curElem, index, backup, 1000);
  273. },
  274. gridLeft: function(index, showNum) {
  275. var self = this;
  276. var $curElem = this.$shutterItem.eq(this.curDisplay);
  277. var $targetElem = this.$shutterItem.eq(index);
  278. var backup = $curElem.html();
  279. var speed = 0;
  280. $targetElem.css('zIndex', 19);
  281. $curElem.find('.animate-img').fadeOut();
  282. for (var i = 0; i < 12; i++) {
  283. var $createElem = $('<div class="created"></div>');
  284. $createElem.html(backup).css({
  285. position: 'absolute',
  286. zIndex: 20,
  287. left: 0,
  288. top: this.shutterH / 12 * i,
  289. overflow: 'hidden',
  290. width: this.shutterW,
  291. height: this.shutterH / 12
  292. });
  293. $curElem.append($createElem);
  294. $createElem.find('.animate-img').css({
  295. display: 'block',
  296. width: this.shutterW,
  297. height: this.shutterH,
  298. marginTop: this.shutterH / -12 * i
  299. });
  300. }
  301. if (showNum === 0) {
  302. var movingVal = 0;
  303. $curElem.find('.created').each(function(i) {
  304. if (i % 2 === 0) {
  305. movingVal = self.shutterW;
  306. } else {
  307. movingVal = -self.shutterW;
  308. }
  309. $(this).velocity({ left: movingVal }, { duration: 1000 });
  310. });
  311. } else if (showNum === 1) {
  312. $curElem.find('.created').each(function(i) {
  313. speed = 80 * i;
  314. $(this).velocity({ left: $(this).width() }, { duration: 120 + speed });
  315. });
  316. } else if (showNum === 2) {
  317. $curElem.find('.created').each(function(i) {
  318. speed = 80 * i;
  319. $(this).velocity({ left: -$(this).width() }, { duration: 120 + speed });
  320. });
  321. }
  322. this.recovery($targetElem, $curElem, index, backup, 1000);
  323. },
  324. gridOpacity: function(index) {
  325. var self = this;
  326. var $curElem = this.$shutterItem.eq(this.curDisplay);
  327. var $targetElem = this.$shutterItem.eq(index);
  328. var backup = $curElem.html();
  329. var $createElem = $('<div class="created"></div>');
  330. $targetElem.css('zIndex', 19);
  331. $curElem.find('.animate-img').fadeOut();
  332. $createElem.html(backup).css({
  333. position: 'absolute',
  334. zIndex: 20,
  335. left: 0,
  336. top: 0,
  337. overflow: 'hidden',
  338. width: this.shutterW,
  339. height: this.shutterH,
  340. opacity: 1
  341. });
  342. $createElem.find('.animate-img').css({
  343. display: 'block',
  344. width: this.shutterW,
  345. height: this.shutterH
  346. });
  347. $curElem.append($createElem);
  348. $createElem.velocity({ opacity: 0 }, { duration: 1000 });
  349. this.recovery($targetElem, $curElem, index, backup, 1000);
  350. },
  351. gridAccordion: function(index) {
  352. var self = this;
  353. var $curElem = this.$shutterItem.eq(this.curDisplay);
  354. var $targetElem = this.$shutterItem.eq(index);
  355. var backup = $curElem.html();
  356. var iNow = 0;
  357. var speed = 0;
  358. $targetElem.css('zIndex', 19);
  359. $curElem.find('.animate-img').fadeOut();
  360. for (var i = 0; i < 12; i++) {
  361. var $createElem = $('<div class="created"></div>');
  362. $createElem.html(backup).css({
  363. position: 'absolute',
  364. zIndex: 20,
  365. left: this.shutterW / 12 * i,
  366. top: 0,
  367. overflow: 'hidden',
  368. width: this.shutterW / 12,
  369. height: this.shutterH,
  370. opacity: 1
  371. });
  372. $curElem.append($createElem);
  373. $createElem.find('.animate-img').css({
  374. display: 'block',
  375. width: this.shutterW,
  376. height: this.shutterH,
  377. marginLeft: this.shutterW / -12 * i
  378. });
  379. }
  380. $curElem.find('.created').each(function(i) {
  381. speed = i * 80;
  382. $(this).velocity({ opacity: 0 }, { duration: 320 + speed });
  383. });
  384. this.recovery($targetElem, $curElem, index, backup, 1200);
  385. },
  386. gridLittle: function(index) {
  387. var self = this;
  388. var $curElem = this.$shutterItem.eq(this.curDisplay);
  389. var $targetElem = this.$shutterItem.eq(index);
  390. var backup = $curElem.html();
  391. var coordinate = null;
  392. $targetElem.css('zIndex', 19);
  393. $curElem.find('.animate-img').fadeOut();
  394. for (var i = 0; i < 24; i++) {
  395. var $createElem = $('<div class="created"></div>');
  396. $createElem.html(backup).css({
  397. width: this.shutterW / 6,
  398. height: this.shutterH / 4,
  399. left: (this.shutterW / 6) * (i % 6),
  400. top: (this.shutterH / 4) * Math.floor(i / 6)
  401. });
  402. $curElem.append($createElem);
  403. $createElem.find('.animate-img').css({
  404. display: 'block',
  405. width: this.shutterW,
  406. height: this.shutterH,
  407. marginLeft: -(this.shutterW / 6) * (i % 6),
  408. marginTop: -(this.shutterH / 4) * Math.floor(i / 6)
  409. });
  410. }
  411. coordinate = getXY($curElem.find('.created'), 4, 6);
  412. tab(coordinate, 0, 0, function() {
  413. var left = parseInt(this.style.left);
  414. var top = parseInt(this.style.top);
  415. $(this).velocity({
  416. left: left + 100,
  417. top: top + 100,
  418. opacity: 0
  419. });
  420. }, 100, +1, +1);
  421. this.recovery($targetElem, $curElem, index, backup, 1200);
  422. },
  423. gridSwitch: function(index) {
  424. var self = this;
  425. var $curElem = this.$shutterItem.eq(this.curDisplay);
  426. var $targetElem = this.$shutterItem.eq(index);
  427. var backup = $curElem.html();
  428. $targetElem.css('zIndex', 19);
  429. $curElem.find('.animate-img').fadeOut();
  430. for (var i = 0; i < 20; i++) {
  431. var $createElem = $('<div class="created"></div>');
  432. $createElem.html(backup).css({
  433. width: this.shutterW / 5,
  434. height: this.shutterH / 4,
  435. left: (this.shutterW / 5) * (i % 5),
  436. top: (this.shutterH / 4) * Math.floor(i / 5)
  437. });
  438. $curElem.append($createElem);
  439. $createElem.find('.animate-img').css({
  440. display: 'block',
  441. width: this.shutterW,
  442. height: this.shutterH,
  443. marginLeft: -(this.shutterW / 5) * (i % 5),
  444. marginTop: -(this.shutterH / 4) * Math.floor(i / 5)
  445. });
  446. }
  447. $curElem.find('.created').each(function(i) {
  448. if (i % 2 === 0) {
  449. $(this).find('.animate-img').velocity({
  450. marginLeft: $(this).width()
  451. }, { duration: 500 });
  452. }
  453. });
  454. setTimeout(function() {
  455. $curElem.find('.created').each(function(i) {
  456. if (i % 1 === 0) {
  457. $(this).find('.animate-img').velocity({
  458. marginLeft: $(this).width()
  459. }, { duration: 500 });
  460. }
  461. });
  462. }, 600);
  463. this.recovery($targetElem, $curElem, index, backup, 1200);
  464. },
  465. constructor: Shutter
  466. };
  467. $.fn.shutter = function(options) {
  468. this.each(function() {
  469. var shutter = new Shutter($(this), options);
  470. });
  471. };
  472. })(jQuery, window, document);
  473. function tab(arr, x, y, fn, delay, speedX, speedY) {
  474. if (!arr[y] || !arr[y][x]) {
  475. return;
  476. }
  477. if (fn) {
  478. fn.call(arr[y][x]);
  479. clearTimeout(arr[y][x].timer);
  480. arr[y][x].timer = setTimeout(function() {
  481. tab(arr, x, y + speedY, fn, delay, speedX, speedY);
  482. tab(arr, x + speedX, y, fn, delay, speedX, speedY);
  483. }, delay);
  484. }
  485. };
  486. function getXY(objs, rows, cols) {
  487. var arr1 = [];
  488. for (var i = 0; i < rows; i++) {
  489. var arr2 = [];
  490. for (var j = 0; j < cols; j++) {
  491. objs[i * cols + j].xIndex = j;
  492. objs[i * cols + j].yIndex = i;
  493. arr2.push(objs[i * cols + j]);
  494. }
  495. arr1.push(arr2);
  496. }
  497. return arr1;
  498. };