导航菜单鼠标移出一直有黑色背景

导航菜单鼠标移出一直有黑色背景

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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>综合实例</title>
    <link type="text/css" rel="styleSheet"  href="css/css.css" />
 
</head>
<body>
    <div class="main" id="main">
 
        <div class="menu-box"></div><!--导航栏设置透明度,在里面写东西也会透明,另起div-->
        <!--主菜单-->
 <div class="menu-content" id="menu-content">
            <div class="menu-item">
                <a href="">
                    <span>手机、配件</span>
                    <i>&#xe665;</i>
                </a>
            </div>
            <div class="menu-item">
                <a href="">
                    <span>电脑</span>
                    <i>&#xe665;</i>
                </a>
            </div>
            <div class="menu-item">
                <a href="">
                    <span>家用电器</span>
                    <i>&#xe665;</i>
                </a>
            </div>
            <div class="menu-item">
                <a href="">
                    <span>家具</span>
                    <i>&#xe665;</i>
                </a>
            </div>
        </div>
 <!--子菜单-->
 <div class="sub-menu hide" id="sub-menu">
 <!--手机、配件-->
 <div class="inner-box">
                <div class="sub-inner-box">
 <!--标题-->
 <div class="title">手机、配件</div>
                    <div class="sub-row">
                        <span class="bold mr10">手机通讯:</span>
                        <a href="">手机</a>
                        <span class="mr10 ml10">/</span>
                        <a href="">手机维修</a>
                        <span class="mr10 ml10">/</span>
                        <a href="">以旧换新</a>
                    </div>
                    <div class="sub-row">
                        <span class="bold mr10">手机配件:</span>
                        <a href="">手机壳</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">手机存储卡</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">数据线</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">充电器</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">电池</a>
                    </div>
                    <div class="sub-row">
                        <span class="bold mr10">运营商:</span>
                        <a href="">中国联通</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">中国移动</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">中国电信</a>
                    </div>
                    <div class="sub-row">
                        <span class="bold mr10">智能设备:</span>
                        <a href="">智能手环</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">智能家居</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">智能手表</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">其他配件</a>
                    </div>
                    <div class="sub-row">
                        <span class="bold mr10">娱乐:</span>
                        <a href="">耳机</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">音响</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">收音机</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">麦克风</a>
                    </div>
                </div>
            </div>
 <!--电脑-->
 <div class="inner-box">
                <div class="sub-inner-box">
                    <div class="title">电脑</div>
                    <div class="sub-row">
                        <span class="bold mr10">电脑:</span>
                        <a href="">笔记本</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">平板</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">一体机</a>
                    </div>
                    <div class="sub-row">
                        <span class="bold mr10">电脑配件:</span>
                        <a href="">显示器</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">CPU</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">主板</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">硬盘</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">电源</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">显卡</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">其他配件</a>
                    </div>
                    <div class="sub-row">
                        <span class="bold mr10">游戏设备:</span>
                        <a href="">游戏机</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">耳机</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">游戏软件</a>
                    </div>
                    <div class="sub-row">
                        <span class="bold mr10">网络产品:</span>
                        <a href="">路由器</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">网络机顶盒</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">交换机</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">存储卡</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">网卡</a>
                    </div>
                    <div class="sub-row">
                        <span class="bold mr10">外部产品:</span>
                        <a href="">鼠标</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">键盘</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">U盘</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">移动硬盘</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">鼠标垫</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">电脑清洁</a>
                    </div>
                </div>
            </div>
 <!--家用电器-->
 <div class="inner-box">
                <div class="sub-inner-box">
                    <div class="title">家用电器</div>
                    <div class="sub-row">
                        <span class="bold mr10">电视:</span>
                        <a href="">国产品牌</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">韩国品牌</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">欧美品牌</a>
                    </div>
                    <div class="sub-row">
                        <span class="bold mr10">空调:</span>
                        <a href="">显示器</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">柜式</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">中央</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">壁挂式</a>
                    </div>
                    <div class="sub-row">
                        <span class="bold mr10">冰箱:</span>
                        <a href="">多门</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">对开门</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">三门</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">双门</a>
                    </div>
                    <div class="sub-row">
                        <span class="bold mr10">洗衣机:</span>
                        <a href="">滚筒式洗衣机</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">迷你洗衣机</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">洗烘一体机</a>
                    </div>
                    <div class="sub-row">
                        <span class="bold mr10">厨房电器:</span>
                        <a href="">油烟机</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">洗碗机</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">燃气灶</a>
                    </div>
                </div>
            </div>
 <!--家具-->
 <div class="inner-box">
                <div class="sub-inner-box">
                    <div class="title">家具</div>
                    <div class="sub-row">
                        <span class="bold mr10">家纺:</span>
                        <a href="">被子</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">枕头</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">四件套</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">床垫</a>
                    </div>
                    <div class="sub-row">
                        <span class="bold mr10">灯具:</span>
                        <a href="">台灯</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">顶灯</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">节能灯</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">应急灯</a>
                    </div>
                    <div class="sub-row">
                        <span class="bold mr10">厨具:</span>
                        <a href="">烹饪锅具</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">餐具</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">菜板刀具</a>
                    </div>
                    <div class="sub-row">
                        <span class="bold mr10">家装:</span>
                        <a href="">地毯</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">沙发垫套</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">装饰字画</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">照片墙</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">窗帘</a>
                    </div>
                    <div class="sub-row">
                        <span class="bold mr10">生活日用:</span>
                        <a href="">收纳用品</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">浴室用品</a>
                        <span class="ml10 mr10">/</span>
                        <a href="">雨伞雨衣</a>
                    </div>
                </div>
            </div>
        </div>
 <!--图片轮播-->
 <div class="banner" id="banner">
            <a href="">
                <div class="banner-slide slide1 slide-active"></div>
            </a>
            <a href="">
                <div class="banner-slide slide2"></div>
            </a>
            <a href="">
                <div class="banner-slide slide3"></div>
            </a>
        </div>
 
 <!--上一张、下一张按钮-->
 <a href="JavaScript:void(0)" class="button prev" id="prev"></a>   // href意思是a是不做任何超链接跳转
 <a href="JavaScript:void(0)" class="button next" id="next"></a>   // href意思是a是不做任何超链接跳转
 
 <!--圆点导航-->
 <div class="dots" id="dots">
            <span class="active"></span>
            <span></span>
            <span></span>
        </div>
    </div>
    <script src="js/js.js"></script>
</body>
</html>
 
/*通用设置*/
* {
    margin0;
    padding0;
}
body {
    font-family"微软雅黑";
    color#14191e;
    background#000000;
}
a:link,a:visited {
    text-decorationnone;
    color#333;
}
@font-face {
    font-family"iconfont";
    srcurl("../img/font/iconfont.eot");
    srcurl("../img/font/iconfont.eot"format("embedded-opentype"),
         url("../img/font/iconfont.woff"format("woff"),
         url("../img/font/iconfont.ttf"format("truetype"),
         url("../img/font/iconfont.svg#iconfog"format("svg");
}
ul {
    list-stylenone;
}
 
 
 
/*大父级框*/
.main {
    width1200px;
    height460px;
    margin30px auto;
    overflowhidden;
    positionrelative;
}
 
 
 
/*轮播图图片框*/
.banner {
    width1200px;
    height460px;
    positionrelative;
}
 
.banner-slide {
    width1200px;
    height460px;
    background-repeatno-repeat;
    positionabsolute;
    displaynone;
}
/*显示第一张图片*/
.slide-active {
    displayblock;
}
/*插入背景图片*/
.slide1 {
    background-imageurl("../img/bg1.jpg");
}
.slide2 {
    background-imageurl("../img/bg2.jpg");
}
.slide3 {
    background-imageurl("../img/bg3.jpg");
}
 
 
 
/*左右按钮区域*/
.button {
    positionabsolute;
    width40px;
    height80px;
    left244px;
    top50%;
    margin-top-40px;
    backgroundurl("../img/arrow.png"no-repeat center center;
}
.button:hover {
    background-color#333;
    opacity: 0.3;   /*透明度*/
    filter: alpha(opacity = 80);    /*浏览器兼容*/
}
.prev {
    transform: rotate(180deg);  /*deg是度数的意思;*/
}
.next {
    leftauto;
    right0;
}
 
 
 
/*圆点导航*/
.dots {
    positionabsolute;
    right40px;
    bottom24px;
    text-alignright;
}
.dots span {
    display: inline-block;
    width12px;
    height12px;
    line-height12px;
    border-radius: 50%;
    background-color: rgba(17,17,27,0.4);
    box-shadow: 0 0 0 2px rgba(255,255,255,0.8inset;    /*阴影的水平方向、阴影的垂直方向、阴影的距离、模糊程度、阴影的颜色、内置阴影*/
    margin-left8px;
    cursorpointer;
}
/*第一个小圆点、当前小圆点*/
.dots .active {
    box-shadow: 0 0 0 2px rgba(7,17,27,0.4inset;
    background#fff;
}
 
 
 
 
 
 
 
 
/*导航栏*/
/*大透明框*/
.menu-box {
    width244px;
    height460px;
    positionabsolute;
    left0;
    top0;
    background: rgba(717270.5);
    opacity: 0.5;
    z-index1;
}
/*文字部分大div*/
.menu-content {
    width244px;
    height454px;
    positionabsolute;
    left0;
    top0;
    z-index2;
    padding-top6px;
}
/*每一行文字部分*/
.menu-item {
    height64px;
    line-height66px;
    font-size16px;
    padding0 24px;
    positionrelative;
}
 
.menu-item a:link,.menu-item a:visited {
    color#fff;
}
 
.menu-item a {
    displayblock;
    border-bottom1px solid rgba(255,255,255,0.2);
    padding0 8px;
    height63px;
}
 
/*小图标*/
.menu-item i {
    positionabsolute;
    right32px;
    font-size24px;
    top0;
    font-family"iconfont";
    font-stylenormal;     /*i标签有自动倾斜(取消倾斜)*/
    font-weightnormal;    /*i标签有自动倾斜(取消倾斜)*/
    color: rgba(255,255,255,0.5);
}
 
 
 
 
/*子菜单*/
/*子菜单父盒子*/
.sub-menu {
    width730px;
    height460px;
    border1px solid #d9dde1;
    background#fff;
    positionabsolute;
    left244px;
    top0;
    z-index999;
    /*阴影      水平方向    垂直方向    阴影的距离   模糊度 颜色*/
    box-shadow: 0 4px 8px 0 rgba(0,0,0,0.3);
}
 
 
/*子菜单子盒子*/
/*手机、配件*/
.inner-box {
    width100%;
    height100%;
    backgroundurl("../img/fe.png");
    displaynone;
}
 
.sub-inner-box {
    width652px;
    margin-left40px;
    overflowhidden;
 
}
 
/*标题*/
.title {
    color#f01414;
    font-size16px;
    line-height16px;
    margin28px 0 30px 0;
    font-weightbold;
}
 
.sub-row {
    margin-bottom25px;
}
 
.bold {
    font-weightbold;
}
.mr10 {
    margin-right10px;
}
.ml10 {
    margin-left10px;
}
 
/*隐藏子菜单*/
.hide {
    displaynone;
}
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
// 封装一个代替getElementById()的方法
function byId(id) {
    // tupeof判断id是否为字符串,用三目运算返回
    return typeof(id) === "string"?document.getElementById(id):id;
}
 
// 设置一全局变量来获取当前的图片位置
var index = 0,
    timer = null,
    pics = byId("banner").getElementsByTagName("div"),  // 把这几个div放到数组当中
    dots = byId("dots").getElementsByTagName("span"),   // 小圆点
    prev = byId("prev"),
    next = byId("next"),
    len = pics.length,
    menu = byId("menu-content"),
    subMenu = byId("sub-menu"),
    innerBox = subMenu.getElementsByClassName("inner-box"),
    menuItems = menu.getElementsByClassName("menu-item");
 
 
function slideImg() {
    var main = byId("main");
    // 划过清除定时器,离开继续
    main.onmouseover = function () {
        // 划过清除定时器
        if (timer) clearInterval(timer);
    };
 
    // 划入一下开起切换
    main.onmouseout = function () {
        // setTimeout()超市定时器
        timer = setInterval(function () {
            index++;    // 必须在第一行,在后面判断
            if (index >= len) {
                index = 0;
            }
 
            // 切换图片
            changeImg();
        }, 3000);
    };
    // 自动在main上触发鼠标离开的事件
    main.onmouseout();
 
    // 遍历所有点击,且绑定点击事件,点击圆点切换图片
    for (var d = 0; d < len; d++) {
        // 给所有span添加一个id的属性,值为d,作为当前的span的索引
        dots[d].id = d;
        dots[d].onclick = function () {
            // 改变index为当前span的id值
            // alert(this.id); // 弹出当前id
            index = this.id;
            // 调用changeImg,实现切换图图片
            changeImg();
        }
    }
 
    // 下一张
    next.onclick = function () {
        index++;
        if (index >= len) {
            index = 0;
        }
        changeImg();
    }
    // 上一张
    prev.onclick = function () {
        index--;
        if (index < 0) {
            // 当index下标是第一张0的时候--进入判断,len长度-1是最后一张下标
            index = len-1;
        }
        changeImg();
    }
 
    // 导航菜单
    // 遍历主菜单,且绑定时间
    for (var m=0;m<menuItems.length;m++) {
        // 没每一个menu-item定义data-index属性,作为索引
        menuItems[m].setAttribute("data-index",m);
        menuItems[m].onmouseover = function () {
            subMenu.className = "sub-menu";
            var idx = this.getAttribute("data-index");
            // 遍历所有子菜单,将每一个都隐藏
            for (var j=0;j<innerBox.length;j++) {
                innerBox[j].style.display = 'none';
                menuItems[j].style.background = 'none';
            }
            menuItems[idx].style.background = 'rgba(0,0,0,0.1)';
            innerBox[idx].style.display = 'block';
        }
    }
 
    menu.onmouseout = function () {
        subMenu.className = "sub-menu hide";
    };
 
    subMenu.onmouseover = function () {
        this.className = "sub-menu";
    };
 
    subMenu.onmouseout = function () {
        this.className = "sub-menu hide";
    }
}
 
// 切换图片
function changeImg() {
    // 遍历banner下多有的div,将其隐藏
    // 遍历dots下所有的span,将div隐藏,将span清除类
    for (var i = 0; i < len; i++){
        pics[i].style.display = "none";
        dots[i].className = "";
    }
    // 根据index索引找到当前div,将其显示出来设为当前
    pics[index].style.display = 'block';
    // 根据index索引找到当前span,将其显示出来设为当前
    dots[index].className = "active";
}
 
slideImg();

http://img1.sycdn.imooc.com//climg/5c1cac8a0001c6d102650151.jpg

正在回答

登陆购买课程后可参与讨论,去登陆

2回答

你好同学,测试了你的代码,按照之前老师的回复,是可以达到你所描述的效果的,

http://img1.sycdn.imooc.com//climg/5c1ef3460001938c04560207.jpg

js中的代码如下,你可以将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
// 封装一个代替getElementById()的方法
function byId(id) {
    // tupeof判断id是否为字符串,用三目运算返回
    return typeof(id) === "string"?document.getElementById(id):id;
}
  
// 设置一全局变量来获取当前的图片位置
var index = 0,
    timer = null,
    pics = byId("banner").getElementsByTagName("div"),  // 把这几个div放到数组当中
    dots = byId("dots").getElementsByTagName("span"),   // 小圆点
    prev = byId("prev"),
    next = byId("next"),
    len = pics.length,
    menu = byId("menu-content"),
    subMenu = byId("sub-menu"),
    innerBox = subMenu.getElementsByClassName("inner-box"),
    menuItems = menu.getElementsByClassName("menu-item");
  
  
function slideImg() {
    var main = byId("main");
    // 划过清除定时器,离开继续
    main.onmouseover = function () {
        // 划过清除定时器
        if (timer) clearInterval(timer);
    };
  
    // 划入一下开起切换
    main.onmouseout = function () {
        // setTimeout()超市定时器
        timer = setInterval(function () {
            index++;    // 必须在第一行,在后面判断
            if (index >= len) {
                index = 0;
            }
  
            // 切换图片
            changeImg();
        }, 3000);
    };
    // 自动在main上触发鼠标离开的事件
    main.onmouseout();
  
    // 遍历所有点击,且绑定点击事件,点击圆点切换图片
    for (var d = 0; d < len; d++) {
        // 给所有span添加一个id的属性,值为d,作为当前的span的索引
        dots[d].id = d;
        dots[d].onclick = function () {
            // 改变index为当前span的id值
            // alert(this.id); // 弹出当前id
            index = this.id;
            // 调用changeImg,实现切换图图片
            changeImg();
        }
    }
  
    // 下一张
    next.onclick = function () {
        index++;
        if (index >= len) {
            index = 0;
        }
        changeImg();
    }
    // 上一张
    prev.onclick = function () {
        index--;
        if (index < 0) {
            // 当index下标是第一张0的时候--进入判断,len长度-1是最后一张下标
            index = len-1;
        }
        changeImg();
    }
  
    // 导航菜单
    // 遍历主菜单,且绑定时间
    for (var m=0;m<menuItems.length;m++) {
        // 没每一个menu-item定义data-index属性,作为索引
        menuItems[m].setAttribute("data-index",m);
        menuItems[m].onmouseover = function () {
            subMenu.className = "sub-menu";
            var idx = this.getAttribute("data-index");
            // 遍历所有子菜单,将每一个都隐藏
            for (var j=0;j<innerBox.length;j++) {
                innerBox[j].style.display = 'none';
                menuItems[j].style.background = 'none';
            }
            menuItems[idx].style.background = 'rgba(0,0,0,0.1)';
            innerBox[idx].style.display = 'block';
        }
    }
  
    // menu.onmouseout = function () {
    //     subMenu.className = "sub-menu hide";
    // };
    menu.onmouseout = function () {
        subMenu.className = "sub-menu hide";
        for(var  m=0;m<menuItems.length;m++){
           menuItems[m].style.background = 'none';
        }
          
    };
  
    subMenu.onmouseover = function () {
        this.className = "sub-menu";
    };
  
    subMenu.onmouseout = function () {
        this.className = "sub-menu hide";
    }
}
  
// 切换图片
function changeImg() {
    // 遍历banner下多有的div,将其隐藏
    // 遍历dots下所有的span,将div隐藏,将span清除类
    for (var i = 0; i < len; i++){
        pics[i].style.display = "none";
        dots[i].className = "";
    }
    // 根据index索引找到当前div,将其显示出来设为当前
    pics[index].style.display = 'block';
    // 根据index索引找到当前span,将其显示出来设为当前
    dots[index].className = "active";
}
  
slideImg();


好帮手慕阿满 2018-12-21 18:00:05

同学是想问鼠标离开子菜单后,子菜单还有透明度吗?可以在鼠标离开子菜单的函数中遍历子菜单,然后设置背景为none。如:

1
2
3
4
5
6
7
menu.onmouseout = function () {
        subMenu.className = "sub-menu hide";
        for(var  m=0;m<menuItems.length;m++){
           menuItems[m].style.background = 'none';
        }
         
    };

这样鼠标离开子菜单后,所有的样式都回到最初。

如果我的回答解决了你的疑惑,请采纳。祝:学习愉快!

  • 提问者 永夜将至 #1
    代码复制上去了 可是鼠标移动到子菜单 阴影也会消失啊 刚学js有点懵逼
    2018-12-21 23:53:35
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
从网页搭建入门Java Web2018版
  • 参与学习           人
  • 提交作业       1088    份
  • 解答问题       10204    个

如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!

了解课程
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码