scripts.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. * Get Viewport Dimensions
  16. * returns object with viewport dimensions to match css in width and height properties
  17. * ( source: http://andylangton.co.uk/blog/development/get-viewport-size-width-and-height-javascript )
  18. */
  19. function updateViewportDimensions() {
  20. 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;
  21. return { width:x,height:y };
  22. }
  23. // setting the viewport width
  24. var viewport = updateViewportDimensions();
  25. /*
  26. * Throttle Resize-triggered Events
  27. * Wrap your actions in this function to throttle the frequency of firing them off, for better performance, esp. on mobile.
  28. * ( source: http://stackoverflow.com/questions/2854407/javascript-jquery-window-resize-how-to-fire-after-the-resize-is-completed )
  29. */
  30. var waitForFinalEvent = (function () {
  31. var timers = {};
  32. return function (callback, ms, uniqueId) {
  33. if (!uniqueId) { uniqueId = "Don't call this twice without a uniqueId"; }
  34. if (timers[uniqueId]) { clearTimeout (timers[uniqueId]); }
  35. timers[uniqueId] = setTimeout(callback, ms);
  36. };
  37. })();
  38. // how long to wait before deciding the resize has stopped, in ms. Around 50-100 should work ok.
  39. var timeToWaitForLast = 100;
  40. /*
  41. * Here's an example so you can see how we're using the above function
  42. *
  43. * This is commented out so it won't work, but you can copy it and
  44. * remove the comments.
  45. *
  46. *
  47. *
  48. * If we want to only do it on a certain page, we can setup checks so we do it
  49. * as efficient as possible.
  50. *
  51. * if( typeof is_home === "undefined" ) var is_home = $('body').hasClass('home');
  52. *
  53. * This once checks to see if you're on the home page based on the body class
  54. * We can then use that check to perform actions on the home page only
  55. *
  56. * When the window is resized, we perform this function
  57. * $(window).resize(function () {
  58. *
  59. * // if we're on the home page, we wait the set amount (in function above) then fire the function
  60. * if( is_home ) { waitForFinalEvent( function() {
  61. *
  62. * // update the viewport, in case the window size has changed
  63. * viewport = updateViewportDimensions();
  64. *
  65. * // if we're above or equal to 768 fire this off
  66. * if( viewport.width >= 768 ) {
  67. * console.log('On home page and window sized to 768 width or more.');
  68. * } else {
  69. * // otherwise, let's do this instead
  70. * console.log('Not on home page, or window sized to less than 768.');
  71. * }
  72. *
  73. * }, timeToWaitForLast, "your-function-identifier-string"); }
  74. * });
  75. *
  76. * Pretty cool huh? You can create functions like this to conditionally load
  77. * content and other stuff dependent on the viewport.
  78. * Remember that mobile devices and javascript aren't the best of friends.
  79. * Keep it light and always make sure the larger viewports are doing the heavy lifting.
  80. *
  81. */
  82. /*
  83. * We're going to swap out the gravatars.
  84. * In the functions.php file, you can see we're not loading the gravatar
  85. * images on mobile to save bandwidth. Once we hit an acceptable viewport
  86. * then we can swap out those images since they are located in a data attribute.
  87. */
  88. function loadGravatars() {
  89. // set the viewport using the function above
  90. viewport = updateViewportDimensions();
  91. // if the viewport is tablet or larger, we load in the gravatars
  92. if (viewport.width >= 768) {
  93. jQuery('.comment img[data-gravatar]').each(function(){
  94. jQuery(this).attr('src',jQuery(this).attr('data-gravatar'));
  95. });
  96. }
  97. } // end function
  98. /*
  99. * Put all your regular jQuery in here.
  100. */
  101. jQuery(document).ready(function($) {
  102. /*
  103. * Let's fire off the gravatar function
  104. * You can remove this if you don't need it
  105. */
  106. loadGravatars();
  107. }); /* end of as page load scripts */