jQuery实现轮播图(宽度全屏高度固定)
程序员文章站
2022-05-06 14:40:07
...
html部分<html>
<head></head>
<body>
<div class="cm-banner-list">
<ul id="cm_banner_list">
<!--图片宽度和高度在css中定为1920x300-->
<li><img src="cm-banner-01.png" /></li>
<li><img src="cm-banner-02.png" /></li>
<li><img src="cm-banner-03.png" /></li>
<li><img src="cm-banner-04.png" /></li>
</ul>
<ul class="cm-banner-num" id="cm_banner_num"></ul>
</div>
<div class="cm-banner-in w">
<img src="cm-arrow-left.png" id="cm_prev" class="cm-banner-arrow" />
<img src="cm-arrow-right.png" id="cm_next" class="cm-banner-arrow" />
</div>
</body>
</html>
2 margin: 0;
3 padding: 0;
4
}
5 .cm-banner {
6 width: 100%;
7 height: 300px;
8 overflow: hidden;
9 cursor: pointer;
10 position: relative;
11
}
12 13 .cm-banner-in {
14 width: 1100px;
15 height: 300px;
16 position: absolute;
17 top: 0;
18 left: 50%;
19 margin-left: -550px;
20
}
21 22 #cm_banner_list li {
23 display: none;
24 position: absolute;
25 top: 0;
26 left: 0;
27
}
28 29 .cm-banner-num {
30 width: 100%;
31 position: absolute;
32 bottom: 0;
33 text-align: center;
34 z-index: 2;
/*如果没有在这里设置层次小圆点的点击效果无法触发*/
35
}
36 37 .cm-banner-num li {
38 width: 10px;
39 margin: 10px 3px;
40 height: 10px;
41 background-color: #fff;
42 border-radius: 5px;
43 -webkit-border-radius: 5px;
44 display: inline-block;
45 opacity: 0.7;
46
}
47 48 .cm-banner-num .active {
49 background-color: #3982de;
50
}
51 52 .cm-banner-arrow {
53 position: absolute;
54 top: 50%;
55 margin-top: -22px;
56 opacity: 0.5;
57 display: none;
58
}
59 60 #cm_prev {
61 left: 0;
62
}
63 64 #cm_next {
65 right: 0;
66
}
<head></head>
<body>
<div class="cm-banner-list">
<ul id="cm_banner_list">
<!--图片宽度和高度在css中定为1920x300-->
<li><img src="cm-banner-01.png" /></li>
<li><img src="cm-banner-02.png" /></li>
<li><img src="cm-banner-03.png" /></li>
<li><img src="cm-banner-04.png" /></li>
</ul>
<ul class="cm-banner-num" id="cm_banner_num"></ul>
</div>
<div class="cm-banner-in w">
<img src="cm-arrow-left.png" id="cm_prev" class="cm-banner-arrow" />
<img src="cm-arrow-right.png" id="cm_next" class="cm-banner-arrow" />
</div>
</body>
</html>
css部分
1 * {2 margin: 0;
3 padding: 0;
4
}
5 .cm-banner {
6 width: 100%;
7 height: 300px;
8 overflow: hidden;
9 cursor: pointer;
10 position: relative;
11
}
12 13 .cm-banner-in {
14 width: 1100px;
15 height: 300px;
16 position: absolute;
17 top: 0;
18 left: 50%;
19 margin-left: -550px;
20
}
21 22 #cm_banner_list li {
23 display: none;
24 position: absolute;
25 top: 0;
26 left: 0;
27
}
28 29 .cm-banner-num {
30 width: 100%;
31 position: absolute;
32 bottom: 0;
33 text-align: center;
34 z-index: 2;
/*如果没有在这里设置层次小圆点的点击效果无法触发*/
35
}
36 37 .cm-banner-num li {
38 width: 10px;
39 margin: 10px 3px;
40 height: 10px;
41 background-color: #fff;
42 border-radius: 5px;
43 -webkit-border-radius: 5px;
44 display: inline-block;
45 opacity: 0.7;
46
}
47 48 .cm-banner-num .active {
49 background-color: #3982de;
50
}
51 52 .cm-banner-arrow {
53 position: absolute;
54 top: 50%;
55 margin-top: -22px;
56 opacity: 0.5;
57 display: none;
58
}
59 60 #cm_prev {
61 left: 0;
62
}
63 64 #cm_next {
65 right: 0;
66
}
js部分
1 $(function(){ 2 //鼠标移入显示箭头按钮 3 $('.cm-banner').hover(function(){ 4 $('.cm-banner-arrow').show(); 5 clearInterval(cm_timer); 6 },function(){ 7 $('.cm-banner-arrow').hide(); 8 cm_timer = setInterval(function(){ 9 i++;10 if(i > cm_length - 1){11 i = 0;12 }13 $('#cm_banner_list li').eq(i).fadeIn(800).siblings().fadeOut(800);14 cm_toggle(i);15 },2500);16 });17 18 //鼠标移入箭头按钮高亮19 $('.cm-banner-arrow').hover(function(){20 $(this).css('opacity','1');21 },function(){22 $(this).css('opacity','0.5');23 });24 25 //初始化必要变量26 var i = 0;27 var cm_length = $('#cm_banner_list li').length;28 var cm_toggle = function(point){29 $('#cm_banner_num li').eq(point).addClass('active').siblings().removeClass('active');30 };31 32 //动态添加小圆点33 for(j = 0;j < cm_length;j++){34 $('#cm_banner_num').append('<li></li>');35 }36 37 //给第一个小圆点添加样式38 $('#cm_banner_num li').first().addClass('active');39 40 //给第一张图片添加样式41 $('#cm_banner_list li').first().css('display','block');42 43 //鼠标点击左箭头切换44 $('#cm_prev').click(function(){45 i--;46 if(i < 0){47 i = cm_length - 1;48 }49 $('#cm_banner_list li').eq(i).fadeIn(800).siblings().fadeOut(800);50 cm_toggle(i);51 });52 53 //鼠标点击右箭头切换54 $('#cm_next').click(function(){55 i++;56 if(i > cm_length - 1){57 i = 0;58 }59 $('#cm_banner_list li').eq(i).fadeIn(800).siblings().fadeOut(800);60 cm_toggle(i);61 });62 63 //鼠标点击圆点切换64 $('#cm_banner_num li').click(function(){65 var cm_index = $(this).index();66 $('#cm_banner_list li').eq(cm_index).fadeIn(800).siblings().fadeOut(800);67 i = cm_index;68 cm_toggle(cm_index);69 });70 71 //自动播放72 cm_timer = setInterval(function(){73 i++;74 if(i > cm_length - 1){75 i = 0;76 }77 $('#cm_banner_list li').eq(i).fadeIn(800).siblings().fadeOut(800);78 cm_toggle(i);79 },2500);80 });
以上就是jQuery实现轮播图(宽度全屏高度固定)的详细内容,更多请关注其它相关文章!