scripts.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Bones Scripts File
  3. * Author: Eddie Machado
  4. *
  5. * This file should contain any js scripts you want to add to the site.
  6. * Instead of calling it in the header or throwing it inside wp_head()
  7. * this file will be called automatically in the footer so as not to
  8. * slow the page load.
  9. *
  10. * There are a lot of example functions and tools in here. If you don't
  11. * need any of it, just remove it. They are meant to be helpers and are
  12. * not required. It's your world baby, you can do whatever you want.
  13. */
  14. /*
  15. * IE8 ployfill for GetComputed Style (for Responsive Script below)
  16. * If you don't want to support IE8, you can just remove this.
  17. */
  18. if (!window.getComputedStyle) {
  19. window.getComputedStyle = function(el, pseudo) {
  20. this.el = el;
  21. this.getPropertyValue = function(prop) {
  22. var re = /(\-([a-z]){1})/g;
  23. if (prop == 'float') prop = 'styleFloat';
  24. if (re.test(prop)) {
  25. prop = prop.replace(re, function () {
  26. return arguments[2].toUpperCase();
  27. });
  28. }
  29. return el.currentStyle[prop] ? el.currentStyle[prop] : null;
  30. }
  31. return this;
  32. }
  33. }
  34. /*
  35. * Get Viewport Dimensions
  36. * returns object with viewport dimensions to match css in width and height properties
  37. * ( source: http://andylangton.co.uk/blog/development/get-viewport-size-width-and-height-javascript )
  38. */
  39. function updateViewportDimensions() {
  40. var w=window,d=document,e=d.documentElement,g=d.getElementsByTagName('body')[0],x=w.innerWidth||e.clientWidth||g.clientWidth,y=w.innerHeight||e.clientHeight||g.clientHeight;
  41. return {width:x,height:y}
  42. }
  43. // setting the viewport width
  44. var viewport = updateViewportDimensions();
  45. /*
  46. * Throttle Resize-triggered Events
  47. * Wrap your actions in this function to throttle the frequency of firing them off, for better performance, esp. on mobile.
  48. * ( source: http://stackoverflow.com/questions/2854407/javascript-jquery-window-resize-how-to-fire-after-the-resize-is-completed )
  49. */
  50. var waitForFinalEvent = (function () {
  51. var timers = {};
  52. return function (callback, ms, uniqueId) {
  53. if (!uniqueId) { uniqueId = "Don't call this twice without a uniqueId"; }
  54. if (timers[uniqueId]) { clearTimeout (timers[uniqueId]); }
  55. timers[uniqueId] = setTimeout(callback, ms);
  56. };
  57. })();
  58. // how long to wait before deciding the resize has stopped, in ms. Around 50-100 should work ok.
  59. var timeToWaitForLast = 100;
  60. /*
  61. * Here's an example so you can see how we're using the above function
  62. *
  63. * This is commented out so it won't work, but you can copy it and
  64. * remove the comments.
  65. *
  66. *
  67. *
  68. * If we want to only do it on a certain page, we can setup checks so we do it
  69. * as efficient as possible.
  70. *
  71. * if( typeof is_home === "undefined" ) var is_home = $('body').hasClass('home');
  72. *
  73. * This once checks to see if you're on the home page based on the body class
  74. * We can then use that check to perform actions on the home page only
  75. *
  76. * When the window is resized, we perform this function
  77. * $(window).resize(function () {
  78. *
  79. * // if we're on the home page, we wait the set amount (in function above) then fire the function
  80. * if( is_home ) { waitForFinalEvent( function() {
  81. *
  82. * // if we're above or equal to 768 fire this off
  83. * if( viewport.width >= 768 ) {
  84. * console.log('On home page and window sized to 768 width or more.');
  85. * } else {
  86. * // otherwise, let's do this instead
  87. * console.log('Not on home page, or window sized to less than 768.');
  88. * }
  89. *
  90. * }, timeToWaitForLast, "your-function-identifier-string"); }
  91. * });
  92. *
  93. * Pretty cool huh? You can create functions like this to conditionally load
  94. * content and other stuff dependent on the viewport.
  95. * Remember that mobile devices and javascript aren't the best of friends.
  96. * Keep it light and always make sure the larger viewports are doing the heavy lifting.
  97. *
  98. */
  99. /*
  100. * We're going to swap out the gravatars.
  101. * In the functions.php file, you can see we're not loading the gravatar
  102. * images on mobile to save bandwidth. Once we hit an acceptable viewport
  103. * then we can swap out those images since they are located in a data attribute.
  104. */
  105. function loadGravatars() {
  106. // set the viewport using the function above
  107. viewport = updateViewportDimensions();
  108. // if the viewport is tablet or larger, we load in the gravatars
  109. if (viewport.width >= 768) {
  110. $('.comment img[data-gravatar]').each(function(){
  111. jQuery(this).attr('src',$(this).attr('data-gravatar'));
  112. });
  113. }
  114. } // end function
  115. /*
  116. * Put all your regular jQuery in here.
  117. */
  118. jQuery(document).ready(function($) {
  119. /*
  120. * Let's fire off the gravatar function
  121. * You can remove this if you don't need it
  122. */
  123. loadGravatars();
  124. }); /* end of as page load scripts */