X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=assets%2Fmain%2Fjs%2Fmain.js;h=494b9ccd04799e1c5ca76af383a419fb9aaf6ebc;hb=5f0749639ca916108518873353bb459e80423f9a;hp=fe372ee18743778425e159437497bbd45bb8f45c;hpb=8af2079e4b096e77ba2f922d6a18224ce01cb7bd;p=editorial.git diff --git a/assets/main/js/main.js b/assets/main/js/main.js index fe372ee..494b9cc 100644 --- a/assets/main/js/main.js +++ b/assets/main/js/main.js @@ -1,5 +1,5 @@ /* - Massively by HTML5 UP + Editorial by HTML5 UP html5up.net | @ajlkn Free for personal and commercial use under the CCA 3.0 license (html5up.net/license) */ @@ -7,252 +7,256 @@ (function($) { var $window = $(window), - $body = $('body'), - $wrapper = $('#wrapper'), - $header = $('#header'), - $nav = $('#nav'), - $main = $('#main'), - $navPanelToggle, $navPanel, $navPanelInner; + $head = $('head'), + $body = $('body'); // Breakpoints. breakpoints({ - default: ['1681px', null ], - xlarge: ['1281px', '1680px' ], - large: ['981px', '1280px' ], - medium: ['737px', '980px' ], - small: ['481px', '736px' ], - xsmall: ['361px', '480px' ], - xxsmall: [null, '360px' ] + xlarge: [ '1281px', '1680px' ], + large: [ '981px', '1280px' ], + medium: [ '737px', '980px' ], + small: [ '481px', '736px' ], + xsmall: [ '361px', '480px' ], + xxsmall: [ null, '360px' ], + 'xlarge-to-max': '(min-width: 1681px)', + 'small-to-xlarge': '(min-width: 481px) and (max-width: 1680px)' }); - /** - * Applies parallax scrolling to an element's background image. - * @return {jQuery} jQuery object. - */ - $.fn._parallax = function(intensity) { + // Stops animations/transitions until the page has ... - var $window = $(window), - $this = $(this); + // ... loaded. + $window.on('load', function() { + window.setTimeout(function() { + $body.removeClass('is-preload'); + }, 100); + }); - if (this.length == 0 || intensity === 0) - return $this; + // ... stopped resizing. + var resizeTimeout; - if (this.length > 1) { + $window.on('resize', function() { - for (var i=0; i < this.length; i++) - $(this[i])._parallax(intensity); + // Mark as resizing. + $body.addClass('is-resizing'); - return $this; + // Unmark after delay. + clearTimeout(resizeTimeout); - } + resizeTimeout = setTimeout(function() { + $body.removeClass('is-resizing'); + }, 100); - if (!intensity) - intensity = 0.25; + }); - $this.each(function() { + // Fixes. - var $t = $(this), - $bg = $('
').appendTo($t), - on, off; + // Object fit images. + if (!browser.canUse('object-fit') + || browser.name == 'safari') + $('.image.object').each(function() { - on = function() { + var $this = $(this), + $img = $this.children('img'); - $bg - .removeClass('fixed') - .css('transform', 'matrix(1,0,0,1,0,0)'); + // Hide original image. + $img.css('opacity', '0'); - $window - .on('scroll._parallax', function() { + // Set background. + $this + .css('background-image', 'url("' + $img.attr('src') + '")') + .css('background-size', $img.css('object-fit') ? $img.css('object-fit') : 'cover') + .css('background-position', $img.css('object-position') ? $img.css('object-position') : 'center'); - var pos = parseInt($window.scrollTop()) - parseInt($t.position().top); + }); - $bg.css('transform', 'matrix(1,0,0,1,0,' + (pos * intensity) + ')'); + // Sidebar. + var $sidebar = $('#sidebar'), + $sidebar_inner = $sidebar.children('.inner'); - }); + // Inactive by default on <= large. + breakpoints.on('<=large', function() { + $sidebar.addClass('inactive'); + }); - }; + breakpoints.on('>large', function() { + $sidebar.removeClass('inactive'); + }); - off = function() { + // Hack: Workaround for Chrome/Android scrollbar position bug. + if (browser.os == 'android' + && browser.name == 'chrome') + $('') + .appendTo($head); - $bg - .addClass('fixed') - .css('transform', 'none'); + // Toggle. + $('Toggle') + .appendTo($sidebar) + .on('click', function(event) { - $window - .off('scroll._parallax'); + // Prevent default. + event.preventDefault(); + event.stopPropagation(); - }; + // Toggle. + $sidebar.toggleClass('inactive'); - // Disable parallax on .. - if (browser.name == 'ie' // IE - || browser.name == 'edge' // Edge - || window.devicePixelRatio > 1 // Retina/HiDPI (= poor performance) - || browser.mobile) // Mobile devices - off(); + }); - // Enable everywhere else. - else { + // Events. - breakpoints.on('>large', on); - breakpoints.on('<=large', off); + // Link clicks. + $sidebar.on('click', 'a', function(event) { - } + // >large? Bail. + if (breakpoints.active('>large')) + return; - }); + // Vars. + var $a = $(this), + href = $a.attr('href'), + target = $a.attr('target'); - $window - .off('load._parallax resize._parallax') - .on('load._parallax resize._parallax', function() { - $window.trigger('scroll'); - }); + // Prevent default. + event.preventDefault(); + event.stopPropagation(); - return $(this); + // Check URL. + if (!href || href == '#' || href == '') + return; - }; + // Hide sidebar. + $sidebar.addClass('inactive'); - // Play initial animations on page load. - $window.on('load', function() { - window.setTimeout(function() { - $body.removeClass('is-preload'); - }, 100); - }); + // Redirect to href. + setTimeout(function() { - // Scrolly. - $('.scrolly').scrolly(); + if (target == '_blank') + window.open(href); + else + window.location.href = href; - // Background. - $wrapper._parallax(0.925); + }, 500); - // Nav Panel. + }); + + // Prevent certain events inside the panel from bubbling. + $sidebar.on('click touchend touchstart touchmove', function(event) { + + // >large? Bail. + if (breakpoints.active('>large')) + return; + + // Prevent propagation. + event.stopPropagation(); - // Toggle. - $navPanelToggle = $( - 'Menu' - ) - .appendTo($wrapper); - - // Change toggle styling once we've scrolled past the header. - $header.scrollex({ - bottom: '5vh', - enter: function() { - $navPanelToggle.removeClass('alt'); - }, - leave: function() { - $navPanelToggle.addClass('alt'); - } }); - // Panel. - $navPanel = $( - '' - ) - .appendTo($body) - .panel({ - delay: 500, - hideOnClick: true, - hideOnSwipe: true, - resetScroll: true, - resetForms: true, - side: 'right', - target: $body, - visibleClass: 'is-navPanel-visible' + // Hide panel on body click/tap. + $body.on('click touchend', function(event) { + + // >large? Bail. + if (breakpoints.active('>large')) + return; + + // Deactivate. + $sidebar.addClass('inactive'); + }); - // Get inner. - $navPanelInner = $navPanel.children('nav'); + // Scroll lock. + // Note: If you do anything to change the height of the sidebar's content, be sure to + // trigger 'resize.sidebar-lock' on $window so stuff doesn't get out of sync. - // Move nav content on breakpoint change. - var $navContent = $nav.children(); + $window.on('load.sidebar-lock', function() { - breakpoints.on('>medium', function() { + var sh, wh, st; - // NavPanel -> Nav. - $navContent.appendTo($nav); + // Reset scroll position to 0 if it's 1. + if ($window.scrollTop() == 1) + $window.scrollTop(0); - // Flip icon classes. - $nav.find('.icons, .icon') - .removeClass('alt'); + $window + .on('scroll.sidebar-lock', function() { - }); + var x, y; - breakpoints.on('<=medium', function() { + // <=large? Bail. + if (breakpoints.active('<=large')) { - // Nav -> NavPanel. - $navContent.appendTo($navPanelInner); + $sidebar_inner + .data('locked', 0) + .css('position', '') + .css('top', ''); - // Flip icon classes. - $navPanelInner.find('.icons, .icon') - .addClass('alt'); + return; - }); + } - // Hack: Disable transitions on WP. - if (browser.os == 'wp' - && browser.osVersion < 10) - $navPanel - .css('transition', 'none'); + // Calculate positions. + x = Math.max(sh - wh, 0); + y = Math.max(0, $window.scrollTop() - x); - // Intro. - var $intro = $('#intro'); + // Lock/unlock. + if ($sidebar_inner.data('locked') == 1) { - if ($intro.length > 0) { + if (y <= 0) + $sidebar_inner + .data('locked', 0) + .css('position', '') + .css('top', ''); + else + $sidebar_inner + .css('top', -1 * x); - // Hack: Fix flex min-height on IE. - if (browser.name == 'ie') { - $window.on('resize.ie-intro-fix', function() { + } + else { - var h = $intro.height(); + if (y > 0) + $sidebar_inner + .data('locked', 1) + .css('position', 'fixed') + .css('top', -1 * x); - if (h > $window.height()) - $intro.css('height', 'auto'); - else - $intro.css('height', h); + } - }).trigger('resize.ie-intro-fix'); - } + }) + .on('resize.sidebar-lock', function() { - // Hide intro on scroll (> small). - breakpoints.on('>small', function() { + // Calculate heights. + wh = $window.height(); + sh = $sidebar_inner.outerHeight() + 30; - $main.unscrollex(); + // Trigger scroll. + $window.trigger('scroll.sidebar-lock'); - $main.scrollex({ - mode: 'bottom', - top: '25vh', - bottom: '-50vh', - enter: function() { - $intro.addClass('hidden'); - }, - leave: function() { - $intro.removeClass('hidden'); - } - }); + }) + .trigger('resize.sidebar-lock'); }); - // Hide intro on scroll (<= small). - breakpoints.on('<=small', function() { + // Menu. + var $menu = $('#menu'), + $menu_openers = $menu.children('ul').find('.opener'); - $main.unscrollex(); + // Openers. + $menu_openers.each(function() { - $main.scrollex({ - mode: 'middle', - top: '15vh', - bottom: '-15vh', - enter: function() { - $intro.addClass('hidden'); - }, - leave: function() { - $intro.removeClass('hidden'); - } - }); + var $this = $(this); - }); + $this.on('click', function(event) { + + // Prevent default. + event.preventDefault(); + + // Toggle. + $menu_openers.not($this).removeClass('active'); + $this.toggleClass('active'); + + // Trigger resize (sidebar lock). + $window.triggerHandler('resize.sidebar-lock'); - } + }); + + }); })(jQuery); \ No newline at end of file