$(document).ready(function() { class stickynavigation { constructor() { this.currentid = null; this.currenttab = null; this.tabcontainerheight = 100; let self = this; $('.tb_a').click(function() { self.ontabclick(event, $(this)); }); $(window).scroll(() => { this.onscroll(); }); $(window).resize(() => { this.onresize(); }); } ontabclick(event, element) { event.preventdefault(); let scrolltop = $(element.attr('href')).offset().top - this.tabcontainerheight + 1; $('html, body').animate({ scrolltop: scrolltop }, 600); } onscroll() { this.checktabcontainerposition(); this.findcurrenttabselector(); } onresize() { if(this.currentid) { this.setslidercss(); } } checktabcontainerposition() { let offset = $('.tb_nav').offset().top + $('.tb_nav').height() - this.tabcontainerheight; if($(window).scrolltop() > offset) { $('.tb_main').addclass('tb_main_top'); } else { $('.tb_main').removeclass('tb_main_top'); } } findcurrenttabselector(element) { let newcurrentid; let newcurrenttab; let self = this; $('.tb_a').each(function() { let id = $(this).attr('href'); let offsettop = $(id).offset().top - self.tabcontainerheight; let offsetbottom = $(id).offset().top + $(id).height() - self.tabcontainerheight; if($(window).scrolltop() > offsettop && $(window).scrolltop() < offsetbottom) { newcurrentid = id; newcurrenttab = $(this); } }); if(this.currentid != newcurrentid || this.currentid === null) { this.currentid = newcurrentid; this.currenttab = newcurrenttab; this.setslidercss(); } } setslidercss() { let width = 0; let left = 0; if(this.currenttab) { width = this.currenttab.css('width'); left = this.currenttab.offset().left; } $('.tb_line').css('width', width); $('.tb_line').css('left', left); } } new stickynavigation(); $('header .mbtn').click(function(event) { $('body').toggleclass('on-menu'); return false; }); })