Banner.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //电脑端banner滑块控件命名空间
  2. Namespace.register("U.MD.UI.slider");
  3. /**
  4. 电脑端banner滑块控件
  5. */
  6. /**
  7. @box {object} 存放控件的div的id
  8. @imgarr {array} 存放图片的数组
  9. @speed {string} 轮播速度
  10. @cut {bool} 判断左右切换按钮是否出现
  11. @dot {bool} 判断小圆点是否出现
  12. @cb {回调callback} 引用方法
  13. **/
  14. U.MD.UI.slider = function (box, imgarr, attr) {
  15. //定义传输进来的参数
  16. var _speed = attr.speed;
  17. var _arrow = attr.arrow;
  18. var _dot = attr.dot;
  19. var _moveFun = attr.moveFun;
  20. //快速创建存放图片的ul
  21. var _sliderimgul = $$("ul", { className: 'U_MD_UI_slider_ImgList' }, box);
  22. //在数组的第一个插入最后一张图片
  23. imgarr.unshift(imgarr[imgarr.length - 1]);
  24. //在arrImg[]最后放第一张图片
  25. imgarr.push(imgarr[1]);
  26. //for循环创建需要的li
  27. for (i = 0; i < imgarr.length; i++) {
  28. var _sliderimgli = $$("li", { className: '' }, _sliderimgul),
  29. _sliderImg = $$("img", { className: '', src: imgarr[i] }, _sliderimgli); //"onerror": U.MD.C.imgError,
  30. }
  31. //快速创建存放左右按钮的ul
  32. var _sliderbtnul = $$("ul", { className: 'U_MD_UI_slider_Btn' }, box),
  33. _sliderbtnlil = $$("li", { className: 'U_MD_UI_slider_Left', innerHTML: '<' }, _sliderbtnul),
  34. _sliderbtnlir = $$("li", { className: 'U_MD_UI_slider_Right', innerHTML: '>' }, _sliderbtnul);
  35. //快速创建存放小圆点的ul
  36. var _sliderpointul = $$("ul", { className: 'U_MD_UI_slider_Point' }, box);
  37. //快速创建li,li的长度为图片的减两张(补图法)
  38. for (i = 0; i < imgarr.length - 3; i++) {
  39. //第一个li时,定义第一个li的class
  40. if (i == 0) {
  41. $$("li", { className: 'U_MD_UI_slider_On' }, _sliderpointul)[0];
  42. }
  43. $$("li", { className: '' }, _sliderpointul)[i];
  44. }
  45. //定义父元素的宽度
  46. var _boxidwidth = box.offsetWidth;
  47. //定义父元素的高度
  48. var _boxidheight = box.offsetHeight;
  49. //定义class为U_MD_UI_slider_ImgList
  50. var _sliderimglist = box.getElementsByClassName("U_MD_UI_slider_ImgList")[0];
  51. var _sliderimgli = _sliderimglist.getElementsByTagName("li");
  52. //定义class为U_MD_UI_slider_Btn
  53. var _sliderbtn = document.getElementsByClassName("U_MD_UI_slider_Btn")[0];
  54. //定义class为U_MD_UI_slider_Point
  55. var _sliderpoint = box.getElementsByClassName('U_MD_UI_slider_Point')[0];
  56. //定义class为_sliderbtn的所有li
  57. var _btnli = _sliderbtn.getElementsByTagName("li");
  58. //定义class为_sliderimglist的li长度
  59. var _listlen = _sliderimglist.getElementsByTagName("li").length;
  60. //定义图片索引为1
  61. var _listindex = 1;
  62. //定义_sliderpoint里的li
  63. var _pointdot = _sliderpoint.getElementsByTagName("li");
  64. //定义_pointdot.length
  65. var _dotlen = _pointdot.length;
  66. //定义小圆点索引为0
  67. var _dotindex = 0;
  68. //初始化timer
  69. var timer = null;
  70. //如果obj.dot为true则显示小圆点
  71. if (_dot) {
  72. _sliderpoint.style.display = "block";
  73. } else {
  74. _sliderpoint.style.display = "none";
  75. }
  76. /** Banner宽度和高度跟随父元素 **/
  77. //定义_sliderimglist里的li
  78. var _sliderli = _sliderimglist.getElementsByTagName("li");
  79. //所有图片的高宽等于盒子
  80. for (var i = 0; i < _sliderli.length; i++) {
  81. _sliderli[i].style.width = _boxidwidth + "px";
  82. _sliderli[i].style.height = _boxidheight + "px";
  83. }
  84. //存放图片的ul的总宽度(盒子长度*图片数量)
  85. _sliderimglist.style.width = (_boxidwidth * imgarr.length) + "px";
  86. //第一张图片的位置
  87. _sliderimglist.style.left = -_boxidwidth + "px";
  88. //左右切换按钮宽度跟随盒子宽度
  89. _sliderbtn.style.width = _boxidwidth + "px";
  90. /** 鼠标停留计时器停止或启动、左右按钮显示 **/
  91. //鼠标停留
  92. box.onmouseover = function () {
  93. //如果obj.cut为true则显示按钮
  94. if (_arrow) {
  95. _sliderbtn.style.display = "block";
  96. } else {
  97. _sliderbtn.style.display = "none";
  98. }
  99. //清除计时器
  100. clearInterval(timer);
  101. }
  102. //鼠标离开
  103. box.onmouseout = function () {
  104. _sliderbtn.style.display = "none";
  105. //启动计时器
  106. timer = setInterval(auto, _speed);
  107. }
  108. //定义当前时间
  109. var sTime = new Date();
  110. //左边按钮
  111. _btnli[0].onclick = function () {
  112. //定义点击后的时间
  113. var nTime = new Date();
  114. //点击后时间距离下次点击要大于500ms才能继续下次点击
  115. if (nTime - sTime > 500) {
  116. //图片索引和小圆点索引减少
  117. _listindex--;
  118. _dotindex--;
  119. if (_listindex == 0) {
  120. //调用move函数
  121. move(_sliderimglist, { left: -_boxidwidth * _listindex }, function () {
  122. _listindex = _listlen - 2;
  123. this.style.left = -_listindex * _boxidwidth + "px";
  124. });
  125. } else {
  126. move(_sliderimglist, { left: -_boxidwidth * _listindex });
  127. }
  128. //小圆点移动
  129. _dotindex = _dotindex == -1 ? _dotlen - 1 : _dotindex;
  130. moveBtn(_dotindex);
  131. _moveFun && _moveFun(_dotindex + 1);
  132. }
  133. sTime = new Date();
  134. }
  135. //右边按钮
  136. _btnli[1].onclick = function () {
  137. //定义点击后的时间
  138. var nTime = new Date();
  139. //点击后时间距离下次点击要大于500ms才能继续下次点击
  140. if (nTime - sTime > 500) {
  141. //图片索引和小圆点索引增加
  142. _listindex++;
  143. _dotindex++;
  144. //小圆点索引清零
  145. _dotindex = _dotindex % _dotlen;
  146. //图片到最后一张时索引清零重新计算
  147. if (_listindex == _listlen - 1) {
  148. move(_sliderimglist, { left: -_boxidwidth * _listindex }, function () {
  149. _listindex = 1;
  150. this.style.left = -_listindex * _boxidwidth + "px";
  151. });
  152. } else {
  153. move(_sliderimglist, { left: -_boxidwidth * _listindex });
  154. }
  155. //小圆点移动
  156. moveBtn(_dotindex);
  157. _moveFun && _moveFun(_dotindex + 1);
  158. }
  159. sTime = new Date();
  160. }
  161. //定义自动轮播
  162. timer = setInterval(auto, _speed);
  163. if (_dotindex == 0) {
  164. console.log(_dotindex + 1);
  165. }
  166. //自动轮播
  167. function auto() {
  168. /* for (var i = 0; i < _sliderimgli.length; i++) {
  169. _sliderimgli[i]._listindex = i;
  170. //点击小圆点
  171. //_listindex = this._listindex;
  172. console.log(_listindex+1);
  173. }*/
  174. //图片索引和小圆点索引
  175. _listindex = ++_listindex % _listlen;
  176. _dotindex++;
  177. //小圆点索引清零
  178. _dotindex = _dotindex % _dotlen;
  179. //图片到最后一张时索引清零重新计算
  180. if (_listindex == _listlen - 1) {
  181. move(_sliderimglist, { left: -_boxidwidth * _listindex }, function () {
  182. _listindex = 1;
  183. this.style.left = -_listindex * _boxidwidth + "px";
  184. });
  185. } else {
  186. move(_sliderimglist, { left: -_boxidwidth * _listindex });
  187. }
  188. moveBtn(_dotindex);
  189. _moveFun && _moveFun(_dotindex + 1);
  190. }
  191. //遍历小圆点
  192. for (var i = 0; i < _dotlen; i++) {
  193. _pointdot[i]._listindex = i;
  194. //点击小圆点
  195. _pointdot[i].onclick = function () {
  196. _dotindex = this._listindex;
  197. _listindex = _dotindex + 1;
  198. move(_sliderimglist, { left: -_boxidwidth * _listindex });
  199. moveBtn(_dotindex);
  200. //console.log(_dotindex);
  201. }
  202. }
  203. //小圆点样式
  204. function moveBtn(_dotindex) {
  205. for (var j = 0; j < _dotlen; j++) {
  206. _pointdot[j].className = "";
  207. }
  208. _pointdot[_dotindex].className = "U_MD_UI_slider_On";
  209. }
  210. }
  211. /*
  212. 切换过程函数
  213. @dom {object} 需要移动的对象
  214. @json {Array}向某个方向移动的距离
  215. @callback {回调callback}
  216. */
  217. function move(dom, json, callback) {
  218. //清除计时器
  219. clearInterval(dom.timer);
  220. dom.timer = setInterval(function () {
  221. //true这个计时器用来标记json中所有的attr已到达target目标
  222. var mark = true;
  223. for (var attr in json) {
  224. var cur = null;
  225. if (attr == "opacity") {
  226. cur = getStyle(dom, attr) * 100;
  227. } else {
  228. cur = parseInt(getStyle(dom, attr)) || 0;
  229. }
  230. var target = json[attr];
  231. // console.log(target);
  232. // console.log(cur);
  233. var speed = (target - cur) * .2;
  234. speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
  235. if (cur != target) {
  236. if (attr == "opacity") {
  237. dom.style.filter = "alpha(opacity = " + (cur + speed) + ")"; //ie
  238. dom.style[attr] = (cur + speed) / 100; //0~1
  239. } else {
  240. dom.style[attr] = cur + speed + "px";
  241. }
  242. //false表明当前的这个attr没有达到目标
  243. mark = false;
  244. }
  245. //console.log(cur);
  246. //console.log(1);
  247. }
  248. if (mark) {
  249. clearInterval(dom.timer);
  250. callback && callback.call(dom);
  251. }
  252. }, 100 / 3);
  253. }
  254. function getStyle(dom, attr) {
  255. return dom.currentStyle ? dom.currentStyle[attr] : getComputedStyle(dom, null)[attr];
  256. }