├── 1.jpg ├── 2.jpg ├── 3.jpg ├── 4.jpg ├── 5.jpg ├── AJAX.jpg ├── logo.jpg ├── timg.jpg ├── gouri.jpg ├── http协议.jpg ├── jsonp.jpg ├── prismjs.jpg ├── README.md ├── index.html └── roomSlider.js /1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkjoebinbin/JS-roomSlider/HEAD/1.jpg -------------------------------------------------------------------------------- /2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkjoebinbin/JS-roomSlider/HEAD/2.jpg -------------------------------------------------------------------------------- /3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkjoebinbin/JS-roomSlider/HEAD/3.jpg -------------------------------------------------------------------------------- /4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkjoebinbin/JS-roomSlider/HEAD/4.jpg -------------------------------------------------------------------------------- /5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkjoebinbin/JS-roomSlider/HEAD/5.jpg -------------------------------------------------------------------------------- /AJAX.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkjoebinbin/JS-roomSlider/HEAD/AJAX.jpg -------------------------------------------------------------------------------- /logo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkjoebinbin/JS-roomSlider/HEAD/logo.jpg -------------------------------------------------------------------------------- /timg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkjoebinbin/JS-roomSlider/HEAD/timg.jpg -------------------------------------------------------------------------------- /gouri.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkjoebinbin/JS-roomSlider/HEAD/gouri.jpg -------------------------------------------------------------------------------- /http协议.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkjoebinbin/JS-roomSlider/HEAD/http协议.jpg -------------------------------------------------------------------------------- /jsonp.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkjoebinbin/JS-roomSlider/HEAD/jsonp.jpg -------------------------------------------------------------------------------- /prismjs.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pkjoebinbin/JS-roomSlider/HEAD/prismjs.jpg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JS-roomSlider 2 | **

原生JS撸的轮播图插件

** 3 | 4 | 5 | 原生JS练手手写了个轻量级的轮播图插件,无第三方依赖,开箱即用,没有过多的繁杂配置。 6 | 7 |

demo

8 |
9 | 10 | 11 | 12 |

1.引入JS

13 | 14 | 15 |
16 |

2.初始化结构。外层div需给定id以及设置宽高,img图片需给定宽高。

17 | 18 | 19 | 30 | 31 |
32 |
33 | 34 | 35 | 36 | 37 | 38 |
39 |
40 |
41 | 42 |

3.JS传入ID。

43 | 44 | var roomSlider = new RoomSlider('slide') 45 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 轮播图 6 | 7 | 8 | 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 | 169 | 170 | -------------------------------------------------------------------------------- /roomSlider.js: -------------------------------------------------------------------------------- 1 | ;(function(){ 2 | 3 | var RoomSlider = function(slideId){ //给一个参数传递 4 | 5 | 6 | this.slide = document.getElementById(slideId); //获取第一层 7 | this.room = this.slide.getElementsByTagName('div')[0]; //获取第二层 8 | this.img = this.room.getElementsByTagName('img'); //获取图片的个数 9 | this.slideWidth = parseInt(getComputedStyle(this.slide).width); //获取slide的宽度 10 | this.slideHeight = parseInt(getComputedStyle(this.slide).height); //获取slide的高度,用来动态居中按钮的位置 11 | 12 | 13 | 14 | this.up = null; //获取按钮 15 | this.down = null; //获取按钮 16 | this.navButton = null; //获取到所有的li 17 | 18 | this.time = null; //定时器 19 | this.imgIndex = 1; //获取图片位置 20 | this.speed = -(this.slideWidth/10); //动画速度 21 | 22 | this.resetAllButton(); //初始化设置 23 | 24 | this.slide.onmouseenter = this.onmouseenter.bind(this); //按钮绑定事件 25 | this.slide.onmouseleave = this.onmouseleave.bind(this); //按钮绑定事件 26 | this.down.onclick = this.nextStart.bind(this); //按钮绑定事件 27 | this.up.onclick = this.prevStart.bind(this); //按钮绑定事件 28 | this.autoStart = setInterval(this.nextStart.bind(this),3000); //定时器 29 | 30 | 31 | 32 | 33 | 34 | //遍历所有原点 35 | for(var j=0;j (-_this.slideWidth*_this.imgIndex) ){ 169 | _this.room.style.left = parseInt(_this.room.style.left)+_this.speed+'px'; 170 | }else{ 171 | clearInterval(_this.time); 172 | _this.room.style.left = -_this.slideWidth*_this.imgIndex +'px'; 173 | 174 | _this.navButton[_this.imgIndex-1].style.background = '#fff'; 175 | _this.imgIndex++; 176 | _this.navButton[_this.imgIndex-1].style.background = '#333'; 177 | 178 | } 179 | 180 | }else{ 181 | if(left < 0){ 182 | _this.room.style.left = parseInt(_this.room.style.left)+(-_this.speed*2)+'px'; 183 | 184 | }else{ 185 | clearInterval(_this.time); 186 | _this.room.style.left = 0+'px'; 187 | _this.navButton[_this.imgIndex-1].style.background = '#fff'; 188 | _this.imgIndex = 1; 189 | _this.navButton[_this.imgIndex-1].style.background = '#333'; 190 | 191 | 192 | }; 193 | 194 | }; 195 | },16.7); 196 | 197 | } 198 | 199 | 200 | //up按钮事件 201 | RoomSlider.prototype.prevStart = function(){ 202 | 203 | clearInterval(this.time); 204 | var _this = this; 205 | 206 | this.time = setInterval(function(){ 207 | 208 | var left = parseInt(_this.room.style.left); 209 | 210 | if(_this.imgIndex == 1){ 211 | if(left > -_this.slideWidth*(_this.img.length-1)){ 212 | _this.room.style.left = left+(_this.speed*2) +'px'; 213 | 214 | }else{ 215 | clearInterval(_this.time); 216 | _this.room.style.left = -_this.slideWidth*(_this.img.length-1)+'px'; 217 | 218 | _this.navButton[_this.imgIndex-1].style.background = '#fff'; 219 | _this.imgIndex = _this.img.length; 220 | _this.navButton[_this.imgIndex-1].style.background = '#333'; 221 | } 222 | }else{ 223 | 224 | 225 | if(left < -(_this.slideWidth*(_this.imgIndex-1)-_this.slideWidth)){ 226 | _this.room.style.left = left - _this.speed +'px'; 227 | 228 | }else{ 229 | clearInterval(_this.time); 230 | _this.room.style.left = -(_this.slideWidth*(_this.imgIndex-1)-_this.slideWidth) + 'px'; 231 | 232 | _this.navButton[_this.imgIndex-1].style.background = '#fff'; 233 | _this.imgIndex--; 234 | _this.navButton[_this.imgIndex-1].style.background = '#333'; 235 | } 236 | 237 | } 238 | 239 | },16.7); 240 | } 241 | 242 | 243 | //原点点击事件 244 | RoomSlider.prototype.navButtonClick = function(e){ 245 | 246 | 247 | clearInterval(this.time); 248 | var _this= this; 249 | var liTarget = e.target; //匿名函数的this问题,需要同时指向构造函数与目标dom,采用e.target获取index 250 | 251 | this.time = setInterval(function(){ 252 | 253 | 254 | var left = parseInt(_this.room.style.left); 255 | 256 | //如果点击的原点位置大于轮播现在的位置 257 | if(liTarget.index>_this.imgIndex){ 258 | if(left > -(_this.slideWidth)*(liTarget.index-1)){ 259 | _this.room.style.left = parseInt(_this.room.style.left)+_this.speed*(liTarget.index-_this.imgIndex)+'px'; 260 | }else{ 261 | clearInterval(_this.time); 262 | _this.room.style.left = -(_this.slideWidth)*(liTarget.index-1)+'px'; 263 | _this.navButtonStyle.call(_this,liTarget); 264 | 265 | 266 | } 267 | 268 | //如果点击的原点位置小于轮播的位置 269 | }else if(liTarget.index<_this.imgIndex){ 270 | if(left < -_this.slideWidth*(liTarget.index-1)){ 271 | _this.room.style.left = parseInt(_this.room.style.left)-_this.speed*(_this.imgIndex-liTarget.index)+'px'; 272 | }else{ 273 | clearInterval(_this.time); 274 | _this.room.style.left = (-_this.slideWidth)*(liTarget.index-1)+'px'; 275 | _this.navButtonStyle.call(_this,liTarget); 276 | 277 | } 278 | }else{ 279 | return false; 280 | } 281 | },16.7); 282 | } 283 | 284 | 285 | 286 | 287 | //原点样式改变 288 | RoomSlider.prototype.navButtonStyle = function(liTarget){ 289 | liTarget.style.background = '#333'; 290 | this.navButton[this.imgIndex-1].style.background = '#fff'; 291 | this.imgIndex = liTarget.index; 292 | } 293 | 294 | 295 | 296 | this.RoomSlider = RoomSlider; 297 | 298 | })(); 299 | 300 | 301 | 302 | 303 | 304 | 305 | --------------------------------------------------------------------------------