'use strict'; (function($) { /** * SmoothScroll: アンカーリンクのスムーススクロール */ var SmoothScroll = function(element, options) { this.$window = $(window); this.$trigger = $(element); this.init(options); }; SmoothScroll.prototype = { defaults: { easing: 'swing', speed: 'normal', fixedHeader: false }, init: function(options) { this.options = $.extend({}, this.defaults, options); this.fixedHeaderHeight = this.options.fixedHeader ? $(this.options.fixedHeader).outerHeight() : 0; this.initializeEvent(); }, initializeEvent: function() { var _this = this; _this.$trigger.on('click', $.proxy(_this.clickHandler, _this)); }, clickHandler: function() { var _this = this, href = _this.getHref(), scrollPos; if (href === '#') { scrollPos = 0; } else { scrollPos = $(href).offset().top - _this.fixedHeaderHeight; } $('html, body').animate( {scrollTop: scrollPos}, _this.options.speed, _this.options.easing ); return false; }, getHref: function () { var _this = this; return _this.$trigger.attr('href') || _this.$trigger.data('href'); } }; $.fn.SmoothScroll = function(options) { return this.each(function() { new SmoothScroll(this, options); }); }; $(function() { $('a[href^="#"]').not('a[href="#"]').SmoothScroll({ fixedHeader: '.header' }); var $navToggle = $('.js-nav-toggle'), $nav = $('.js-nav'), $navItem = $nav.find('a'), $window = $(window), openClass = 'is-opened'; $navToggle.on('click', function(e) { e.preventDefault(); if ($(this).hasClass(openClass)) { $nav.fadeOut().removeClass(openClass); $(this).removeClass(openClass); } else { $nav.fadeIn().addClass(openClass); $(this).addClass(openClass); } }); $navItem.on('click', function(e) { $nav.fadeOut().removeClass(openClass); $navToggle.removeClass(openClass); }); $window.on('scroll', function(e) { var winScroll = $window.scrollTop(), header = $('.header'), headerHeight = $('.header').outerHeight(), pagetop = $('.pagetop'), fixedClass = 'is-fixed'; if (winScroll > headerHeight) { header.addClass(fixedClass); } else { header.removeClass(fixedClass); } if (winScroll > 100) { pagetop.fadeIn(); } else { pagetop.fadeOut(); } }); }); } )( jQuery, window, document );