-
Notifications
You must be signed in to change notification settings - Fork 0
/
TableScrollWithDots.html
174 lines (153 loc) · 4.8 KB
/
TableScrollWithDots.html
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<style>
.wrapper {
scroll-behavior: smooth;
overflow-x: auto;
overflow-y: hidden;
/* height: 200px; */
}
.table {
width: 1000px;
border-collapse: collapse;
}
.table th,
.table td {
padding: 10px;
border: 1px solid black;
}
.scroll-indicator {
position: fixed;
top: 0;
z-index: 1;
width: 100%;
height: 6px;
background-color: #f1f1f1;
}
.scroll-indicator>div {
width: 1%;
height: 100%;
background-color: #4caf50;
}
.dot {
display: inline-block;
width: 20px;
height: 20px;
border-radius: 50%;
margin: 10px;
cursor: pointer;
background-color: #cccc;
}
.dot.active {
background-color: #4caf50;
}
</style>
<div class="scroll-indicator">
<div></div>
</div>
<div class="wrapper">
<table class="table">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Points</th>
</tr>
</thead>
<tbody id="table-body">
<tr id="row-1">
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr id="row-2">
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr id="row-3">
<td>Adam</td>
<td>Johnson</td>
<td>67</td>
</tr>
<tr id="row-4">
<td>Bob</td>
<td>Brown</td>
<td>80</td>
</tr>
<tr id="row-5">
<td>Alice</td>
<td>Green</td>
<td>75</td>
</tr>
<tr id="row-6">
<td>David</td>
<td>Miller</td>
<td>88</td>
</tr>
<tr id="row-7">
<td>Lisa</td>
<td>Parker</td>
<td>92</td>
</tr>
<tr id="row-8">
<td>Tom</td>
<td>Baker</td>
<td>86</td>
</tr>
<!-- Add more rows as needed -->
</tbody>
</table>
</div>
<div class="dots">
<!-- Add dots as needed -->
<span class="dot active" onclick="scrollTableWrapper(0);" data-target="row-1"></span>
<span class="dot" onclick="scrollTableWrapper(1);" data-target="row-2"></span>
<span class="dot" onclick="scrollTableWrapper(2);" data-target="row-3"></span>
</div>
<script>
let currentSettWidth = 0;
let currentDot = 1;
const container = document.querySelector(".wrapper");
const table = document.querySelector(".table");
const allDots =document.querySelectorAll(".dot");
const totalNOodDots = parseInt(allDots.length);
let widthtocalulatRightScrollPositin = 0;
let widthToAppltArray = [];
const tableWidth = table.scrollWidth;
const tableWidthToScroll = tableWidth/totalNOodDots;
const maxScroll = table.scrollWidth - container.offsetWidth;
let countofdots = 0;
allDots.forEach((element) => {
if(countofdots > 0){
widthtocalulatRightScrollPositin = widthtocalulatRightScrollPositin + tableWidthToScroll;
}
if(countofdots == (totalNOodDots - 1)){
widthtocalulatRightScrollPositin = maxScroll;
}
widthToAppltArray.push(widthtocalulatRightScrollPositin);
countofdots++;
})
const scrollTableWrapper = (dotNo) => {
var currentScroll = container.scrollLeft;
if (currentScroll >= Math.floor(widthToAppltArray[dotNo+1])) {
currentScroll = 0;
}
// Calculate the new scroll position
var newScroll = widthToAppltArray[dotNo];
// Limit the scroll position to the maximum value
if (newScroll > maxScroll) {
newScroll = maxScroll;
}
// Set the new scroll position
container.scrollLeft = newScroll;
}
</script>
</body>
</html>