header js
Fri Mar 29 2024 13:02:29 GMT+0000 (Coordinated Universal Time)
Saved by @divyasoni23 #javascript #jquery
import $ from 'jquery';
class Header {
constructor(element) {
this.$element = $(element);
this.$burger = this.$element.find('.header__burger');
this.$Nav = this.$element.find('.nav-main');
this.$hasSub = this.$element.find('.menu-item-has-children');
this.$menuArrow = this.$element.find('.dropdown-arv-cont');
this.$menuBg = this.$element.find('.header__menu-bg');
this.init();
}
// headerPadding() {
// if (!this.$element.hasClass('header--transperant')) {
// let hHeight = this.$element.height();
// $('.main-wrapper').css({ 'padding-top': hHeight });
// }
// }
init() {
// $(document).ready(() => {
// this.headerPadding();
// });
$(window).on('resize load', () => {
this.$element.addClass('loaded');
});
$(window).on('scroll', () => {
if ($(window).scrollTop() > 100) {
$('.header').addClass('sticky');
} else {
$('.header').removeClass('sticky');
}
});
$(document).on(
'click',
'.menu-item-has-children .dropdown-arv-cont',
function () {
$(this).closest('li').toggleClass('active');
},
);
this.$burger.on('click', (e) => {
e.preventDefault();
if ($(e.currentTarget).hasClass('opened')) {
$(e.currentTarget).removeClass('opened').attr('aria-expanded', 'false');
this.$Nav.slideUp(300).parent().removeClass('active');
$('.menu-item-has-children .dropdown-arv-cont').removeClass('active');
$('body').css('overflow-y', 'visible');
$('.dropdown-arv-cont')
.closest('li')
.children('.sub-menu')
.removeClass('active');
} else {
$(e.currentTarget).addClass('opened').attr('aria-expanded', 'true');
this.$Nav.slideDown(300).parent().toggleClass('active');
$('body').css('overflow-y', 'hidden');
}
});
this.$menuBg.on('click', (e) => {
e.preventDefault();
this.$burger.removeClass('opened').attr('aria-expanded', 'false');
this.$Nav.slideUp(300).parent().removeClass('active');
});
$(document).on('click', '.dropdown-arv-cont', function (e) {
e.preventDefault();
if ($(this).hasClass('active')) {
$(this).removeClass('active');
$(this).closest('li').children('.sub-menu').removeClass('active');
} else {
$(this).addClass('active');
$(this).closest('li').children('.sub-menu').addClass('active');
}
});
}
}
$('[data-header]').each((index, element) => new Header(element));



Comments