scripts.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. Bones Scripts File
  3. Author: Eddie Machado
  4. This file should contain any js scripts you want to add to the site.
  5. Instead of calling it in the header or throwing it inside wp_head()
  6. this file will be called automatically in the footer so as not to
  7. slow the page load.
  8. */
  9. // IE8 ployfill for GetComputed Style (for Responsive Script below)
  10. if (!window.getComputedStyle) {
  11. window.getComputedStyle = function(el, pseudo) {
  12. this.el = el;
  13. this.getPropertyValue = function(prop) {
  14. var re = /(\-([a-z]){1})/g;
  15. if (prop == 'float') prop = 'styleFloat';
  16. if (re.test(prop)) {
  17. prop = prop.replace(re, function () {
  18. return arguments[2].toUpperCase();
  19. });
  20. }
  21. return el.currentStyle[prop] ? el.currentStyle[prop] : null;
  22. }
  23. return this;
  24. }
  25. }
  26. // as the page loads, call these scripts
  27. jQuery(document).ready(function($) {
  28. /*
  29. Responsive jQuery is a tricky thing.
  30. There's a bunch of different ways to handle
  31. it, so be sure to research and find the one
  32. that works for you best.
  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. var viewport = updateViewportDimensions();
  44. /*
  45. * Throttle Resize-triggered Events
  46. * Wrap your actions in this function to throttle the frequency of firing them off, for better performance, esp. on mobile.
  47. * ( source: http://stackoverflow.com/questions/2854407/javascript-jquery-window-resize-how-to-fire-after-the-resize-is-completed )
  48. */
  49. var waitForFinalEvent = (function () {
  50. var timers = {};
  51. return function (callback, ms, uniqueId) {
  52. if (!uniqueId) { uniqueId = "Don't call this twice without a uniqueId"; }
  53. if (timers[uniqueId]) { clearTimeout (timers[uniqueId]); }
  54. timers[uniqueId] = setTimeout(callback, ms);
  55. };
  56. })();
  57. // how long to wait before deciding the resize has stopped, in ms. Around 50-100 should work ok.
  58. var timeToWaitForLast = 100;
  59. /*
  60. * Setup Resize-triggered Actions
  61. * These will be more resource intensive than one-off checks made at load-time
  62. */
  63. /* Example, uncomment and edit as needed.
  64. // Are we on a page where we need to do something? Create one-time flags for efficient checks.
  65. // Another good thing to check for might be body.no-touch, to avoid running UI interactivity on touch devices.
  66. if( typeof is_home === "undefined" ) var is_home = $('body').hasClass('home');
  67. $(window).resize(function () {
  68. // Test on flags and do something if needed
  69. if( is_home ) { waitForFinalEvent( function() {
  70. callMyResizeDependentFunction();
  71. }, timeToWaitForLast, "your-function-identifier-string"); }
  72. });
  73. // Example function
  74. function callMyResizeDependentFunction() {
  75. viewport = updateViewportDimensions();
  76. if( viewport.width >= 768 ) {
  77. console.log('On home page and window sized to 768 width or more.');
  78. } else {
  79. console.log('Not on home page, or window sized to less than 768.');
  80. }
  81. }
  82. callMyResizeDependentFunction(); // initial page load call
  83. */
  84. /*
  85. * Resize-unaware responsive scripts
  86. */
  87. /* if is below 481px */
  88. if (viewport.width < 481) {
  89. } /* end smallest screen */
  90. /* if is larger than 481px */
  91. if (viewport.width > 481) {
  92. } /* end larger than 481px */
  93. /* if is above or equal to 768px */
  94. if (viewport.width >= 768) {
  95. /* load gravatars */
  96. $('.comment img[data-gravatar]').each(function(){
  97. $(this).attr('src',$(this).attr('data-gravatar'));
  98. });
  99. }
  100. /* off the bat large screen actions */
  101. if (viewport.width > 1030) {
  102. }
  103. // add your scripts below this line
  104. }); /* end of as page load scripts */
  105. /*! A fix for the iOS orientationchange zoom bug.
  106. Script by @scottjehl, rebound by @wilto.
  107. MIT License.
  108. */
  109. (function(w){
  110. // This fix addresses an iOS bug, so return early if the UA claims it's something else.
  111. if( !( /iPhone|iPad|iPod/.test( navigator.platform ) && navigator.userAgent.indexOf( "AppleWebKit" ) > -1 ) ){ return; }
  112. var doc = w.document;
  113. if( !doc.querySelector ){ return; }
  114. var meta = doc.querySelector( "meta[name=viewport]" ),
  115. initialContent = meta && meta.getAttribute( "content" ),
  116. disabledZoom = initialContent + ",maximum-scale=1",
  117. enabledZoom = initialContent + ",maximum-scale=10",
  118. enabled = true,
  119. x, y, z, aig;
  120. if( !meta ){ return; }
  121. function restoreZoom(){
  122. meta.setAttribute( "content", enabledZoom );
  123. enabled = true; }
  124. function disableZoom(){
  125. meta.setAttribute( "content", disabledZoom );
  126. enabled = false; }
  127. function checkTilt( e ){
  128. aig = e.accelerationIncludingGravity;
  129. x = Math.abs( aig.x );
  130. y = Math.abs( aig.y );
  131. z = Math.abs( aig.z );
  132. // If portrait orientation and in one of the danger zones
  133. if( !w.orientation && ( x > 7 || ( ( z > 6 && y < 8 || z < 8 && y > 6 ) && x > 5 ) ) ){
  134. if( enabled ){ disableZoom(); } }
  135. else if( !enabled ){ restoreZoom(); } }
  136. w.addEventListener( "orientationchange", restoreZoom, false );
  137. w.addEventListener( "devicemotion", checkTilt, false );
  138. })( this );