I’m a beginner to HTML and JavaScript.
I’m building a tabs bar, in which I want to have the option to scroll it horizontally, not with a traditional browser scroll, but with arrow buttons that I’ve created.
Here is how my tabs bar looks like:
This is the best I’ve managed to do:
JavaScript
x
12
12
1
function clickLeft(){
2
arrowLeft.style.color="white";
3
setTimeout(function(){
4
arrowLeft.style.color="black";
5
},420);
6
}
7
function clickRight(){
8
arrowRight.style.color="white";
9
setTimeout(function(){
10
arrowRight.style.color="black";
11
},420);
12
}
JavaScript
1
63
63
1
#outer_container{
2
margin: auto;
3
}
4
#tabs_container{
5
display: flex;
6
overflow-x: auto;
7
8
left: 0;
9
right: 0;
10
margin: auto;
11
margin-top: 10px;
12
width: 60vh;
13
height: 70px;
14
border: 2px solid black;
15
border-bottom: 0;
16
border-radius: 10px;
17
padding: 4px;
18
}
19
#inner_wrap{
20
display: flex;
21
border-bottom: 2px solid black;
22
height: 50px;
23
}
24
#inner_wrap div{
25
text-align: center;
26
background-color: gray;
27
padding: 10px;
28
height: 20px;
29
border-radius: 5px;
30
margin: 2px;
31
width: max-content;
32
}
33
#tabs_container::-webkit-scrollbar{
34
width: 0;
35
}
36
#tabs_container::-webkit-scrollbar-track {
37
margin-top: 20px;
38
width: 20px;
39
padding: 20px;
40
-webkit-box-shadow:
41
inset 0 0 30px rgba(0, 0, 0, 0);
42
border-radius: 10px;
43
background-color: transparent;
44
}
45
#tabs_container::-webkit-scrollbar-thumb {
46
border-radius: 10px;
47
-webkit-box-shadow: inset 0 0 8px rgba(0, 0, 0, .3);
48
background-color: #666666;
49
}
50
#icon_tab{
51
display: inline-block;
52
background-color: none;
53
border:0;
54
color:white;
55
float: right;
56
width: 20px;
57
margin:5px;
58
}
59
.arrow{
60
font-size: 34px;
61
margin: 15px;
62
transition: color 0.4s;
63
}
JavaScript
1
57
57
1
<div id="main_container">
2
<table id=outer_container>
3
<tr>
4
<td>
5
<div>
6
<i class="arrow fas fa-arrow-circle-left" onclick="clickLeft()"></i>
7
</div>
8
</td>()
9
10
<td>
11
<div id="tabs_container">
12
<div id=inner_wrap>
13
<div>
14
geisha ch 1
15
</div>
16
<div>
17
geisha ch 2
18
</div>
19
<div>
20
geisha ch 3
21
</div>
22
<div>
23
work
24
</div>
25
<div>
26
hobby
27
</div>
28
<div>
29
music
30
</div>
31
<div>
32
movie
33
</div>
34
<div>
35
book1
36
</div>
37
<div>
38
book2
39
</div>
40
<div>
41
game
42
</div>
43
44
</div>
45
<div id=icon_tab>
46
<i class=" fa fa-plus-circle "aria-hidden="true"></i>
47
</div>
48
</div>
49
</td>
50
<td>
51
<div>
52
<i class="arrow fas fa-arrow-circle-right" onclick="clickRight()"></i>
53
</div>
54
</td>
55
</tr>
56
</table>
57
</div>
I manage to go to the JavaScript function, but have no idea how to scroll horizontally by JS code. Also I would like to hide the OOTB scroll.
I’ve also created a fiddle: https://jsfiddle.net/b40c19h6/1/
Advertisement
Answer
Use overflow-x: hidden
to hide the scrollbar and you can use the scrollLeft
or scrollBy
function on your tabs element to move the content.
Here how you can do it:
JavaScript
1
18
18
1
const arrowLeft = document.getElementsByClassName('arrow')[0];
2
const arrowRight = document.getElementsByClassName('arrow')[1];
3
const tabs = document.getElementById('tabs_container');
4
console.log("here")
5
function clickLeft(){
6
arrowLeft.style.color="white";
7
setTimeout(function(){
8
arrowLeft.style.color="black";
9
},420);
10
tabs.scrollLeft -= 30;
11
}
12
function clickRight(){
13
arrowRight.style.color="white";
14
setTimeout(function(){
15
arrowRight.style.color="black";
16
},420);
17
tabs.scrollLeft += 30;
18
}
JavaScript
1
78
78
1
body{
2
height:100vh;
3
width:100%;
4
margin: 0;
5
}
6
7
#main_container{
8
background-color: #3f51b5;
9
height:100%;
10
}
11
#outer_container{
12
margin: auto;
13
}
14
#tabs_container{
15
display: flex;
16
overflow-x: auto;
17
18
left: 0;
19
right: 0;
20
margin: auto;
21
margin-top: 10px;
22
width: 60vh;
23
height: 70px;
24
border: 2px solid black;
25
border-bottom: 0;
26
border-radius: 10px;
27
padding: 4px;
28
}
29
#inner_wrap{
30
display: flex;
31
border-bottom: 2px solid black;
32
height: 50px;
33
}
34
#inner_wrap div{
35
text-align: center;
36
background-color: gray;
37
padding: 10px;
38
height: 20px;
39
border-radius: 5px;
40
margin: 2px;
41
width: max-content;
42
}
43
#tabs_container{
44
overflow-x: hidden;
45
}
46
47
48
#tabs_container::-webkit-scrollbar{
49
width: 0;
50
}
51
#tabs_container::-webkit-scrollbar-track {
52
margin-top: 20px;
53
width: 20px;
54
padding: 20px;
55
-webkit-box-shadow:
56
inset 0 0 30px rgba(0, 0, 0, 0);
57
border-radius: 10px;
58
background-color: transparent;
59
}
60
#tabs_container::-webkit-scrollbar-thumb {
61
border-radius: 10px;
62
-webkit-box-shadow: inset 0 0 8px rgba(0, 0, 0, .3);
63
background-color: #666666;
64
}
65
#icon_tab{
66
display: inline-block;
67
background-color: none;
68
border:0;
69
color:white;
70
float: right;
71
width: 20px;
72
margin:5px;
73
}
74
.arrow{
75
font-size: 34px;
76
margin: 15px;
77
transition: color 0.4s;
78
}
JavaScript
1
68
68
1
<!DOCTYPE html>
2
<html>
3
<head>
4
<title>vacabulary</title>
5
<link rel="stylesheet" href="index.css">
6
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.12.1/css/all.css" crossorigin="anonymous">
7
</head>
8
<body>
9
<div id="main_container">
10
<table id=outer_container>
11
<tr>
12
<td>
13
<div>
14
<i class="arrow fas fa-arrow-circle-left" onclick="clickLeft()"></i>
15
</div>
16
</td>()
17
18
<td>
19
<div id="tabs_container">
20
<div id=inner_wrap>
21
<div>
22
geisha ch 1
23
</div>
24
<div>
25
geisha ch 2
26
</div>
27
<div>
28
geisha ch 3
29
</div>
30
<div>
31
work
32
</div>
33
<div>
34
hobby
35
</div>
36
<div>
37
music
38
</div>
39
<div>
40
movie
41
</div>
42
<div>
43
book1
44
</div>
45
<div>
46
book2
47
</div>
48
<div>
49
game
50
</div>
51
52
</div>
53
<div id=icon_tab>
54
<i class=" fa fa-plus-circle "aria-hidden="true"></i>
55
</div>
56
</div>
57
</td>
58
<td>
59
<div>
60
<i class="arrow fas fa-arrow-circle-right" onclick="clickRight()"></i>
61
</div>
62
</td>
63
</tr>
64
</table>
65
</div>
66
<script src="main.js"></script>
67
</body>
68
</html>