/* * Name : common.js * Version : 1.0 * Author : onepixel studio * Date : 2020-05-18 --------------------------------------------------- Table of contents --------------------------------------------------- 01. global variables 02. init 03. header init 04. sticky header 05. bootstrap file init 06. scroll event 07. contact form init --------------------------------------------------- */ 'use strict'; $(function () { // global variables var $screen_lg = 1200; var $screen_md = 992; var $screen_sm = 768; var $body = $('body'), $header = $('#header'), $currURL = $(location).attr('href'); var didScroll, lastScrollTop = 0, delta = 5, stickyPos = 100; // init header_init(); bs_file_init(); contact_form_init(); // page load complete function page_load_complete() { var $loader = $('#page-loader'); $loader.find('.loader-stage').fadeOut('slow', function() { setTimeout(function() { $loader.fadeOut('slow'); }, 100); }); } // header init function header_init() { var $header = $('#header'), $btn_toggle = $('#header .hamburger-button'), $backdrop = $('#header .mobile-nav .backdrop'); $('#header .nav-for-pc > ul > li, #header .mobile-nav .body > ul > li').each(function() { var $this = $(this); if ( $currURL.indexOf($(this).find('>a').attr('href')) > -1 ) { $(this).addClass('active open'); } $(this).find('.subnav li').each(function () { if ( $currURL.indexOf($(this).find('>a').attr('href')) > -1 ) { $(this).addClass('active'); $(this).parents('li').addClass('active open'); $(this).closest('ul').show(); } }); }); $('#header .mobile-nav .body > ul > li').each(function() { if ( $(this).find('.subnav').length ) $(this).addClass('has-child-items'); }); if ( $('#header .nav-for-pc > ul > li.active').length > 1 ) $('#header .nav-for-pc > ul > li:eq(0)').removeClass('active open'); if ( $('#header .mobile-nav .body > ul > li.active').length > 1 ) $('#header .mobile-nav .body > ul > li:eq(0)').removeClass('active open'); $('#header .nav-for-pc > ul > li > a').on('click', function(e) { var $this = $(this); if ( $this.siblings('.subnav').length ) { e.preventDefault(); navAccordionAction(); function navAccordionAction() { if ( !$this.parent().hasClass('open') ) { $('#header .nav-for-pc > ul > li').removeClass('open'); $('#header .nav-for-pc > ul > li > .subnav').stop().slideUp(300); $this.parent().addClass('open'); $this.siblings('.subnav').stop().slideDown(300); } else { $this.parent().removeClass('open'); $this.siblings('.subnav').stop().slideUp(300); } } } }); $btn_toggle.on('click', function() { $header.toggleClass('nav-open'); $backdrop.fadeToggle(400); }); $backdrop.on('click', function() { $header.removeClass('nav-open'); $(this).fadeOut(400); }); $('#header .mobile-nav .body > ul > li > a').on('click', function(e) { if ( $(this).siblings('.subnav').length ) { e.preventDefault(); if ( !$(this).parent().hasClass('open') ) { $('#header .mobile-nav .body > ul > li').removeClass('open'); $('#header .mobile-nav .body > ul > li .subnav').stop().slideUp(300); $(this).parent().addClass('open'); $(this).siblings('.subnav').stop().slideDown(300); } else { $(this).parent().removeClass('open'); $(this).siblings('.subnav').stop().slideUp(300); } } }); $('#header .mobile-nav .button-close').on('click', function(e) { $header.removeClass('nav-open'); $backdrop.fadeOut(400); }); } // sticky header function sticky_header() { var $header = $('#header'), $pos = 100, $st = $(window).scrollTop(); if ( $st >= $pos ) { $header.addClass('sticky'); } else { $header.removeClass('sticky'); } } setInterval(function() { if ( didScroll ) { hasScrolled(); didScroll = false; } }, 250); function hasScrolled() { var $st = $(window).scrollTop(), $header = $('#header'); if ( Math.abs(lastScrollTop - $st) <= delta ) return; if ( $st > lastScrollTop && $st > stickyPos ) { $header.removeClass('nav-down').addClass('nav-up'); } else { if ( $st + $(window).height() < $(document).height() ) { $header.removeClass('nav-up').addClass('nav-down'); } } lastScrollTop = $st; } // bootstrap file init function bs_file_init() { bsCustomFileInput.init(); } // scroll event $(window).on('scroll', function() { didScroll = true; sticky_header(); }); // contact form init function contact_form_init() { if ( $('textarea[rows=9]').length ) { var $text = $('textarea[rows=9]').val(); $('#privacy_term_context').val($text); } } // load event $(window).load(function() { page_load_complete(); }); });