我是靠谱客的博主 虚拟鞋垫,这篇文章主要介绍jQuery实现的简单图片轮播效果完整示例,现在分享给大家,希望可以做个参考。

本文实例讲述了jQuery实现的简单图片轮播效果。分享给大家供大家参考,具体如下:

先来看看运行效果:

具体代码如下:

复制代码
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
<!DOCTYPE html> <html> <head> <title>jquery实现图片轮播效果</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <style> #lunbo{width: 600px;height: 300px;margin: 0 auto;overflow: hidden;} #pics{width: 600px;height: 300px;cursor: pointer;position: relative;} ul li{width: 600px;height: 300px;list-style: none;position: absolute;top: 0;left: 0;display: none;} img{width: 600px;height: 300px;} </style> </head> <body> <div id="lunbo"> <ul id="pics"> <li style="display: inline;"><img src="http://www.uoften.com/uploadfile/2018/0411/20180411033739776.jpg"></li> <li><img src="http://www.uoften.com/uploadfile/2018/0411/20180411033739405.jpg"></li> <li><img src="http://www.uoften.com/uploadfile/2018/0411/20180411033740217.jpg"></li> <li><img src="http://www.uoften.com/uploadfile/2018/0411/20180411033740473.jpg"></li> <li><img src="http://www.uoften.com/uploadfile/2018/0411/20180411033749552.jpg"></li> </ul> </div> <script type="text/javascript"> $(document).ready(function(){ var oLi = $("li"); var liWidth = oLi.eq(0).width(); var liHeight = oLi.eq(0).height(); //每隔3秒进行轮播 var timer = setInterval(change,3000); //鼠标放置在图片上时停止轮播,移开时继续轮播 $("div").mouseover ( function(){ clearInterval(timer); }) $("div").mouseout (function(){ timer = setInterval(change,3000); }) //轮播函数 var prevIndex = 0,nextIndex = 1; function change(){ if (prevIndex == oLi.length-1 ) {//若当前图片是最后一张图片,下一张出现首张图片 nextIndex = 0; } var n = Math.floor(Math.random()*3); if (n == 0) { fade(prevIndex,nextIndex); } else if (n== 2) { slide(prevIndex,nextIndex); } else{ grap(prevIndex,nextIndex); } prevIndex = nextIndex; nextIndex ++; } //淡入淡出效果, function fade(prevIndex,nextIndex){ //传入当前显示图片以及下一张图片的索引 oLi.eq(prevIndex).fadeOut(1000); oLi.eq(nextIndex).fadeIn(1000); } //向左右滑动效果 function slide(prevIndex,nextIndex){ var rand = Math.floor(Math.random()*2); oLi.eq(nextIndex).show(); oLi.eq(nextIndex).css("z-index","-1"); if (rand) { //向左滑动 oLi.eq(prevIndex).animate({left: -liWidth},1000,fun); } else{ oLi.eq(prevIndex).animate({left: liWidth},1000,fun); } function fun(){ oLi.eq(prevIndex).css({"left":"0","display":"none"}); oLi.eq(nextIndex).css("z-index","1"); } } //收缩效果 function grap(prevIndex,nextIndex){ var rand = Math.floor(Math.random()*4); oLi.eq(nextIndex).show(); oLi.eq(nextIndex).css("z-index","-1"); switch (rand){ case 0://向左上角滑动 oLi.eq(prevIndex).animate({left: -liWidth,top: -liHeight},1000,function(){ oLi.eq(prevIndex).css({"left":"0","top": "0","display":"none"}); oLi.eq(nextIndex).css("z-index","1"); });break; case 1://向右上角滑动 oLi.eq(prevIndex).animate({left: liWidth,top: -liHeight},1000,function(){ oLi.eq(prevIndex).css({"left":'0',"top":"0","display":"none"}); oLi.eq(nextIndex).css("z-index","1"); });break; case 2://向右下角滑动 oLi.eq(prevIndex).animate({left: liWidth,top: liHeight},1000,function(){ oLi.eq(prevIndex).css({"left":'0',"top":"0","display":"none"}); oLi.eq(nextIndex).css("z-index","1"); });break; case 3://向左下角滑动 oLi.eq(prevIndex).animate({left: -liWidth,top: liHeight},1000,function(){ oLi.eq(prevIndex).css({"left":'0',"top":"0","display":"none"}); oLi.eq(nextIndex).css("z-index","1"); });break; default:break; } } }); </script> </body> </html>

更多关于jQuery相关内容感兴趣的读者可查看本站专题:《jQuery图片操作技巧大全》、《jQuery切换特效与技巧总结》、《jQuery拖拽特效与技巧总结》、《jQuery扩展技巧总结》、《jQuery常见经典特效汇总》、《jquery选择器用法总结》及《jQuery常用插件及用法总结》

希望本文所述对大家jQuery程序设计有所帮助。

最后

以上就是虚拟鞋垫最近收集整理的关于jQuery实现的简单图片轮播效果完整示例的全部内容,更多相关jQuery实现内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(114)

评论列表共有 0 条评论

立即
投稿
返回
顶部