PageRenderTime 77ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/files/jquery.adipoli/2.0/jquery.adipoli.js

https://gitlab.com/Mirros/jsdelivr
JavaScript | 421 lines | 381 code | 12 blank | 28 comment | 96 complexity | 5f73f2aa8baae57a471b5d99392eba07 MD5 | raw file
  1. /*
  2. * Adipoli jQuery Image Hover Plugin
  3. * http://jobyj.in/adipoli
  4. *
  5. * Copyright 2012, Joby Joseph
  6. * Free to use under the MIT license.
  7. * http://www.opensource.org/licenses/mit-license.php
  8. *
  9. */
  10. (function($) {
  11. $.fn.adipoli = function(options) {
  12. // Create some defaults, extending them with any options that were provided
  13. //hovereffect: normal,sliceDown,sliceDownLeft,sliceUp,sliceUpLeft
  14. var settings = $.extend( {
  15. 'startEffect' : 'transparent',
  16. 'hoverEffect' : 'normal',
  17. 'imageOpacity' : 0.5,
  18. 'animSpeed' : 300,
  19. 'fillColor' : '#000',
  20. 'textColor' : '#fff',
  21. 'overlayText' : '',
  22. 'slices' : 10,
  23. 'boxCols' : 5,
  24. 'boxRows' : 3,
  25. 'popOutMargin' : 10,
  26. 'popOutShadow' : 10
  27. }, options);
  28. //wrap it with div
  29. //$(this).one('load',function(){
  30. $(this).one('load', function() {
  31. // do stuff
  32. $(this).wrap(function(){
  33. return '<div class="adipoli-wrapper '+$(this).attr('class')+'" style="width:'+$(this).width()+'px; height:'+$(this).height()+'px;"/>';
  34. });
  35. $(this).parent().append('<div class="adipoli-before '+$(this).attr('class')+'" style="display:none;width:'+$(this).width()+'px; height:'+$(this).height()+'px;"><img src="'+$(this).attr('src')+'"/></div>');
  36. $(this).parent().append('<div class="adipoli-after '+$(this).attr('class')+'" style="display:none;width:'+$(this).width()+'px; height:'+$(this).height()+'px;"></div>');
  37. //set start effect
  38. if(settings.startEffect=="transparent")
  39. {
  40. $(this).hide();
  41. $(this).siblings('.adipoli-before').css({
  42. '-ms-filter': '"progid:DXImageTransform.Microsoft.Alpha(Opacity='+settings.imageOpacity*100+')"',
  43. 'filter': 'alpha(opacity='+settings.imageOpacity*100+')',
  44. '-moz-opacity': settings.imageOpacity,
  45. '-khtml-opacity': settings.imageOpacity,
  46. 'opacity': settings.imageOpacity
  47. }).show();
  48. }
  49. else if(settings.startEffect=="grayscale")
  50. {
  51. var element=$(this);
  52. element.hide();
  53. element.siblings('.adipoli-before').show();
  54. element.siblings('.adipoli-before').children('img').each(function(){
  55. this.src = grayscale(this.src);
  56. });
  57. }
  58. else if(settings.startEffect=="normal")
  59. {
  60. $(this).hide();
  61. $(this).siblings('.adipoli-before').show();
  62. }
  63. else if(settings.startEffect=="overlay")
  64. {
  65. element=$(this);
  66. element.hide();
  67. $(this).siblings('.adipoli-before').html(settings.overlayText).css({
  68. '-ms-filter': '"progid:DXImageTransform.Microsoft.Alpha(Opacity='+settings.imageOpacity*100+')"',
  69. 'filter': 'alpha(opacity='+settings.imageOpacity*100+')',
  70. '-moz-opacity': settings.imageOpacity,
  71. '-khtml-opacity': settings.imageOpacity,
  72. 'opacity': settings.imageOpacity,
  73. 'background':settings.fillColor,
  74. 'color':settings.textColor
  75. }).fadeIn();
  76. element.show();
  77. }
  78. //binding events for mouseover
  79. $(this).parent().bind('mouseenter',function(){
  80. if(settings.hoverEffect=='normal')
  81. {
  82. var element=$(this);
  83. element.children('.adipoli-after').html('<img src="'+element.children('img').attr('src')+'"/>').fadeIn(settings.animSpeed);
  84. }
  85. else if(settings.hoverEffect=='popout')
  86. {
  87. element=$(this);
  88. var curImageWidth=element.children('img').width();
  89. var curImageHeight=element.children('img').height();
  90. element.children('.adipoli-after').html('<img src="'+element.children('img').attr('src')+'"/>');
  91. var popOutImage=element.children('.adipoli-after').children('img');
  92. popOutImage.width(curImageWidth+2*settings.popOutMargin);
  93. popOutImage.height(curImageHeight+2*settings.popOutMargin);
  94. element.children('.adipoli-after').width(curImageWidth+2*settings.popOutMargin);
  95. element.children('.adipoli-after').height(curImageHeight+2*settings.popOutMargin);
  96. element.children('.adipoli-after').css({
  97. 'left':'-'+settings.popOutMargin+'px',
  98. 'top':'-'+settings.popOutMargin+'px',
  99. 'box-shadow':'0px 0px '+settings.popOutShadow+'px #000'
  100. }).show();
  101. }
  102. else if(settings.hoverEffect=='sliceDown' || settings.hoverEffect=='sliceDownLeft'|| settings.hoverEffect=='sliceUp' || settings.hoverEffect=='sliceUpLeft' || settings.hoverEffect=='sliceUpRandom' || settings.hoverEffect=='sliceDownRandom')
  103. {
  104. $(this).children('.adipoli-after').show();
  105. createSlices($(this),settings);
  106. var timeBuff = 0;
  107. var i = 0;
  108. var slices = $('.adipoli-slice', $(this));
  109. if(settings.hoverEffect == 'sliceDownLeft' || settings.hoverEffect=='sliceUpLeft') slices = $('.adipoli-slice', $(this))._reverse();
  110. if(settings.hoverEffect=='sliceUpRandom' || settings.hoverEffect=='sliceDownRandom') slices = shuffle($('.adipoli-slice', $(this)));
  111. slices.each(function(){
  112. var slice = $(this);
  113. if(settings.hoverEffect=='sliceDown' || settings.hoverEffect=='sliceDownLeft'){
  114. slice.css({
  115. 'top': '0px'
  116. });
  117. }
  118. else if(settings.hoverEffect=='sliceUp' || settings.hoverEffect=='sliceUpLeft')
  119. {
  120. slice.css({
  121. 'bottom': '0px'
  122. });
  123. }
  124. if(i == settings.slices-1){
  125. setTimeout(function(){
  126. slice.animate({
  127. height:'100%',
  128. opacity:'1.0'
  129. }, settings.animSpeed, '', function(){
  130. //event to fire when animation finish
  131. });
  132. }, (100 + timeBuff));
  133. } else {
  134. setTimeout(function(){
  135. slice.animate({
  136. height:'100%',
  137. opacity:'1.0'
  138. }, settings.animSpeed);
  139. }, (100 + timeBuff));
  140. }
  141. timeBuff += 50;
  142. i++;
  143. });
  144. }
  145. else if(settings.hoverEffect == 'sliceUpDown' || settings.hoverEffect == 'sliceUpDownLeft'){
  146. $(this).children('.adipoli-after').show();
  147. createSlices($(this),settings);
  148. timeBuff = 0;
  149. i = 0;
  150. var v = 0;
  151. slices = $('.adipoli-slice', $(this));
  152. if(settings.hoverEffect == 'sliceUpDownLeft') slices = $('.adipoli-slice', $(this))._reverse();
  153. slices.each(function(){
  154. var slice = $(this);
  155. if(i == 0){
  156. slice.css('top','0px');
  157. i++;
  158. } else {
  159. slice.css('bottom','0px');
  160. i = 0;
  161. }
  162. if(v == settings.slices-1){
  163. setTimeout(function(){
  164. slice.animate({
  165. height:'100%',
  166. opacity:'1.0'
  167. }, settings.animSpeed, '', function(){
  168. //event when animation finish
  169. });
  170. }, (100 + timeBuff));
  171. } else {
  172. setTimeout(function(){
  173. slice.animate({
  174. height:'100%',
  175. opacity:'1.0'
  176. }, settings.animSpeed);
  177. }, (100 + timeBuff));
  178. }
  179. timeBuff += 50;
  180. v++;
  181. });
  182. }
  183. else if(settings.hoverEffect == 'fold'||settings.hoverEffect == 'foldLeft'){
  184. $(this).children('.adipoli-after').show();
  185. element=$(this);
  186. createSlices(element,settings);
  187. timeBuff = 0;
  188. i = 0;
  189. slices=$('.adipoli-slice', element);
  190. if(settings.hoverEffect == 'foldLeft') slices = $('.adipoli-slice', $(this))._reverse();
  191. slices.each(function(){
  192. var slice = $(this);
  193. var origWidth = slice.width();
  194. slice.css({
  195. top:'0px',
  196. height:'100%',
  197. width:'0px'
  198. });
  199. if(i == settings.slices-1){
  200. setTimeout(function(){
  201. slice.animate({
  202. width:origWidth,
  203. opacity:'1.0'
  204. }, settings.animSpeed, '', function(){
  205. //trigger when animation finish
  206. });
  207. }, (100 + timeBuff));
  208. } else {
  209. setTimeout(function(){
  210. slice.animate({
  211. width:origWidth,
  212. opacity:'1.0'
  213. }, settings.animSpeed);
  214. }, (100 + timeBuff));
  215. }
  216. timeBuff += 50;
  217. i++;
  218. });
  219. }
  220. else if(settings.hoverEffect == 'boxRandom'){
  221. $(this).children('.adipoli-after').show();
  222. element=$(this);
  223. createBoxes(element, settings);
  224. var totalBoxes = settings.boxCols * settings.boxRows;
  225. i = 0;
  226. timeBuff = 0;
  227. var boxes = shuffle($('.adipoli-box', element));
  228. boxes.each(function(){
  229. var box = $(this);
  230. if(i == totalBoxes-1){
  231. setTimeout(function(){
  232. box.animate({
  233. opacity:'1'
  234. }, settings.animSpeed, '', function(){});
  235. }, (100 + timeBuff));
  236. } else {
  237. setTimeout(function(){
  238. box.animate({
  239. opacity:'1'
  240. }, settings.animSpeed);
  241. }, (100 + timeBuff));
  242. }
  243. timeBuff += 20;
  244. i++;
  245. });
  246. }
  247. else if(settings.hoverEffect == 'boxRain' || settings.hoverEffect == 'boxRainReverse' || settings.hoverEffect == 'boxRainGrow' || settings.hoverEffect == 'boxRainGrowReverse'){
  248. $(this).children('.adipoli-after').show();
  249. element=$(this);
  250. createBoxes(element, settings);
  251. totalBoxes = settings.boxCols * settings.boxRows;
  252. i = 0;
  253. timeBuff = 0;
  254. // Split boxes into 2D array
  255. var rowIndex = 0;
  256. var colIndex = 0;
  257. var box2Darr = new Array();
  258. box2Darr[rowIndex] = new Array();
  259. boxes = $('.adipoli-box', element);
  260. if(settings.hoverEffect == 'boxRainReverse' || settings.hoverEffect == 'boxRainGrowReverse'){
  261. boxes = $('.adipoli-box', element)._reverse();
  262. }
  263. boxes.each(function(){
  264. box2Darr[rowIndex][colIndex] = $(this);
  265. colIndex++;
  266. if(colIndex == settings.boxCols){
  267. rowIndex++;
  268. colIndex = 0;
  269. box2Darr[rowIndex] = new Array();
  270. }
  271. });
  272. // Run animation
  273. for(var cols = 0; cols < (settings.boxCols * 2); cols++){
  274. var prevCol = cols;
  275. for(var rows = 0; rows < settings.boxRows; rows++){
  276. if(prevCol >= 0 && prevCol < settings.boxCols){
  277. (function(row, col, time, i, totalBoxes) {
  278. var box = $(box2Darr[row][col]);
  279. var w = box.width();
  280. var h = box.height();
  281. if(settings.hoverEffect == 'boxRainGrow' || settings.hoverEffect == 'boxRainGrowReverse'){
  282. box.width(0).height(0);
  283. }
  284. if(i == totalBoxes-1){
  285. setTimeout(function(){
  286. box.animate({
  287. opacity:'1',
  288. width:w,
  289. height:h
  290. }, settings.animSpeed/1.3, '', function(){
  291. //animation complete event
  292. });
  293. }, (100 + time));
  294. } else {
  295. setTimeout(function(){
  296. box.animate({
  297. opacity:'1',
  298. width:w,
  299. height:h
  300. }, settings.animSpeed/1.3);
  301. }, (100 + time));
  302. }
  303. })(rows, prevCol, timeBuff, i, totalBoxes);
  304. i++;
  305. }
  306. prevCol--;
  307. }
  308. timeBuff += 100;
  309. }
  310. }
  311. });
  312. //binding events for mouseleave
  313. $(this).parent().bind('mouseleave',function(){
  314. $(this).children('.adipoli-after').html('').hide();
  315. });
  316. }).each(function() {
  317. if(this.complete) $(this).load();
  318. });
  319. //});
  320. return $(this);
  321. // Add slices for slice animations
  322. function createSlices(element, settings){
  323. for(var i = 0; i < settings.slices; i++){
  324. var sliceWidth = Math.round(element.width()/settings.slices);
  325. if(i == settings.slices-1){
  326. element.children('.adipoli-after').append(
  327. $('<div class="adipoli-slice"></div>').css({
  328. left:(sliceWidth*i)+'px',
  329. width:(element.width()-(sliceWidth*i))+'px',
  330. height:'0px',
  331. opacity:'0',
  332. background: 'url("'+ element.children('img').attr('src') +'") no-repeat -'+ ((sliceWidth + (i * sliceWidth)) - sliceWidth) +'px 0%'
  333. })
  334. );
  335. } else {
  336. element.children('.adipoli-after').append(
  337. $('<div class="adipoli-slice"></div>').css({
  338. left:(sliceWidth*i)+'px',
  339. width:sliceWidth+'px',
  340. height:'0px',
  341. opacity:'0',
  342. background: 'url("'+ element.children('img').attr('src') +'") no-repeat -'+ ((sliceWidth + (i * sliceWidth)) - sliceWidth) +'px 0%'
  343. })
  344. );
  345. }
  346. }
  347. }
  348. // Add boxes for box animations
  349. function createBoxes(element, settings){
  350. var boxWidth = Math.round(element.width()/settings.boxCols);
  351. var boxHeight = Math.round(element.height()/settings.boxRows);
  352. for(var rows = 0; rows < settings.boxRows; rows++){
  353. for(var cols = 0; cols < settings.boxCols; cols++){
  354. if(cols == settings.boxCols-1){
  355. element.children('.adipoli-after').append(
  356. $('<div class="adipoli-box"></div>').css({
  357. opacity:0,
  358. left:(boxWidth*cols)+'px',
  359. top:(boxHeight*rows)+'px',
  360. width:(element.width()-(boxWidth*cols))+'px',
  361. height:boxHeight+'px',
  362. background: 'url("'+element.children('img').attr('src') +'") no-repeat -'+ ((boxWidth + (cols * boxWidth)) - boxWidth) +'px -'+ ((boxHeight + (rows * boxHeight)) - boxHeight) +'px'
  363. })
  364. );
  365. } else {
  366. element.children('.adipoli-after').append(
  367. $('<div class="adipoli-box"></div>').css({
  368. opacity:0,
  369. left:(boxWidth*cols)+'px',
  370. top:(boxHeight*rows)+'px',
  371. width:boxWidth+'px',
  372. height:boxHeight+'px',
  373. background: 'url("'+ element.children('img').attr('src') +'") no-repeat -'+ ((boxWidth + (cols * boxWidth)) - boxWidth) +'px -'+ ((boxHeight + (rows * boxHeight)) - boxHeight) +'px'
  374. })
  375. );
  376. }
  377. }
  378. }
  379. }
  380. // Shuffle an array
  381. function shuffle(arr){
  382. for(var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
  383. return arr;
  384. }
  385. // Grayscale w canvas method
  386. function grayscale(src){
  387. var canvas = document.createElement('canvas');
  388. var ctx = canvas.getContext('2d');
  389. var imgObj = new Image();
  390. imgObj.src = src;
  391. canvas.width = imgObj.width;
  392. canvas.height = imgObj.height;
  393. ctx.drawImage(imgObj, 0, 0);
  394. var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
  395. for(var y = 0; y < imgPixels.height; y++){
  396. for(var x = 0; x < imgPixels.width; x++){
  397. var i = (y * 4) * imgPixels.width + x * 4;
  398. var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
  399. imgPixels.data[i] = avg;
  400. imgPixels.data[i + 1] = avg;
  401. imgPixels.data[i + 2] = avg;
  402. }
  403. }
  404. ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
  405. return canvas.toDataURL();
  406. }
  407. };
  408. $.fn._reverse = [].reverse;
  409. })(jQuery);