-
Notifications
You must be signed in to change notification settings - Fork 0
/
blog.js
113 lines (90 loc) · 2.99 KB
/
blog.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//=======Filtering (inspiration from w3schools: https://www.w3schools.com/howto/howto_js_filter_elements.asp)=========
filterSelection('all');
function filterSelection(c){
var x,i;
x=document.getElementsByClassName("filterDiv");
for(i=0;i<x.length;i++){
RemoveClass(x[i], "show");
if(x[i].className.indexOf(c)>-1) AddClass(x[i], "show");
}
}
//=======Showing filtered elements=======
function AddClass(element,name){
var i, arr1, arr2;
arr1 = element.className.split(" ");
arr2 = name.split(" ");
for (i = 0; i < arr2.length; i++) {
if (arr1.indexOf(arr2[i]) == -1) {
element.className += " " + arr2[i];
}
}
}
//=======Hiding elements that are not selected=======
function RemoveClass(element, name) {
var i, arr1, arr2;
arr1 = element.className.split(" ");
arr2 = name.split(" ");
for (i = 0; i < arr2.length; i++) {
while (arr1.indexOf(arr2[i]) > -1) {
arr1.splice(arr1.indexOf(arr2[i]), 1);
}
}
element.className = arr1.join(" ");
}
// =======Add active class to the current control button (highlight it)========
var btnContainer = document.getElementsByClassName("filtercontainer");
var btns = document.getElementsByClassName("btn");
for (var i = 0; i < btns.length; i++) {
btns[i].addEventListener("click", function() {
var current = document.getElementsByClassName("active");
current[0].className = current[0].className.replace(" active", "");
this.className += " active";
});
}
//scroll navbar
window.addEventListener("scroll", function(){
var nav =this.document.getElementById("navbar");
nav.classList.toggle("sticky",window.scrollY > 0);
});
window.addEventListener("scroll", function(){
var nav2 =this.document.getElementById("navbar2");
nav2.classList.toggle("sticky",window.scrollY > 0);
});
//=======part of the code for the searchbar====
function myFunction(){
let input=document.getElementById('searchbar').value;
input=input.toLowerCase();
let x =document.getElementsByClassName('filterDiv');
let y=document.getElementsByClassName('title');
for (i = 0; i < x.length; i++) {
if (!y[i].innerHTML.toLowerCase().includes(input)) {
RemoveClass(x[i], "show");
}
else {
AddClass(x[i], "show");
}
}
}
//responsive nav burger bar
const navSlide = () =>{
const burger =document.querySelector('#rgt2')
const navb =document.querySelector('.nav-links');
const navLinks=document.querySelectorAll('.nav-links li');
//toggle nav
burger.addEventListener('click', () => {
navb.classList.toggle('nav-active');
//burger animation
burger.classList.toggle('toggle');
//animate links
navLinks.forEach((link,index) =>{
if(link.style.animation)
{
link.style.animation="";
}
else{
link.style.animation= `navLinkFade 0.5s ease forwards ${index/7+ 0.3}s`;
}
})
});
};
navSlide();