script.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. (function($){
  2. // Search
  3. var $searchWrap = $('#search-form-wrap'),
  4. isSearchAnim = false,
  5. searchAnimDuration = 200;
  6. var startSearchAnim = function(){
  7. isSearchAnim = true;
  8. };
  9. var stopSearchAnim = function(callback){
  10. setTimeout(function(){
  11. isSearchAnim = false;
  12. callback && callback();
  13. }, searchAnimDuration);
  14. };
  15. $('#nav-search-btn').on('click', function(){
  16. if (isSearchAnim) return;
  17. startSearchAnim();
  18. $searchWrap.addClass('on');
  19. stopSearchAnim(function(){
  20. $('.search-form-input').focus();
  21. });
  22. });
  23. $('.search-form-input').on('blur', function(){
  24. startSearchAnim();
  25. $searchWrap.removeClass('on');
  26. stopSearchAnim();
  27. });
  28. // Share
  29. $('body').on('click', function(){
  30. $('.article-share-box.on').removeClass('on');
  31. }).on('click', '.article-share-link', function(e){
  32. e.stopPropagation();
  33. var $this = $(this),
  34. url = $this.attr('data-url'),
  35. encodedUrl = encodeURIComponent(url),
  36. id = 'article-share-box-' + $this.attr('data-id'),
  37. offset = $this.offset();
  38. if ($('#' + id).length){
  39. var box = $('#' + id);
  40. if (box.hasClass('on')){
  41. box.removeClass('on');
  42. return;
  43. }
  44. } else {
  45. var html = [
  46. '<div id="' + id + '" class="article-share-box">',
  47. '<input class="article-share-input" value="' + url + '">',
  48. '<div class="article-share-links">',
  49. '<a href="https://twitter.com/intent/tweet?url=' + encodedUrl + '" class="article-share-twitter" target="_blank" title="Twitter"></a>',
  50. '<a href="https://www.facebook.com/sharer.php?u=' + encodedUrl + '" class="article-share-facebook" target="_blank" title="Facebook"></a>',
  51. '<a href="http://pinterest.com/pin/create/button/?url=' + encodedUrl + '" class="article-share-pinterest" target="_blank" title="Pinterest"></a>',
  52. '<a href="https://plus.google.com/share?url=' + encodedUrl + '" class="article-share-google" target="_blank" title="Google+"></a>',
  53. '</div>',
  54. '</div>'
  55. ].join('');
  56. var box = $(html);
  57. $('body').append(box);
  58. }
  59. $('.article-share-box.on').hide();
  60. box.css({
  61. top: offset.top + 25,
  62. left: offset.left
  63. }).addClass('on');
  64. }).on('click', '.article-share-box', function(e){
  65. e.stopPropagation();
  66. }).on('click', '.article-share-box-input', function(){
  67. $(this).select();
  68. }).on('click', '.article-share-box-link', function(e){
  69. e.preventDefault();
  70. e.stopPropagation();
  71. window.open(this.href, 'article-share-box-window-' + Date.now(), 'width=500,height=450');
  72. });
  73. // Caption
  74. $('.article-entry').each(function(i){
  75. $(this).find('img').each(function(){
  76. if ($(this).parent().hasClass('fancybox')) return;
  77. var alt = this.alt;
  78. if (alt) $(this).after('<span class="caption">' + alt + '</span>');
  79. $(this).wrap('<a href="' + this.src + '" title="' + alt + '" class="fancybox"></a>');
  80. });
  81. $(this).find('.fancybox').each(function(){
  82. $(this).attr('rel', 'article' + i);
  83. });
  84. });
  85. if ($.fancybox){
  86. $('.fancybox').fancybox();
  87. }
  88. // Mobile nav
  89. var $container = $('#container'),
  90. isMobileNavAnim = false,
  91. mobileNavAnimDuration = 200;
  92. var startMobileNavAnim = function(){
  93. isMobileNavAnim = true;
  94. };
  95. var stopMobileNavAnim = function(){
  96. setTimeout(function(){
  97. isMobileNavAnim = false;
  98. }, mobileNavAnimDuration);
  99. }
  100. $('#main-nav-toggle').on('click', function(){
  101. if (isMobileNavAnim) return;
  102. startMobileNavAnim();
  103. $container.toggleClass('mobile-nav-on');
  104. stopMobileNavAnim();
  105. });
  106. $('#wrap').on('click', function(){
  107. if (isMobileNavAnim || !$container.hasClass('mobile-nav-on')) return;
  108. $container.removeClass('mobile-nav-on');
  109. });
  110. })(jQuery);