移动端— Touch事件轮播图

约定不等于承诺〃 2023-10-11 13:16 80阅读 0赞

   虽然 以前也写过手机端页面 。当时用的jquery moblie 框架。啥也不懂 就知道复制粘贴出效果 不敢改内部样式。现在呢 了解手机端原理 一些基本的概念 视口 缩放 后 。再去想以前写的页面 套框架 显然得心应手了不少。

手机移动端轮播 原生js

代码:

  1. let banner = document.querySelector('.jd_banner')
  2. let imgbox = banner.querySelector('ul:first-of-type')//第一个ul
  3. let first = imgbox.querySelector('li:first-of-type')
  4. let last = imgbox.querySelector('li:last-of-type')
  5. //插入
  6. // 最后的位置 //复制一个
  7. imgbox.appendChild(first.cloneNode(true))
  8. //开始的位置
  9. imgbox.insertBefore(last.cloneNode(true), imgbox.firstChild)
  10. let lilist = imgbox.children
  11. let bannerWidth = banner.offsetWidth;
  12. imgbox.style.width = lilist.length * bannerWidth + 'px'
  13. for (let i = 0; i < lilist.length; i++) {
  14. lilist[i].style.width = imgbox.offsetWidth / lilist.length + 'px'
  15. }
  16. imgbox.style.left = -bannerWidth + 'px'
  17. let index = 1;
  18. //屏幕大小改变
  19. window.onresize = function () {
  20. bannerWidth = banner.offsetWidth;
  21. imgbox.style.width = lilist.length * bannerWidth + 'px'
  22. for (let i = 0; i < lilist.length; i++) {
  23. lilist[i].style.width = imgbox.offsetWidth / lilist.length + 'px'
  24. }
  25. imgbox.style.left = (-index * bannerWidth) + 'px'
  26. }
  27. let bannerIndex = document.querySelector('.jd_bannerIndex')
  28. function active(i) {
  29. for (let j = 0; j < bannerIndex.children.length; j++) {
  30. bannerIndex.children[j].className = ''
  31. }
  32. bannerIndex.children[i].className = 'active'
  33. }
  34. function banntime(){
  35. index++
  36. imgbox.style.transition = 'left 0.5s ease-in-out'
  37. imgbox.style.left = (-index * bannerWidth) + 'px'
  38. if (index == lilist.length - 1) {
  39. active(0)
  40. index = 1
  41. setTimeout(function () {
  42. imgbox.style.transition = 'none'
  43. active(index - 1)
  44. imgbox.style.left = (-index * bannerWidth) + 'px'
  45. }, 500)
  46. }
  47. active(index - 1)
  48. }
  49. let bannerInt = setInterval(banntime, 2000)
  50. let startX, moveX, distanceX;
  51. imgbox.addEventListener('touchstart', function (e) {
  52. startX = e.targetTouches[0].clientX
  53. clearInterval(bannerInt)
  54. })
  55. imgbox.addEventListener('touchmove', function (e) {
  56. moveX = e.targetTouches[0].clientX
  57. distanceX = moveX - startX
  58. imgbox.style.left = (-index * bannerWidth + distanceX) + 'px'
  59. })
  60. imgbox.addEventListener('touchend', function (e) {
  61. imgbox.style.transition = 'left 0.5s ease-in-out'
  62. let drX = distanceX % bannerWidth
  63. if (Math.abs(drX) > bannerWidth / 2) {
  64. if (drX > 0) {
  65. index--
  66. } else {
  67. index++
  68. }
  69. if (index == 0) {
  70. index = 8
  71. imgbox.style.transition = 'none'
  72. }
  73. if (index == lilist.length - 1) {
  74. index = 1
  75. imgbox.style.transition = 'none'
  76. }
  77. }
  78. active(index - 1)
  79. imgbox.style.left = (-index * bannerWidth) + 'px'
  80. bannerInt=setInterval(banntime,2000)
  81. })

  效果:

797834-20190522095739376-1696498438.gif

总结 :

不管什么框架库,还是基本很重要 了解原理 才能得心应手

转载于:https://www.cnblogs.com/ruogu/p/10904134.html

发表评论

表情:
评论列表 (有 0 条评论,80人围观)

还没有评论,来说两句吧...

相关阅读