最近lz在做项目的一些优化,发现我的项目里有个奖品列表的功能;我们之前是引入swiper这个库去做的;swiper库的滑动效果确实比较好看,但是js文件以及css文件相对是比较大的;考虑到这个小小的需求而去引入如此大的库,感觉太不值得了;所以自己去封装了一下;

  项目原需求如下图:

  

       封装思路:

  1.我想的是swiper在布局是不是隐藏掉了滚动条?因为在进行左右滑动时候首先想到的是css属性“overflow-x:scroll或者overflow-x:auto”;但是我审查了下元素发现swiper并没有使用这个css属性;经过查看原来使用了translate3d去让其滑动的;于是解决了滑动问题;

  2.思考可视展示问题;我的做法是将整个大的容器除以页面可视容器所要展示的个数得到每个子元素的宽度;然后进行排列;这样布局问题就搞定了;

  3.就是滑动问题,监听容器的touchStart、touchMove、touchEnd事件;先要计算出两边的临界值;左边很好理解就是0,右边稍微计算下把整个子容器宽度和-父级容器宽度=最大滑动距离;这样再做下滑动临界值的处理就好了;

  4.联动问题,滚动条的滑动跟容器内容滑动原理是一样的,稍微动脑下的是联动的一个逻辑。这里有个等式就是容器滑动距离/容器最大滑动距离 = 滚动条滑动距离/滚动条最大滑动距离;这样就能做好相应的联动效果;对于边界值,我们再稍微的优化下;整个功能就差不多实现了;

  封装代码如下:

  1. /**
  2. * 我们在各个活动工具中要用到swiper去做奖品列表的渲染,由于swiper.js和swiper.css文件较大;所以仿照swiper封装了此组件
  3. * @param {Object} config
  4. * @param {String} containerId:选择器
  5. * @param {Number} slidesPerView:一屏要展示数
  6. * @param {Number} spaceBetween:奖品间距
  7. * @param {Boolean} scrollbarHide:是否显示滚动条
  8. */
  9. import './index.scss';
  10. class CSlide {
  11. init(config) {
  12. let { containerId, slidesPerView, spaceBetween = 20, scrollbarHide } = config;
  13. this.container = document.querySelector(containerId);
  14. this.wrapper = this.container.children[0];
  15. this.store = {
  16. containerId,
  17. containerWidth: this.wrapper.offsetWidth,
  18. children: this.wrapper.children,
  19. slidesPerView,
  20. spaceBetween,
  21. calculateSlideX: 0,
  22. scrollbarHide
  23. };
  24. this.state = {
  25. startX: 0,
  26. diffX: 0,
  27. touchStart: false,
  28. touchEnd: false,
  29. touchMove: false,
  30. translateX: 0,
  31. transitionDuring: 300,
  32. ifanimateEnd: false
  33. };
  34. this.buildSwiper();
  35. }
  36. bind() {
  37. let events = ['touchstart', 'touchmove', 'touchend'];
  38. this.addEvent(this.container, events[0], this.touchStart);
  39. this.addEvent(this.container, events[1], this.touchMove);
  40. this.addEvent(this.container, events[2], this.touchEnd);
  41. }
  42. //初始化内容
  43. buildSwiper() {
  44. let {
  45. containerWidth,
  46. children,
  47. slidesPerView,
  48. spaceBetween
  49. } = this.store;
  50. let slideWidth = Math.round(containerWidth / slidesPerView);
  51. for (let i = 0; i < children.length; i++) {
  52. children[i].style.width = slideWidth + 'px';
  53. children[i].style.marginRight = spaceBetween + 'px';
  54. }
  55. //是否有滚动条
  56. if (this.store.scrollbarHide) {
  57. $(this.store.containerId).append(` <div class="swiper-scrollbar">
  58. <div class="swiper-scrollbar-drag"></div>
  59. </div>`);
  60. this.srollbarContainer = document.querySelector('.swiper-scrollbar-drag');
  61. this.scrollbar = document.querySelector('.swiper-scrollbar');
  62. this.store.scrollbarWidth = this.scrollbar.offsetWidth;
  63. this.store.scrollbarDragWidth = this.srollbarContainer.offsetWidth;
  64. setTimeout(() => {
  65. this.store.ableslideX = Number(this.scrollbar.offsetWidth - this.srollbarContainer.offsetWidth);
  66. }, 10);
  67. }
  68. this.bind();
  69. }
  70. //监听touchStart事件
  71. touchStart(e, that) {
  72. if (that.state.touchStart) return;
  73. that.state.startX = e.type === 'touchstart' ? e.targetTouches[0].pageX : e.pageX;
  74. that.state.touchEnd = that.state.touchMove = false;
  75. that.state.touchStart = true;
  76. that.state.diffX = 0;
  77. }
  78. //监听touchMove事件
  79. touchMove(e, that) {
  80. let { startX } = that.state;
  81. if (!that.state.touchStart) return;
  82. that.state.touchMove = true;
  83. let currentX = e.type === 'touchmove' ? e.targetTouches[0].pageX : e.pageX;
  84. if (!that.state.ifanimateEnd) {
  85. that.state.diffX = Math.round(currentX - startX);
  86. that.targetMaxmove();
  87. if(that.store.scrollbarHide){
  88. that.scrollbarMove();
  89. }
  90. }
  91. }
  92. //监听touchEnd事件
  93. touchEnd(e, that) {
  94. let {
  95. touchStart,
  96. touchMove,
  97. touchEnd,
  98. } = that.state;
  99. if (!touchStart || !touchMove || touchEnd) return;
  100. that.state.touchEnd = true;
  101. that.state.touchStart = false;
  102. that.state.ifanimateEnd = false;
  103. that.ifdeadLine();
  104. if(that.store.scrollbarHide){
  105. that.scrollbarInit();
  106. }
  107. setTimeout(() => {
  108. that.transitionDurationEndFn();
  109. }, that.state.transitionDuring);
  110. }
  111. //返回滑动区域的最大值
  112. calculateSlide() {
  113. let {
  114. children,
  115. spaceBetween,
  116. containerWidth
  117. } = this.store;
  118. let slide_max = 0,
  119. totalWidth = 0;
  120. for (let i = 0; i < children.length; i++) {
  121. totalWidth += children[0].offsetWidth + spaceBetween;
  122. }
  123. slide_max = containerWidth + spaceBetween - totalWidth;
  124. return slide_max;
  125. }
  126. //滑动的临界值判断
  127. targetMaxmove() {
  128. let {
  129. diffX
  130. } = this.state;
  131. let currentSlide = this.state.diffX + this.getTranslate(this.wrapper).x;
  132. let rightLine = this.calculateSlide();
  133. if (diffX > 0 && currentSlide > this.store.containerWidth / 2) {
  134. this.state.ifanimateEnd = true;
  135. this.state.translateX = this.store.containerWidth / 2;
  136. } else if (diffX < 0 && currentSlide <= rightLine - this.store.containerWidth / 2) {
  137. this.state.ifanimateEnd = true;
  138. this.state.translateX = rightLine - this.store.containerWidth / 2;
  139. } else {
  140. this.state.translateX = currentSlide;
  141. }
  142. this.recover(this.wrapper, Number(this.state.translateX), 0, 0);
  143. }
  144. //结束时临界值处理
  145. ifdeadLine() {
  146. let {
  147. diffX,
  148. translateX
  149. } = this.state;
  150. let rightLine = this.calculateSlide();
  151. if (diffX > 0) {
  152. this.recover(this.wrapper, 0, 0, 0);
  153. }
  154. if (diffX < 0 && translateX <= rightLine) {
  155. this.recover(this.wrapper, rightLine, 0, 0);
  156. }
  157. }
  158. //滚动条滑动处理
  159. scrollbarMove() {
  160. let { diffX } = this.state;
  161. let { ableslideX } = this.store;
  162. let rightLine = this.calculateSlide();
  163. let radio = Math.abs(diffX) / Math.abs(rightLine);
  164. let scrollX = diffX > 0 ? -radio * ableslideX : radio * ableslideX;
  165. scrollX += this.getTranslate(this.srollbarContainer).x;
  166. this.recover(this.srollbarContainer, scrollX, 0, 0);
  167. }
  168. //结束滚动条处理
  169. scrollbarInit() {
  170. let { ableslideX } = this.store;
  171. let { diffX } = this.state;
  172. let scrollX = this.getTranslate(this.srollbarContainer).x;
  173. if (diffX > 0) {
  174. this.recover(this.srollbarContainer, 0, 0, 0);
  175. }
  176. if (diffX < 0 && scrollX > ableslideX) {
  177. this.recover(this.srollbarContainer, ableslideX, 0, 0);
  178. }
  179. }
  180. recover(container, x, y, z) {
  181. this.transitionDuration(container, this.state.transitionDuring);
  182. this.translate(container, x, y, z);
  183. }
  184. translate(ele, x, y, z) {
  185. this.transform(ele, 'translate3d(' + x + 'px, ' + y + 'px, ' + z + 'px)');
  186. }
  187.  
  188. transform(ele, transform) {
  189. let elStyle = ele.style;
  190. elStyle.webkitTransform = elStyle.MsTransform = elStyle.msTransform = elStyle.MozTransform = elStyle.OTransform = elStyle.transform = transform;
  191. }
  192.  
  193. transitionDuration(ele, time) {
  194. let elStyle = ele.style;
  195. elStyle.webkitTransitionDuration = elStyle.MsTransitionDuration = elStyle.msTransitionDuration = elStyle.MozTransitionDuration = elStyle.OTransitionDuration = elStyle.transitionDuration = time + 'ms';
  196. }
  197.  
  198. transitionDurationEndFn() {
  199. this.transitionDuration(this.wrapper, 0);
  200. }
  201. getTranslate(el) {
  202. let curStyle = window.getComputedStyle(el);
  203. let curTransform = curStyle.transform || curStyle.webkitTransform;
  204. let x, y;
  205. x = y = 0;
  206. curTransform = curTransform.split(', ');
  207. if (curTransform.length === 6) {
  208. x = parseInt(curTransform[4], 10);
  209. y = parseInt(curTransform[5], 10);
  210. }
  211. return {
  212. x,
  213. y
  214. };
  215. }
  216. addEvent(target, type, fn) {
  217. $(document).on(type, target, event => {
  218. if (typeof fn !== 'function') return;
  219. fn(event, this);
  220. }).bind(this);
  221. }
  222. };

  css代码如下:

  1. .bxm-container {
  2. width: 100%;
  3. height: auto;
  4. position: relative;
  5. overflow: hidden;
  6. list-style: none;
  7. padding: 0;
  8. .bxm-wrapper {
  9. position: relative;
  10. width: 100%;
  11. height: 100%;
  12. z-index: 1;
  13. display: flex;
  14. transition-property: transform;
  15. box-sizing: content-box;
  16. .bxm-slide {
  17. text-align: center;
  18. font-size: 18px;
  19. background: #fff;
  20. display: flex;
  21. justify-content: center;
  22. align-items: center;
  23. flex-shrink: 0;
  24. width: 100%;
  25. height: 100%;
  26. box-sizing: border-box;
  27. transition-property: transform;
  28. -webkit-flex-shrink: 0;
  29. -ms-flex-negative: 0;
  30. flex-shrink: 0;
  31. }
  32. }
  33. .swiper-scrollbar {
  34. position: relative;
  35. left: 1%;
  36. bottom: 0px;
  37. z-index: 50;
  38. height: 5px;
  39. width: 98%;
  40. border-radius: 10px;
  41. -ms-touch-action: none;
  42. background: rgba(0, 0, 0, .1);
  43. margin-top: 10px;
  44. overflow: hidden;
  45. .swiper-scrollbar-drag {
  46. height: 100%;
  47. width: 60%;
  48. position: relative;
  49. background: rgba(0, 0, 0, .5);
  50. border-radius: 10px;
  51. left: 0;
  52. top: 0
  53. }
  54. }
  55. }

  具体调用该组件类似于

  1. <div class="bxm-container">
  2. <div class="bxm-wrapper">
  3. <div class="bxm-slide">
  4. <div class="awardImg"><img src="https://buyimg.bianxianmao.com/dist/ACTIVITY/certificate/2018/10/24/04c32df6-13f2-464d-b298-b712e14daa2e"
  5. alt=""></div>
  6. </div>
  7. <div class="bxm-slide">
  8. <div class="awardImg"><img src="https://buyimg.bianxianmao.com/dist/ACTIVITY/certificate/2018/10/24/04c32df6-13f2-464d-b298-b712e14daa2e"
  9. alt=""></div>
  10. </div>
  11. <div class="bxm-slide">
  12. <div class="awardImg"><img src="https://buyimg.bianxianmao.com/dist/ACTIVITY/certificate/2018/10/24/04c32df6-13f2-464d-b298-b712e14daa2e"
  13. alt=""></div>
  14. </div>
  15. <div class="bxm-slide">
  16. <div class="awardImg"><img src="https://buyimg.bianxianmao.com/dist/ACTIVITY/certificate/2018/10/24/04c32df6-13f2-464d-b298-b712e14daa2e"
  17. alt=""></div>
  18. </div>
  19. <div class="bxm-slide">
  20. <div class="awardImg"><img src="https://buyimg.bianxianmao.com/dist/ACTIVITY/certificate/2018/10/24/04c32df6-13f2-464d-b298-b712e14daa2e"
  21. alt=""></div>
  22. </div>
  23. <div class="bxm-slide">
  24. <div class="awardImg"><img src="https://buyimg.bianxianmao.com/dist/ACTIVITY/certificate/2018/10/24/04c32df6-13f2-464d-b298-b712e14daa2e"
  25. alt=""></div>
  26. </div>
  27. </div>
    <script>

  var config = {
    containerId: ‘.bxm-container’,
    slidesPerView: 2.4,
    spaceBetween: 20,
    scrollbarHide: true,
  };

  1.   new CSlide().init(config, temp);
    </script>  

  LZ刚写到快结尾时候被叫到公司去做一个紧急需求,心里一万匹草泥马在奔腾。

  如有不妥,请大佬指正;

 

版权声明:本文为zdf-xue原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/zdf-xue/p/9854102.html