面向对象的轮播图
用的是JQ,大概就是先生成dom,然后绑定事件,然后init~
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 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
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>面向对象轮播图</title> <style> * { margin: 0 auto; padding: 0; list-style: none; }
#container { width: 1200px; height: 600px; margin: 0 auto; overflow: hidden; position: relative; }
.uls { position: absolute; left: 0; top: 0; }
.uls li { width: 1200px; height: 600px; float: left; }
.yuan { position: absolute; }
.circleUL { transform: translate(-50%, 0); }
.circle { width: 30px; height: 30px; background: rgba(0, 0, 0, 0.8); border-radius: 15px; margin-left: 10px; float: left; }
.cur { background: rgba(155, 200, 3, 5); } </style> </head>
<body> <div id="container"></div> </body> <script src="jquery-3.1.1.js"></script> <script> function Picrun(options) { this.images = options.images || []; this.width = options.width || 1200; this.height = options.height || 600; this.index = 0; this.timer = null; }; Picrun.prototype = { init: function () { this.bindDOM(); this.bindEvent(); this.bindRUN(); }, bindDOM() { const that = this; this.OUL = $('<ul></ul>'); this.OUL.addClass('uls'); this.circleOUL = $('<ul></ul>'); this.circleOUL.addClass('circleUL'); this.circleOUL.css({ 'left': that.width / 2, 'bottom': '20px', 'position': 'absolute', 'translate': '(-50%,-50%)' }); this.OUL.width((this.images.length + 1) * 100 + '%'); for (let i = 0; i < this.images.length; i++) { let OLI = $('<li><img src="' + this.images[i] + '" alt=""></li>'); this.circle = $('<li></li>'); this.circle.addClass('circle'); this.circleOUL.append(this.circle); this.OUL.append(OLI); } this.OUL.append(this.OUL.children(0).eq(0).clone()); this.circleOUL.css({ 'marginLeft': -(this.circleOUL.width / 2) }); this.circleOUL.children().eq(0).addClass('cur').siblings().removeClass('cur'); $('#container').append(this.OUL); $('#container').append(this.circleOUL); }, bindEvent() { const that = this; this.circleOUL.children().each(function (index) { $(this).click(() => { that.bindPause(); $(this).addClass('cur').siblings().removeClass('cur'); if (that.index == 5 && $(this).index() == 1) { that.OUL.css({ 'left': 0 }); } that.OUL.stop().animate({ 'left': -index * that.width }); that.bindRUN($(this).index()); }) }) }, bindRUN(indx) { this.index = indx != undefined ? indx : this.index; this.timer = setInterval(() => { this.index++; if (this.index > 5) { this.index = 1; this.OUL.css({ 'left': 0 }); } this.circleOUL.children().eq(this.index % 5).addClass('cur').siblings().removeClass('cur'); this.OUL.stop().animate({ 'left': -this.width * this.index }); }, 1000);
}, bindPause() { clearInterval(this.timer); } }; var pic = new Picrun({ images: ['11.JPG', '12.JPG', '13.JPG', '14.jpg', '15.jpg'] }); pic.init(); </script> </html>
|
—-end—-
have a good trip!