本文实例为大家分享了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
|
<!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 > .box { width: 500px; height: 300px; /* border: 1px solid #ccc; */ margin: 50px auto; } span { width: 80px; height: 30px; border: 1px solid #ccc; display: inline-block; text-align: center; line-height: 30px; } .body { margin-top: 5px; width: 450px; border: 1px solid #ccc; height: 250px; background: #eee; } .current { color: red; background: #555; } .body div { display: none; text-align: center; line-height: 250px; } .body .active { display: block } </ style > </ head > < body > < div class = "box" > < div class = "header" > < span class = "current" >关注</ span > < span >推荐</ span > < span >热点</ span > < span >本地</ span > < span >段子</ span > </ div > < div class = "body" > < div class = "active" >关注:早上吃什么?</ div > < div >推荐:中午吃什么?</ div > < div >热点:晚饭吃什么?</ div > < div >本地:宵夜吃什么?</ div > < div >段子:半夜吃什么?</ div > </ div > </ div > < script > var oSpan = document.querySelectorAll('span') var oDiv = document.querySelector('.body').children for (let i = 0; i < oSpan.length ; i++) { oSpan[i] .index = i console.log(oSpan[i].index); oSpan[i] .onclick = function () { for (let k = 0 ; k < oSpan.length; k++) { oSpan[k].classList.remove('current') } this.classList.add('current') console.log(oSpan[i].index); for (let k = 0 ; k < oSpan.length; k++) { oDiv[k].classList.remove('active') } oDiv[this.index].classList.add('active') } } </script> </ body > </ html > |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/Frank_yzb123123/article/details/110919016