1. function throttle(func, wait) {
    2. let timer = null,
    3. result = null,
    4. previous = 0;
    5. return function anonymous(...args) {
    6. let context = this,
    7. now = new Date,
    8. spanTime = wait - (now - previous);
    9. if (spanTime <= 0) {
    10. result = func.call(context, ...args);
    11. clearTimeout(timer);
    12. timer = null;
    13. previous = now;
    14. } else if (!timer) {
    15. timer = setTimeout(() => {
    16. result = func.call(context, ...args);
    17. timer = null;
    18. previous = new Date;
    19. }, spanTime);
    20. }
    21. return result;
    22. }
    23. }
    24. function debounce(func, wait, immediate) {
    25. let timer = null,
    26. result = null;
    27. return function anonymous(...args) {
    28. let context = this,
    29. now = immediate && !timer;
    30. clearTimeout(timer);
    31. timer = setTimeout(() => {
    32. timer = null;
    33. !immediate ? result = func.call(context, ...args) : null;
    34. }, wait);
    35. now ? result = func.call(context, ...args) : null;
    36. return result;
    37. }
    38. }
    39. let bannerModule = (function () {
    40. let $container = $('.container'),
    41. $wrapper = $container.find('.wrapper'),
    42. $pagination = $container.find('.pagination'),
    43. $slideList = null;
    44. let autoTimer = null,
    45. interval = 1000,
    46. speed = 300,
    47. activeIndex = 0,
    48. count = 0;
    49. // queryData:获取数据
    50. let queryData = function (callBack) {
    51. $.ajax({
    52. url: 'json/bannerData1.json',
    53. method: 'get',
    54. async: true,
    55. success: result => {
    56. callBack && callBack(result);
    57. }
    58. });
    59. };
    60. // bindHTML:数据绑定
    61. let bindHTML = function (data) {
    62. let str1 = ``,
    63. str2 = ``;
    64. data.forEach((item, index) => {
    65. str1 += `<div class="slide">
    66. <img src="${item.pic}" alt="">
    67. </div>`;
    68. str2 += `<span class="${index===0?'active':''}"></span>`;
    69. });
    70. $wrapper.html(str1);
    71. $pagination.html(str2);
    72. // 获取结构内容
    73. $slideList = $wrapper.children('.slide');
    74. };
    75. // autoMove:自动轮播
    76. let change = function () {
    77. let $active = $slideList.eq(activeIndex),
    78. $siblings = $active.siblings();
    79. $active.css('transition', `opacity ${speed}ms`);
    80. $siblings.css('transition', `opacity 0ms`);
    81. $active.css('z-index', 1);
    82. $siblings.css('z-index', 0);
    83. $active.css('opacity', 1).on('transitionend', function () {
    84. $siblings.css('opacity', 0);
    85. });
    86. $pagination.children('span').each((index, item) => {
    87. let $item = $(item);
    88. if (index === activeIndex) {
    89. $item.addClass('active');
    90. return;
    91. }
    92. $item.removeClass('active');
    93. });
    94. };
    95. let autoMove = function () {
    96. activeIndex++;
    97. activeIndex >= count ? activeIndex = 0 : null;
    98. change();
    99. };
    100. return {
    101. init() {
    102. queryData(function anonymous(data) {
    103. bindHTML(data);
    104. count = data.length;
    105. autoTimer = setInterval(autoMove, interval);
    106. });
    107. $container.mouseenter(function () {
    108. clearInterval(autoTimer);
    109. }).mouseleave(function () {
    110. autoTimer = setInterval(autoMove, interval);
    111. }).click(function (ev) {
    112. // 基于事件委托实现焦点和左右按钮点击
    113. let target = ev.target,
    114. $target = $(target);
    115. // 事件源:焦点
    116. if (target.tagName === "SPAN" && $target.parent().hasClass('pagination')) {
    117. activeIndex = $target.index();
    118. change();
    119. return;
    120. }
    121. // 事件源:前进后退按钮
    122. if (target.tagName === 'A') {
    123. if ($target.hasClass('button-prev')) {
    124. activeIndex--;
    125. activeIndex < 0 ? activeIndex = count - 1 : null;
    126. change();
    127. return;
    128. }
    129. if ($target.hasClass('button-next')) {
    130. autoMove();
    131. return;
    132. }
    133. }
    134. });
    135. // 在真实项目中,如果要操作的元素是基于 JS 动态绑定的,那么“相关事件行为触发做些事情”的处理操作,我们尽可能基于事件委托来处理(事件委托可以给动态绑定的元素绑定事件)
    136. }
    137. }
    138. })();
    139. bannerModule.init();