移动端通栏滑动切换tab

直接上代码
注:主要展示功能,样式随意更改

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
  <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.tabScroll{
width: 100%;
white-space: nowrap;
overflow: hidden;
}
.tabsBox{
padding: 2px 20px;
white-space: nowrap;
overflow: hidden;
overflow-x: auto;
}
.tabsBox span{
display: inline-block;
width: 54px;
height: 25px;
line-height: 25px;
text-align: center;
background: #ff710d;
border-radius: 12.5px;
margin-right: 8px;
font-size: 12px;
color: #fff;
opacity: 0.4;
vertical-align: middle;
}
.tabsBox span.active{
background: #ff7214;
color: #fff;
opacity: 1;
}
.tabsContent{
white-space: nowrap;
overflow: hidden;
overflow-x: auto;
display: flex;
padding-left: 20px;
}
ul{
flex-shrink: 0;
overflow-x: auto;
display: flex;
padding: 12px 0 16px;
list-style: none;
display: -webkit-flex;
display: -webkit-box;
}
ul li{
flex-shrink: 0;
width: 107px;
height: 102px;
background: #fff;
border: 1px solid #eee;
box-shadow: 0 2px 4px 0 rgba(0,0,0,0.05);
border-radius: 6px;
margin-right: 8px;
}
</style>
</head>
<body>
<div id="app">
<div class="tabScroll">
<div class="tabsBox">
<span v-for="x in 5" :key="x" :class="{active: activeValue === x}" @click="tabsChange(x)">
tab_{{x}}
</span>
</div>
<div class="tabsContent" ref="tabsContent">
<ul id="ul1">
<li>1-1</li>
<li>1-2</li>
<li>1-3</li>
<li>1-4</li>
<li>1-5</li>
</ul>
<ul id="ul2">
<li>2-1</li>
<li>2-2</li>
</ul>
<ul id="ul3">
<li>3-1</li>
<li>3-2</li>
<li>3-3</li>
<li>3-4</li>
<li>3-5</li>
</ul>
<ul id="ul4">
<li>4-1</li>
</ul>
<ul id="ul5">
<li>5-1</li>
<li>5-2</li>
<li>5-3</li>
<li>5-4</li>
<li>5-5</li>
</ul>
</div>
</div>
</div>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script>
new Vue({
el: '#app',
data: function() {
return {
activeValue: 1,
scrollLeftValue: ''
}
},
mounted:function() {
setTimeout(() => {
this.scrollLeftValue = this.$refs.tabsContent.scrollLeft;
this.$refs.tabsContent.addEventListener("scroll", this.handleScroll, true)
}, 0)
},
methods: {
handleScroll() {
const a = this.$refs.tabsContent.scrollLeft;
const b = this.$refs.tabsContent.clientWidth;
const ul1 = document.getElementById("ul1").offsetLeft;
const ul2 = document.getElementById("ul2").offsetLeft;
const ul3 = document.getElementById("ul3").offsetLeft;
const ul4 = document.getElementById("ul4").offsetLeft;
const ul5 = document.getElementById("ul5").offsetLeft;
const arr = [ul1,ul2,ul3,ul4,ul5];
const arrLen = arr.length;
for(let i = 0; i < arrLen; i++){
if(arr[i] >= a && arr[i]< a+b){
this.activeValue = i+1;
}
}
},
tabsChange(row) {
this.activeValue = row;
document.getElementById("ul"+this.activeValue).scrollIntoView();
}
}
})
</script>
</body>
</html>