图片切换.----so屌
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 <style type="text/css"> 7 body,div,ul,li { 8 margin: 0; 9 padding: 0; 10 } 11 ul,li { 12 list-style: none; 13 } 14 a img { 15 border: none; 16 } 17 .wrap { 18 width: 100%; 19 overflow: hidden; 20 position: relative; 21 } 22 .wrap .prev, 23 .wrap .next { 24 height: 320px; 25 position: absolute; 26 left: -50%; 27 top: 0; 28 background-color: #999; 29 opacity: 0.7; 30 width: 100%; 31 } 32 .wrap .next { 33 left: auto; 34 right: -50%; 35 } 36 .wrap .prev:hover, 37 .wrap .next:hover { 38 opacity: 0.5; 39 } 40 .container { 41 width: 100%; 42 height: 320px; 43 } 44 .container ul { 45 height: 100%; 46 } 47 .container li { 48 width: 1000px; 49 height: 100%; 50 float: left; 51 } 52 .container li a, 53 .container li img { 54 display: block; 55 width: 100%; 56 height: 100%; 57 } 58 .page { 59 position: absolute; 60 left: 50%; 61 bottom: 10px; 62 display:none; 63 } 64 .page span { 65 float: left; 66 margin-right: 10px; 67 width: 20px; 68 height: 20px; 69 display: block; 70 cursor: pointer; 71 background: #999; 72 border-radius: 50%; 73 text-align: center; 74 color: #fff; 75 } 76 .page .select { 77 background: #f00; 78 } 79 </style> 80 </head> 81 <body> 82 <div class="wrap"> 83 <div class="container" id="container"> 84 <ul> 85 <li><a href="1"><img src="http://www.hengqijy.com/uploadfile/2013/1031/20131031084228263.jpg"></a> </li> 86 <li><a href="2"><img src="http://www.hengqijy.com/uploadfile/2014/0315/20140315023711196.jpg"></a></li> 87 <li><a href="3"><img src="http://www.hengqijy.com/uploadfile/2014/0228/20140228091207797.jpg"></a></li> 88 <li><a href="4"><img src="http://www.hengqijy.com/statics/images/new_img/ksss.jpg"></a></li> 89 </ul> 90 </div> 91 <div class="prev" id="prev"></div> 92 <div class="next" id="next"></div> 93 <div id="page" class="page"></div> 94 </div> 95 <script type="text/javascript"> 96 /* 97 * Slider constructor 98 * @param {Node} ele 容器节点 99 * @param {Int} index 默认显示第几张 100 */ 101 var Slider = function(ele, autoInterval){ 102 autoInterval = parseInt(autoInterval, 10); 103 this.autoInterval = !isNaN(autoInterval) && Math.abs(autoInterval) || 3000; 104 this.ele = ele; 105 this.oList = ele.children[0]; 106 this.items = this.oList.getElementsByTagName("li"); 107 this.itemWidth = parseInt(this.items[0].offsetWidth, 10); 108 this.page = document.getElementById("page"); 109 this.prevBtn = document.getElementById("prev"); 110 this.nextBtn = document.getElementById("next"); 111 this.init(); 112 } 113 Slider.prototype = { 114 constructor: Slider, 115 init: function(){ 116 this.oList.style.position = 'absolute'; 117 this.oList.style.top = 0; 118 this.oList.style.left = 0; 119 120 this.going = 0; 121 this.current = 1; 122 this.speed = 100; 123 this.timer = null; 124 this.inter = null; 125 this.lock = false; 126 this.wrapWidth = parseInt(this.ele.offsetWidth, 10); 127 this.pageCircles = null; 128 129 this.prevBtn.style.marginLeft = -this.itemWidth/2 + 'px'; 130 this.nextBtn.style.marginRight = -this.itemWidth/2 + 'px'; 131 132 if(this.items.length > 2) { 133 this.setUp(); 134 } else { 135 this.oList.style.left = (this.wrapWidth - this.itemWidth * this.items.length)/2 + 'px'; 136 this.prevBtn.style.display = "none"; 137 this.nextBtn.style.display = "none"; 138 } 139 140 this.oList.style.width = this.itemWidth * this.items.length + 'px'; 141 142 }, 143 setUp:function(){ 144 var first1 = this.items[0].cloneNode(); 145 first1.innerHTML = this.items[0].innerHTML; 146 147 var first2 = this.items[1].cloneNode(); 148 first2.innerHTML = this.items[1].innerHTML; 149 150 var last1 = this.items[this.items.length-1].cloneNode(); 151 last1.innerHTML = this.items[this.items.length-1].innerHTML; 152 153 var last2 = this.items[this.items.length-2].cloneNode(); 154 last2.innerHTML = this.items[this.items.length-2].innerHTML; 155 156 this.oList.appendChild(first1); 157 this.oList.appendChild(first2); 158 this.oList.insertBefore(last1, this.items[0]); 159 this.oList.insertBefore(last2, this.items[0]); 160 161 this.buildPage(); 162 this.bindEvent(); 163 this.slientGoTo(); 164 this.doAnimate(); 165 this.autoSlider(); 166 }, 167 autoSlider:function(){ 168 var that = this; 169 if(this.autoInterval > 0) { 170 this.inter = setInterval(function(){ 171 that.next(); 172 }, this.autoInterval); 173 } 174 }, 175 stopAutoSlider:function(){ 176 clearInterval(this.inter); 177 }, 178 buildPage:function(){ 179 for(var i = 0,len = this.items.length - 4; i<len; i++) { 180 var circle = document.createElement('span'); 181 circle.innerHTML = i + 1; 182 this.page.appendChild(circle); 183 } 184 this.pageCircles = this.page.getElementsByTagName('span'); 185 this.addEvent(this.page, 'click', 'gotoIndex'); 186 this.page.style.display = 'block'; 187 }, 188 bindEvent:function(){ 189 this.addEvent(this.prevBtn, 'click', 'prev'); 190 this.addEvent(this.nextBtn, 'click', 'next'); 191 this.addEvent(this.ele, 'mouseover' , 'stopAutoSlider'); 192 this.addEvent(this.ele, 'mouseout' , 'autoSlider'); 193 }, 194 addEvent:function(ele, type, fn, context) { 195 context = context || this; 196 var innerFun = function(event) { 197 var evt = event || window.event; 198 if(typeof fn === 'string'){ 199 context[fn].call(context, this, evt); 200 } else { 201 fn.call(context, this, evt); 202 } 203 } 204 if(window.addEventListener) { 205 ele.addEventListener(type, innerFun, false); 206 } else { 207 ele.attachEvent('on'+type, innerFun); 208 } 209 }, 210 index:function(ele){ 211 var parentNode = ele.parentNode; 212 var eles = parentNode.getElementsByTagName(ele.tagName); 213 214 for(var i = 0, len = eles.length; i<len; i++){ 215 if(eles[i] == ele) { 216 return i; 217 } 218 } 219 }, 220 hasClass: function(obj, cls) { 221 return !!obj.className && obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)')); 222 }, 223 addClass: function(obj, cls) { 224 if (!this.hasClass(obj, cls)) obj.className += " " + cls; 225 }, 226 removeClass:function (obj, cls) { 227 if (this.hasClass(obj, cls)) { 228 var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)'); 229 obj.className = obj.className.replace(reg, ' '); 230 } 231 }, 232 gotoIndex:function(eleContext, evt){ 233 var ele = evt.target || evt.srcElement; 234 var index = this.index(ele); 235 if(this.lock){return false;} 236 if(this.current === this.items.length-4 && index === 0) { 237 this.current = 0; 238 this.slientGoTo(); 239 } else if(this.current === 1 && index ===this.items.length-5) { 240 this.current = this.items.length-3; 241 this.slientGoTo(); 242 } 243 this.current = index + 1; 244 this.doAnimate(); 245 }, 246 prev:function(eleContext, evt){ 247 if(this.lock){return false;} 248 this.current--; 249 this.doAnimate(); 250 }, 251 next:function(eleContext, evt){ 252 if(this.lock){return false;} 253 this.current++; 254 this.doAnimate(); 255 }, 256 doAnimate:function(){ 257 this.distance = (this.wrapWidth-this.itemWidth*(2*this.current + 3))/2 - parseInt(this.oList.style.left, 10); 258 if(this.distance < 0){ 259 this.speed = -Math.abs(this.speed); 260 } else { 261 this.speed = Math.abs(this.speed); 262 } 263 this.distance = Math.abs(this.distance); 264 this.lock = true; 265 this.animate(); 266 }, 267 animate:function(){ 268 var that = this; 269 this.timer = setTimeout(function() { 270 var left = parseInt(that.oList.style.left, 10) || 0; 271 272 if (that.going+Math.abs(that.speed) >= that.distance) { 273 if (that.speed > 0) { 274 that.oList.style.left = left + that.distance - that.going + 'px'; 275 } else { 276 that.oList.style.left = left - that.distance + that.going + 'px'; 277 } 278 clearTimeout(that.timer); 279 that.going = 0; 280 that.onceEnd(); 281 } else { 282 that.going += Math.abs(that.speed); 283 that.oList.style.left = left + that.speed + 'px'; 284 that.animate(); 285 } 286 }, 25); 287 288 }, 289 slientGoTo:function(){ 290 this.oList.style.left = (this.wrapWidth-this.itemWidth*(2*this.current + 3))/2 + 'px'; 291 }, 292 setCircleSelect:function(){ 293 for(var i=0,len = this.pageCircles.length; i<len; i++) { 294 var ele= this.pageCircles[i]; 295 if(this.hasClass(ele, 'select')) { 296 this.removeClass(ele, 'select'); 297 } 298 } 299 this.addClass(this.pageCircles[this.current-1], 'select'); 300 }, 301 correctCurrent:function(){ 302 if(this.current === 0) { 303 this.current = this.items.length - 4; 304 } else if(this.current === this.items.length - 3) { 305 this.current = 1; 306 } else { 307 return false; 308 } 309 this.slientGoTo(); 310 }, 311 onceEnd:function(){ 312 this.lock = false; 313 this.correctCurrent(); 314 this.setCircleSelect(); 315 } 316 } 317 var con = document.getElementById("container"); 318 var s = new Slider(con, 4000); 319 </script> 320 </body> 321 </html>
1 /* 2 *2 3 */ 4 <!doctype html> 5 <html lang="en"> 6 <head> 7 <meta charset="UTF-8"> 8 <title>Document</title> 9 <style type="text/css"> 10 body,div,ul,li { 11 margin: 0; 12 padding: 0; 13 } 14 ul,li { 15 list-style: none; 16 } 17 a img { 18 border: none; 19 } 20 .wrap { 21 width: 100%; 22 overflow: hidden; 23 position: relative; 24 } 25 .wrap .prev, 26 .wrap .next { 27 height: 320px; 28 position: absolute; 29 left: -50%; 30 top: 0; 31 background-color: #999; 32 opacity: 0.7; 33 width: 100%; 34 } 35 .wrap .next { 36 left: auto; 37 right: -50%; 38 } 39 .wrap .prev:hover, 40 .wrap .next:hover { 41 opacity: 0.5; 42 } 43 .container { 44 width: 100%; 45 height: 320px; 46 } 47 .container ul { 48 height: 100%; 49 } 50 .container li { 51 width: 1000px; 52 height: 100%; 53 float: left; 54 } 55 .container li a, 56 .container li img { 57 display: block; 58 width: 100%; 59 height: 100%; 60 } 61 .page { 62 position: absolute; 63 left: 50%; 64 bottom: 10px; 65 display:none; 66 } 67 .page span { 68 float: left; 69 margin-right: 10px; 70 width: 20px; 71 height: 20px; 72 display: block; 73 cursor: pointer; 74 background: #999; 75 border-radius: 50%; 76 text-align: center; 77 color: #fff; 78 } 79 .page .select { 80 background: #f00; 81 } 82 </style> 83 </head> 84 <body> 85 <div class="wrap"> 86 <div class="container" id="container"> 87 <ul> 88 <li><a href="1"><img src="http://www.hengqijy.com/uploadfile/2013/1031/20131031084228263.jpg"></a> </li> 89 <li><a href="2"><img src="http://www.hengqijy.com/uploadfile/2014/0315/20140315023711196.jpg"></a></li> 90 <li><a href="3"><img src="http://www.hengqijy.com/uploadfile/2014/0228/20140228091207797.jpg"></a></li> 91 <li><a href="4"><img src="http://www.hengqijy.com/statics/images/new_img/ksss.jpg"></a></li> 92 </ul> 93 </div> 94 <div class="prev" id="prev"></div> 95 <div class="next" id="next"></div> 96 <div id="page" class="page"></div> 97 </div> 98 <script type="text/javascript"> 99 /* 100 * Slider constructor 101 * @param {Node} ele 容器节点 102 * @param {Int} index 默认显示第几张 103 */ 104 var Slider = function(ele, autoInterval){ 105 autoInterval = parseInt(autoInterval, 10); 106 this.autoInterval = !isNaN(autoInterval) && Math.abs(autoInterval) || 3000; 107 this.ele = ele; 108 this.oList = ele.children[0]; 109 this.items = this.oList.getElementsByTagName("li"); 110 this.itemWidth = parseInt(this.items[0].offsetWidth, 10); 111 this.page = document.getElementById("page"); 112 this.prevBtn = document.getElementById("prev"); 113 this.nextBtn = document.getElementById("next"); 114 this.init(); 115 } 116 Slider.prototype = { 117 constructor: Slider, 118 init: function(){ 119 this.oList.style.position = 'absolute'; 120 this.oList.style.top = 0; 121 this.oList.style.left = 0; 122 123 this.going = 0; 124 this.current = 1; 125 this.speed = 100; 126 this.timer = null; 127 this.inter = null; 128 this.lock = false; 129 this.wrapWidth = parseInt(this.ele.offsetWidth, 10); 130 this.pageCircles = null; 131 132 this.prevBtn.style.marginLeft = -this.itemWidth/2 + 'px'; 133 this.nextBtn.style.marginRight = -this.itemWidth/2 + 'px'; 134 135 if(this.items.length > 2) { 136 this.setUp(); 137 } else { 138 this.oList.style.left = (this.wrapWidth - this.itemWidth * this.items.length)/2 + 'px'; 139 this.prevBtn.style.display = "none"; 140 this.nextBtn.style.display = "none"; 141 } 142 143 this.oList.style.width = this.itemWidth * this.items.length + 'px'; 144 145 }, 146 setUp:function(){ 147 var first1 = this.items[0].cloneNode(); 148 first1.innerHTML = this.items[0].innerHTML; 149 150 var first2 = this.items[1].cloneNode(); 151 first2.innerHTML = this.items[1].innerHTML; 152 153 var last1 = this.items[this.items.length-1].cloneNode(); 154 last1.innerHTML = this.items[this.items.length-1].innerHTML; 155 156 var last2 = this.items[this.items.length-2].cloneNode(); 157 last2.innerHTML = this.items[this.items.length-2].innerHTML; 158 159 this.oList.appendChild(first1); 160 this.oList.appendChild(first2); 161 this.oList.insertBefore(last1, this.items[0]); 162 this.oList.insertBefore(last2, this.items[0]); 163 164 this.buildPage(); 165 this.bindEvent(); 166 this.slientGoTo(); 167 this.doAnimate(); 168 this.autoSlider(); 169 }, 170 autoSlider:function(){ 171 var that = this; 172 if(this.autoInterval > 0) { 173 this.inter = setInterval(function(){ 174 that.next(); 175 }, this.autoInterval); 176 } 177 }, 178 stopAutoSlider:function(){ 179 clearInterval(this.inter); 180 }, 181 buildPage:function(){ 182 for(var i = 0,len = this.items.length - 4; i<len; i++) { 183 var circle = document.createElement('span'); 184 circle.innerHTML = i + 1; 185 this.page.appendChild(circle); 186 } 187 this.pageCircles = this.page.getElementsByTagName('span'); 188 this.addEvent(this.page, 'click', 'gotoIndex'); 189 this.page.style.display = 'block'; 190 }, 191 bindEvent:function(){ 192 this.addEvent(this.prevBtn, 'click', 'prev'); 193 this.addEvent(this.nextBtn, 'click', 'next'); 194 this.addEvent(this.ele, 'mouseover' , 'stopAutoSlider'); 195 this.addEvent(this.ele, 'mouseout' , 'autoSlider'); 196 }, 197 addEvent:function(ele, type, fn, context) { 198 context = context || this; 199 var innerFun = function(event) { 200 var evt = event || window.event; 201 if(typeof fn === 'string'){ 202 context[fn].call(context, this, evt); 203 } else { 204 fn.call(context, this, evt); 205 } 206 } 207 if(window.addEventListener) { 208 ele.addEventListener(type, innerFun, false); 209 } else { 210 ele.attachEvent('on'+type, innerFun); 211 } 212 }, 213 index:function(ele){ 214 var parentNode = ele.parentNode; 215 var eles = parentNode.getElementsByTagName(ele.tagName); 216 217 for(var i = 0, len = eles.length; i<len; i++){ 218 if(eles[i] == ele) { 219 return i; 220 } 221 } 222 }, 223 hasClass: function(obj, cls) { 224 return !!obj.className && obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)')); 225 }, 226 addClass: function(obj, cls) { 227 if (!this.hasClass(obj, cls)) obj.className += " " + cls; 228 }, 229 removeClass:function (obj, cls) { 230 if (this.hasClass(obj, cls)) { 231 var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)'); 232 obj.className = obj.className.replace(reg, ' '); 233 } 234 }, 235 gotoIndex:function(eleContext, evt){ 236 var ele = evt.target || evt.srcElement; 237 var index = this.index(ele); 238 if(this.current === this.items.length-4 && index === 0) { 239 if(this.setCurrent(0)){ 240 this.slientGoTo(); 241 } 242 } else if(this.current === 1 && index ===this.items.length-5) { 243 if(this.setCurrent(this.items.length-3)){ 244 this.slientGoTo(); 245 } 246 } 247 if(this.setCurrent(index + 1)){ 248 this.doAnimate(); 249 } 250 }, 251 prev:function(eleContext, evt){ 252 if(this.setCurrent(this.current-1)){ 253 this.doAnimate(); 254 } 255 }, 256 next:function(eleContext, evt){ 257 if(this.setCurrent(this.current+1)){ 258 this.doAnimate(); 259 } 260 }, 261 doAnimate:function(){ 262 this.distance = (this.wrapWidth-this.itemWidth*(2*this.current + 3))/2 - parseInt(this.oList.style.left, 10); 263 if(this.distance < 0){ 264 this.speed = -Math.abs(this.speed); 265 } else { 266 this.speed = Math.abs(this.speed); 267 } 268 this.distance = Math.abs(this.distance); 269 this.lock = true; 270 this.animate(); 271 }, 272 setCurrent:function(index){ 273 if(this.lock){return false;} 274 this.current = index; 275 return true; 276 }, 277 animate:function(){ 278 var that = this; 279 this.timer = setTimeout(function() { 280 var left = parseInt(that.oList.style.left, 10) || 0; 281 282 if (that.going+Math.abs(that.speed) >= that.distance) { 283 if (that.speed > 0) { 284 that.oList.style.left = left + that.distance - that.going + 'px'; 285 } else { 286 that.oList.style.left = left - that.distance + that.going + 'px'; 287 } 288 clearTimeout(that.timer); 289 that.going = 0; 290 that.onceEnd(); 291 } else { 292 that.going += Math.abs(that.speed); 293 that.oList.style.left = left + that.speed + 'px'; 294 that.animate(); 295 } 296 }, 25); 297 298 }, 299 slientGoTo:function(){ 300 this.oList.style.left = (this.wrapWidth-this.itemWidth*(2*this.current + 3))/2 + 'px'; 301 }, 302 setCircleSelect:function(){ 303 for(var i=0,len = this.pageCircles.length; i<len; i++) { 304 var ele= this.pageCircles[i]; 305 if(this.hasClass(ele, 'select')) { 306 this.removeClass(ele, 'select'); 307 } 308 } 309 this.addClass(this.pageCircles[this.current-1], 'select'); 310 }, 311 correctCurrent:function(){ 312 if(this.current === 0) { 313 this.current = this.items.length - 4; 314 } else if(this.current === this.items.length - 3) { 315 this.current = 1; 316 } else { 317 return false; 318 } 319 this.slientGoTo(); 320 }, 321 onceEnd:function(){ 322 this.correctCurrent(); 323 this.setCircleSelect(); 324 this.lock = false; 325 } 326 } 327 var con = document.getElementById("container"); 328 var s = new Slider(con); 329 </script> 330 </body> 331 </html>
- 不待续...