-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.js
75 lines (67 loc) · 1.91 KB
/
function.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Code By Webdevtrick ( https://webdevtrick.com )
class StickyNavigation {
constructor() {
this.currentId = null;
this.currentTab = null;
this.tabContainerHeight = 70;
let self = this;
$('.navs').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 = $('.nav-items').offset().top + $('.nav-items').height() - this.tabContainerHeight;
if($(window).scrollTop() > offset) {
$('.items-container').addClass('items-container--top');
}
else {
$('.items-container').removeClass('items-container--top');
}
}
findCurrentTabSelector(element) {
let newCurrentId;
let newCurrentTab;
let self = this;
$('.navs').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;
}
$('.bottom-slider').css('width', width);
$('.bottom-slider').css('left', left);
}
}
new StickyNavigation();