-
Notifications
You must be signed in to change notification settings - Fork 1
/
tabs.js
91 lines (89 loc) · 2.37 KB
/
tabs.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
new Vue({
el: ".buttons",
data: () => ({
activeTab: 0,
menuItems: [{
title: "Top Ideas"
},
{
title: "Latest"
},
{
title: "Daily"
},
{
title: "Weekly"
}
,
{
title: "Monthly"
}
,
{
title: "All Time"
},
{
title: "It's happening !"
}
]
}),
mounted() {
// set the initial stripe
let strip = this.$el.querySelector(".buttons-strip");
let activeBtn = this.$el.querySelector('.button.active');
strip.style.left = this.actualPos(activeBtn, "left") + 'px'
strip.style.width = activeBtn.getBoundingClientRect().width.toFixed(2) + 'px'
},
methods: {
actualPos(k, type) {
let parent = this.$el.getBoundingClientRect()
let child = k.getBoundingClientRect()
return child[type] - parent[type]
},
onTabSelect(index, e) {
let payload = {
e,
oldIndex: this.activeTab,
newIndex: index
};
this.applyStrip(payload);
this.activeTab = index;
},
applyStrip(data) {
let direction = null;
let strip = this.$el.querySelector(".buttons-strip");
let {
e,
oldIndex,
newIndex
} = data;
if (newIndex > oldIndex) direction = "right"
else if (oldIndex > newIndex) direction = "left"
let targetUtils = {
width: e.target.getBoundingClientRect().width,
left: this.actualPos(e.target, "left")
};
let stripLeft = this.actualPos(strip, "left")
if (direction === "right") {
strip.style.width =
strip.getBoundingClientRect().width +
(targetUtils.left +
targetUtils.width -
(stripLeft + strip.getBoundingClientRect().width)) +
"px";
setTimeout(() => {
strip.style.left = targetUtils.left + "px";
strip.style.width = targetUtils.width + "px";
}, 300);
} else if (direction === "left") {
let hole;
hole = stripLeft - targetUtils.left;
strip.style.width = strip.getBoundingClientRect().width + hole + "px";
strip.style.left = targetUtils.left + "px";
setTimeout(() => {
strip.style.width = targetUtils.width + "px";
}, 300);
}
},
},
});