本文實例為大家分享了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
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
|
<!DOCTYPE html> < html lang = "en" > < head > < meta charset = "UTF-8" > < meta name = "viewport" content = "width=device-width, initial-scale=1.0" > < meta http-equiv = "X-UA-Compatible" content = "ie=edge" > < title >圖片切換</ title > < style > .picture { position: relative; width: 500px; height: 333px; margin: 0 auto; border: 2px solid rgb(231, 127, 217); overflow: hidden; } .radius { width: 100%; height: 10px; position: absolute; bottom: 30px; text-align: center; } .pg { //圖片上方頁碼 position: absolute; margin: 0; width: 100%; height: 20px; background-color: rgba(0, 0, 0, .4); text-align: center; font-size: 16px; font-weight: 600; color: #fff; } .title { position: absolute; width: 100%; bottom: 0px; text-align: center; font-size: 16px; font-weight: 600; color: rgb(21, 223, 72); } span { display: inline-block; border: 10px solid #fdfdfd; border-radius: 50%; } .active { border: 10px solid #656466; } /* 左右箭頭 */ .arrowhead-left, .arrowhead-right { position: absolute; width: 41px; height: 69px; font-size: 30px; line-height: 70px; text-align: center; color: #D6D8D4; background-color: rgba(0,0,0,.3); } .arrowhead-left { left: 0; top: 40%; } .arrowhead-right { right: 0; top: 40%; } </ style > </ head > < body > < div class = "picture" > <!-- 圖片頁碼 --> < p class = "pg" >封面</ p > < img src = "./image/d8.jpeg" alt = "" > <!-- 小圓點點 --> < p class = "radius" ></ p > <!-- 圖片的下面標題 --> < p class = "title" >標題</ p > <!-- 左右箭頭 --> < div class = "arrowhead-left" id = "al" > < </ div > < div class = "arrowhead-right" id = "ar" > > </ div > </ div > < script > var address = ["./image/d1.jpeg", "./image/d2.jpeg", "./image/d3.jpeg", "./image/d4.jpeg", "./image/d5.jpeg", "./image/d7.jpeg"]; // var imgs = document.getElementsByTagName("img"); var imgs = document.querySelector("img"); var len = address.length; var str = ""; var pp = document.getElementsByTagName("p");//獲取的是一個集合 // var pp = document.querySelector("p"); //獲取的是一個元素 var al = document.getElementById("al"); var ar = document.getElementById("ar"); //添加span標簽 for (i = 0; i < len ; i++) { str += ' <span></ span >' } console.log(str); console.log(pp); pp[1].innerHTML = str; var spans = pp[1].getElementsByTagName('span'); spans[0].className = 'active'; for (i = 0; i < len ; i++) { spans[i] .index = i; spans[i] .onmouseover = function () { //所有圓點的類為空 for ( i = 0 ; i < len; i++) { spans[i] .className = "" ; } this.className = 'active' ; //給點擊的span(圓點)添加類名 imgs.src = address [this.index]; pp[0].innerHTML = [this.index + 1] + "/6"; pp[2] .innerHTML = "風光" + [this.index + 1]; } } var n = 0 ; ar.onclick = function () { for ( i = 0 ; i < len; i++) { spans[i] .className = "" ; } spans[n] .className = "active" ; imgs.src = address [n]; pp[0].innerHTML = (n+1) + "/6"; pp[2] .innerHTML = "風光" +(n+1); if (n<5) { n++; } else { n = 0 ; } } al.onclick = function () { for ( i = 0 ; i < len; i++) { spans[i] .className = "" ; } spans[n] .className = "active" ; imgs.src = address [n]; pp[0].innerHTML = (n+1) + "/6"; pp[2] .innerHTML = "風光" +(n+1); if (n>0) { n--; } else {} n=(len-1); } } </ script > </ body > </ html > |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qq_38318589/article/details/99050117