jquery实现图片广告轮换效果

先看看效果:http://demo.vs2010.com/hotmarquee/

html代码如下:

1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2  <html xmlns="http://www.w3.org/1999/xhtml">
3  <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>hotmarquee</title>
6 <style type="text/css">
7 *
8 {
9 margin: 0;
10 padding: 0;
11 }
12 body
13 {
14 font: 12px;
15 padding-top: 50px;
16 padding-right: 200px;
17 padding-bottom: 100px;
18 padding-left: 200px;
19 }
20 ul
21 {
22 list-style: none;
23 }
24 img
25 {
26 padding: 2px;
27 border: 1px solid #eee;
28 }
29 a
30 {
31 outline: none;
32 }
33
34 #imgs
35 {
36 width: 410px;
37 margin-right: auto;
38 margin-left: auto;
39 }
40 .top, .btm
41 {
42 background: url(../images/sprite.gif) no-repeat;
43 overflow: hidden;
44 }
45 .top
46 {
47 background-position: 0 0;
48 height: 5px;
49 }
50 .btm
51 {
52 height: 7px;
53 }
54 .mid
55 {
56 width: 400px;
57 padding: 5px 7px 0;
58 border: 1px solid #999;
59 }
60 .mid ul
61 {
62 width: 400px;
63 height: 600px;
64 background: #fff;
65 position: relative;
66 overflow: hidden;
67 }
68 .mid ul li
69 {
70 width: 400px;
71 height: 600px;
72 position: absolute;
73 left: 490px;
74 top: 0;
75 }
76 .mid ul li.first
77 {
78 left: 0;
79 }
80 #img_list
81 {
82 width: 486px;
83 height: 20px;
84 padding-top: 5px;
85 overflow: hidden;
86 height: 1%;
87 }
88 #img_list a
89 {
90 display: block;
91 width: 14px;
92 height: 14px;
93 text-indent: -9999px;
94 float: left;
95 margin-right: 5px;
96 background: url(../images/sprite.gif) no-repeat 0 -13px;
97 }
98 #img_list a:hover, #img_list a.active
99 {
100 background-position: -14px -13px;
101 }
102 </style>
103 <script src="../Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
104 <script type="text/javascript">
105 var curr = 0, next = 0, count = 0;
106 $(document).ready(function () {
107 // 记录图片的数量
108   count = $('#img_list a').size();
109
110 t = setInterval('imgPlay()', 3000);
111
112 // 鼠标移动到图片或导航上停止播放,移开后恢复播放
113 $('#imgs li, #img_list a').hover(function () {
114 clearInterval(t);
115 }, function () {
116 t = setInterval('imgPlay()', 3000);
117 });
118
119 //点击导航播放到相应的图片
120 $('#img_list a').click(function () {
121 // index()函数返回当前导航的下标
122 var index = $('#img_list a').index(this);
123 if (curr != index) {
124 play(index);
125 curr = index;
126 };
127 return false;
128 });
129 });
130
131 // 播放图片的函数
132 var imgPlay = function () {
133 next = curr + 1;
134 // 若当前图片播放到最后一张,这设置下一张要播放的图片为第一张图片的下标
135 if (curr == count - 1) next = 0;
136 play(next);
137
138 curr++;
139 // 在当前图片的下标加1后,若值大于最后一张图片的下标,则设置下一轮其实播放的图片下标为第一张图片的下标,而next永远比curr大1
140 if (curr > count - 1) { curr = 0; next = curr + 1; }
141 };
142
143 // 控制播放效果的函数
144 var play = function (next) {
145 // 当前的图片滑到左边-500px,完成后返回到右边490px
146 // 下一张图片滑到0px处,完成后导航的焦点切换到下一个点上
147 $('#imgs li').eq(curr).css({ 'opacity': '0.5' }).animate({ 'left': '-500px', 'opacity': '1' }, 'slow', function () {
148 $(this).css({ 'left': '490px' });
149 }).end()
150 .eq(next).animate({ 'left': '0px', 'opacity': '1' }, 'slow', function () {
151 $('#img_list a').siblings('a').removeClass('active').end().eq(next).addClass('active');
152 });
153 };
154 </script>
155 </head>
156 <body>
157 <div id="imgs">
158 <div class="mid">
159 <ul>
160 <li class="first"><a href="http://meinv.vs2010.com/topic/7/201107/show-id2231.html"
161 target="_blank">
162 <img src="http://meinv.vs2010.com/DownLoad/2011-7-6/e9c17b8d52e14757b03729fe57dac21f.jpg"
163 alt="1" /></a></li>
164 <li><a href="http://meinv.vs2010.com/topic/7/201106/show-id2130.html" target="_blank">
165 <img src="http://meinv.vs2010.com/DownLoad/2011-7-2/2aaaba4b512c4af9ad335d962ea1b00e.jpg"
166 alt="2" /></a></li>
167 <li><a href="http://meinv.vs2010.com/topic/7/201106/show-id2184.html" target="_blank">
168 <img src="http://meinv.vs2010.com/DownLoad/2011-7-1/622f65f85622417f8c19a47545265c41.jpg"
169 alt="3" /></a></li>
170 <li><a href="http://meinv.vs2010.com/topic/7/201106/show-id2163.html" target="_blank">
171 <img src="http://meinv.vs2010.com/DownLoad/2011-6-25/81575b5f42ff417e85c13f9a77054f8c.jpg"
172 alt="4" /></a></li>
173 </ul>
174 <div id="img_list">
175 <a href="#1" class="active">1</a> <a href="#2">2</a> <a href="#3">3</a> <a href="#4">
176 4</a>
177 </div>
178 </div>
179 </div>
180 </body>
181 </html>
posted @ 2011-07-06 19:08  孩他爸  阅读(527)  评论(0编辑  收藏  举报