bhalash

Ad loader

Sep 17th, 2014
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 1.80 KB | None | 0 0
  1. 'use strict';
  2.  
  3. var mobileBreak = 770;
  4.  
  5. var imgSuffix = {
  6.     mobile  : '_mobile_',
  7.     desktop : '_desktop_'
  8. }
  9.  
  10. var adGroups = {
  11.     header     : '.g-1',
  12.     sidebar    : '.g-2',
  13.     sidebarInt :'.g-col',
  14.     footer     : '.g-3'    
  15. }
  16.  
  17. function centerAdvertAmnt(obj) {
  18.     var a = obj.width();
  19.     var b = obj.parent().width();
  20.     return (b * 0.5) - (a * 0.5);
  21. }
  22.  
  23. function swapAdvertImage(obj) {
  24.     $(obj).children('img').each(function() {
  25.         var img = $(this).attr('src');
  26.  
  27.         if ($(window).width() > mobileBreak) {
  28.             if (img.indexOf(imgSuffix.desktop) == -1)
  29.                 img = img.replace(imgSuffix.mobile, imgSuffix.desktop);  
  30.         }
  31.        
  32.         if ($(window).width() <= mobileBreak) {
  33.             if (img.indexOf(imgSuffix.mobile) == -1)
  34.                 img = img.replace(imgSuffix.desktop, imgSuffix.mobile);
  35.         }
  36.  
  37.         $(this).attr('src', img);
  38.     });
  39. }
  40.  
  41. function resizeAdvert(obj) {
  42.     $(obj).each(function() {
  43.         if ($(this).children('img').length > 0) {
  44.             var h = $(this).children('img').first().height();
  45.             var w = $(this).children('img').first().width();
  46.  
  47.             $(this).css({'height' : h + 'px','width'  : w + 'px'});
  48.             $(this).children('img').toggle();
  49.             $(this).parent().css('padding-left', centerAdvertAmnt($(this)));
  50.         }
  51.     });
  52. }
  53.  
  54. $(function() {
  55.     console.log('Testing');
  56.  
  57.     $.each(adGroups, function(k,v) {
  58.         swapAdvertImage(v + ' a');
  59.     });
  60. });
  61.  
  62. $(window).load(function() {
  63.     $(adGroups.sidebar).css('width', $('#sidebar').width() + 'px');
  64.  
  65.     $.each(adGroups, function(k,v) {
  66.         resizeAdvert(v + ' a');
  67.     });
  68. });
  69.  
  70. $(window).resize(function() {
  71.     $.each(adGroups, function(k,v) {
  72.         swapAdvertImage(v + ' a');
  73.     });
  74. });
Advertisement
Add Comment
Please, Sign In to add comment