• vue3手写一个轮播图


    1. <template>
    2. <div>
    3. <div class="swiper_container">
    4. <div :class="index==0||index==-1?'noAnimate':'animate'" class='swiper_content' ref="swiperRef" :style="{transform:`translateX(${scrollWin}px)`}">
    5. <div class="swiper_item" v-for="(item,index) in imgs" :key="index">
    6. <img :src="item.img" alt="">
    7. </div>
    8. </div>
    9. <div class="dots_wrap">
    10. <span :class="index==idx?'active':''" class="dot" v-for="(item,idx) of imgs1.length" :key="idx"></span>
    11. </div>
    12. <span class="left_btn" @click="clickLeft">left</span>
    13. <span class="right_btn" @click="clickRight">right</span>
    14. </div>
    15. </div>
    16. </template>
    17. <script lang="ts" setup>
    18. import { ref, reactive, toRefs, onMounted, } from 'vue'
    19. const swiperRef=ref(null)
    20. let w=ref(800)
    21. let scrollWin=ref(0)
    22. const imgs1=[
    23. {id:"001",img:new URL("../assets/imgs/1.png",import.meta.url).href},
    24. {id:"002",img:new URL("../assets/imgs/2.png",import.meta.url).href},
    25. {id:"003",img:new URL("../assets/imgs/3.png",import.meta.url).href},
    26. {id:"004",img:new URL("../assets/imgs/4.png",import.meta.url).href},
    27. {id:"005",img:new URL("../assets/imgs/5.png",import.meta.url).href},
    28. ]
    29. let imgs=ref([...imgs1])
    30. // imgs.value.push(imgs1[0],imgs1[1])
    31. const firstimg=imgs.value[0]
    32. const lastimg=imgs.value[imgs.value.length-1]
    33. onMounted(() => {
    34. })
    35. const index=ref(0)
    36. const clickLeft=()=>{
    37. index.value--;
    38. console.log("index.value",index.value)
    39. if(index.value<=-1){
    40. scrollWin.value=-w.value*imgs.value.length
    41. console.log("w.value",w.value)
    42. console.log("imgs.value",imgs.value.length)
    43. console.log("scrollWin.value",scrollWin.value)
    44. setTimeout(()=>{
    45. index.value=4
    46. scrollWin.value=-index.value*w.value;
    47. },0)
    48. }else{
    49. scrollWin.value=-index.value*w.value;
    50. }
    51. }
    52. const clickRight=()=>{
    53. index.value++;
    54. console.log("index.value",index.value)
    55. console.log("index.value",index.value)
    56. if(index.value>=5){
    57. setTimeout(()=>{
    58. imgs.value.push(firstimg)
    59. console.log("imgs---prev",imgs)
    60. index.value=0;
    61. scrollWin.value=0
    62. console.log("imgs--next",imgs)
    63. },200)
    64. }
    65. console.log("ndex.value*w.value",index.value,w.value)
    66. scrollWin.value=-index.value*w.value;
    67. // swiperRef.value.style.transform=`translateX(${w}px)`
    68. // debugger
    69. }
    70. </script>
    71. <style scoped>
    72. *{
    73. margin: 0;
    74. padding: 0;
    75. }
    76. .swiper_container{
    77. width: 800px;
    78. height: 320px;
    79. margin: 0 auto;
    80. position: relative;
    81. overflow: hidden;
    82. }
    83. .swiper_content{
    84. display: flex;
    85. position: absolute;
    86. top:0;
    87. left:0;
    88. height: 100%;
    89. }
    90. .dots_wrap{
    91. position: absolute;
    92. bottom: 0;
    93. width: 50%;
    94. left:50%;
    95. transform: translate(-50%,-50%);
    96. text-align: center;
    97. }
    98. .swiper_item{
    99. width: 800px;
    100. margin: 0;
    101. }
    102. .swiper_item img{
    103. width: 100%;
    104. height: 100%;
    105. object-fit: cover;
    106. }
    107. .dot{
    108. display: inline-block;
    109. width: 20px;
    110. height: 20px;
    111. border-radius: 50%;
    112. z-index: 1;
    113. background: #fff;
    114. margin: 0 10px;
    115. }
    116. .active{
    117. background: #f90;
    118. }
    119. .left_btn,.right_btn{
    120. display: inline-block;
    121. width: 60px;
    122. height: 42px;
    123. background: #000;
    124. color:#fff;
    125. z-index: 1;
    126. display: flex;
    127. align-items: center;
    128. justify-content: center;
    129. cursor: pointer;
    130. }
    131. .left_btn{
    132. position: absolute;
    133. top:50%;
    134. left:20px;
    135. }
    136. .right_btn{
    137. position: absolute;
    138. top:50%;
    139. right:20px
    140. }
    141. .noAnimate{
    142. transition: none;
    143. }
    144. .animate{
    145. transition: all 0.2s;
    146. }
    147. .active{
    148. color:#f00
    149. }
    150. </style>

     

     在第一张图片和最后一张图片无缝切换的思路就是在原来数据的尾部添加一张第一张图片,然后通过scrollWin.value=0迅速让整个container回到left为0的位置。为了避免此时第二张图大幅度回滚效果,我们先把transition动画取消,也就是通过:class="index==0||index==-1?'noAnimate':'animate'" 来控制。先不让其有动画,回到初始位置以后再进行动画设置

  • 相关阅读:
    c++中的内存分区模型
    APEX数据源加载实现Excel表数据导入及自定义存储过程
    常见的开源规则引擎简介
    Python算法——树的子树
    【校招VIP】互联网校招项目&实习对项目的要求不重要?大错特错!你忽略掉的项目考察重点都在这里!
    实战级详解Spring框架中引入阿里开源组件Nacos作配置中心
    死锁和死锁的处理
    判断num是不是2的某次方
    【Elasticsearch索引】索引基础操作
    数据结构 哈希表
  • 原文地址:https://blog.csdn.net/baidu_41601048/article/details/133240148