• 如何快速生成一个H5滑动的卡片(单页和分页都有)


     单页

      • class="combo">
    1. <li v-for="(item, index) in arr" :key="index">
    2. <div class="combo-name">{{ item.A }}div>
    3. <div class="combo-price">{{ item.B }}div>
    4. <div class="combo-button" @click="handleImmediatelyData(item, idx)">
    5. 立即办理
    6. div>
    7. li>
  • arr: [
  • {
  • A: "套餐打折",
  • B: "五G套餐专享",
  • },
  • {
  • A: "套餐打折",
  • B: "五G套餐专享",
  • },
  • {
  • A: "套餐打折",
  • B: "五G套餐专享",
  • },
  • {
  • A: "套餐打折",
  • B: "五G套餐专享",
  • },
  • {
  • A: "套餐打折",
  • B: "五G套餐专享",
  • },
  • {
  • A: "套餐打折",
  • B: "五G套餐专享",
  • },
  • {
  • A: "套餐打折",
  • B: "五G套餐专享",
  • },
  • {
  • A: "套餐打折",
  • B: "五G套餐专享",
  • },
  • {
  • A: "套餐打折",
  • B: "五G套餐专享",
  • },
  • ],
  • .combo {
  • width: 100%;
  • // height: 86px;
  • box-shadow: 0px 4px 6px 0px rgba(0, 0, 0, 0.05);
  • border-radius: 0px 0px 0px 0px;
  • opacity: 1;
  • margin-top: 12px;
  • display: -webkit-box;
  • overflow-x: auto;
  • -webkit-overflow-scrolling: touch;
  • .combo::-webkit-scrollbar {
  • display: none;
  • // width: 0px;
  • opacity: 0;
  • }
  • li {
  • display: flex;
  • justify-content: center;
  • align-items: center;
  • flex-direction: column;
  • text-align: center;
  • width: 90px;
  • height: 90px;
  • // background: linear-gradient(320deg, #ffffff 0%, #f9daa4 100%);
  • background-color: #fff;
  • border-radius: 8px 8px 8px 8px;
  • opacity: 1;
  • // border: 1px solid #ffffff;
  • margin-left: 10px;
  • margin-bottom: 5px;
  • &:first-child{
  • margin-left: 0;
  • }
  • .combo-title {
  • line-height: 20px;
  • font-size: 14px;
  • font-weight: 400;
  • color: blue;
  • }
  • .combo-price {
  • font-size: 13px;
  • font-weight: 600;
  • color: #fca418;
  • height: 20px;
  • line-height: 20px;
  • margin-top: 6px;
  • margin-bottom: 6px;
  • }
  • .combo-button {
  • border-radius: 40px 40px 40px 40px;
  • font-size: 12px;
  • background: #fca418;
  • font-weight: 400;
  • color: #fff;
  • line-height: 20px;
  • width: 65px;
  • }
  • }
  • }
  • 当要对滚动做一些处理的时候可以参考下面的代码 

    1. handleScroll(event) {
    2. // 获取滚动的距离
    3. const scrollDistance = event.target.scrollLeft;
    4. 当前视口的宽度
    5. var viewportWidth = window.innerWidth || document.documentElement.clientWidth;
    6. // 获取第一个 li 元素的宽度
    7. const firstItemWidth = this.$refs.comboList.querySelector('.combo li:first-child').clientWidth;
    8. // console.log(scrollDistance,'滚动');
    9. // 判断滚动的距离是否超过第一个盒子的宽度,外边距10*(n-1)+视口内边距20 = 30+20
    10. if (scrollDistance >= 4*firstItemWidth-viewportWidth+50) {
    11. console.log(scrollDistance,viewportWidth-20,4*firstItemWidth,'滚动距离超过第一个盒子的宽度!');
    12. // 在这里进行你的打印操作或其他操作
    13. }
    14. },

    分页

    这里以vant的轮播图组件

     结构

    1. class="combo-swiper">
    2. <van-swipe class="my-swipe" indicator-color="white" :loop="false" v-if="info.event.length">
    3. <van-swipe-item v-for="(slice, sliceIndex) in comboArrChunks" :key="sliceIndex">
    4. <div class="combo-swiper-box">
    5. <div v-for="(item, itemIndex) in slice" :key="itemIndex" class="swiper-son">
    6. <div class="li-title">{{ item.spec }}div>
    7. <div class="li-text">{{ item.activityName }}div>
    8. <div class="li-bnt" @click="handleImmediatelyData(item, sliceIndex * 3 + itemIndex)">
    9. 立即办理
    10. div>
    11. div>
    12. div>
    13. van-swipe-item>
    14. van-swipe>

     样式

    1. .combo-swiper{
    2. width: 100%;
    3. .my-swipe .van-swipe-item {
    4. width: 100%;
    5. height: 100px;
    6. color: #fff;
    7. font-size: 20px;
    8. line-height: 150px;
    9. text-align: center;
    10. margin-bottom: 20px;
    11. margin-top: 10px;
    12. .combo-swiper-box{
    13. display: flex;
    14. // justify-content: center;
    15. align-items: center;
    16. flex-wrap: wrap;
    17. flex-direction: row;
    18. .swiper-son{
    19. width: 100px;
    20. height: 90px;
    21. margin-right: 10px;
    22. padding: 5px;
    23. border-radius: 8px;
    24. background-color: #fff;
    25. text-align: center;
    26. &:nth-child(3){
    27. margin-right:0;
    28. }
    29. .li-title{
    30. font-size: 14px;
    31. margin-top: 5px;
    32. line-height: 20px;
    33. font-weight: bold;
    34. color: #575757;
    35. padding-top: 2px;
    36. }
    37. .li-text {
    38. font-size: 12px;
    39. margin: 5px 0px;
    40. height: 20px;
    41. line-height: 20px;
    42. color: #fca418;
    43. }
    44. .li-bnt {
    45. width: 80%;
    46. height: 18px;
    47. font-size: 12px;
    48. line-height: 18px;
    49. padding: 2px 6px;
    50. margin:0 auto;
    51. background: #fca418;
    52. color: #fff;
    53. border-radius: 40px 40px 40px 40px;
    54. }
    55. }
    56. }
    57. }
    58. }

     方法函数

    1. data(){
    2. return{
    3. comboArr: [
    4. { spec: 'Spec A', activityName: 'Activity 1' },
    5. { spec: 'Spec B', activityName: 'Activity 2' },
    6. { spec: 'Spec C', activityName: 'Activity 3' },
    7. { spec: 'Spec D', activityName: 'Activity 4' },
    8. { spec: 'Spec E', activityName: 'Activity 5' }
    9. ],
    10. itemsPerSwipe: 3, // 每个 包含的数据数量
    11. },
    12. computed: {
    13. comboArrChunks() {
    14. const chunks = [];
    15. if (this.info.event) {
    16. for (let i = 0; i < this.comboArr.length; i += this.itemsPerSwipe) {
    17. chunks.push(this.comboArr.slice(i, i + this.itemsPerSwipe));
    18. }
    19. }
    20. return chunks;
    21. },
    22. },
    23. methods:{
    24. handleImmediatelyData(item, index){
    25. console.log(item,index);
    26. //需要处理的逻辑
    27. }
    28. }

    计算属性用于将原始的 comboArr 数组切分成多个子数组,每个子数组包含 itemsPerSwipe 个元素。这样可以确保每个轮播项内都有相同数量的组合套餐信息。这个方法返回一个包含子数组的数组,用于在轮播组件中进行循环渲染。

  • 相关阅读:
    1x1卷积的作用
    Logback日志框架使用详解以及如何Springboot快速集成
    为什么你的项目总延期?多半是没做好5件事
    logback.xml配置详解
    Java学习第十九节之static关键字详解
    webpack5 (四)
    深入探索JVM高效并发 — Java内存模型(四) 先行发生原则
    winsw 注册的java服务jps process information unavailable
    08、SpringCloud之SpringBoot-Admin监控组件学习笔记
    零时科技 || EGD被黑客攻击损失超3.6万 BUSD,事件分析
  • 原文地址:https://blog.csdn.net/weixin_53818172/article/details/132611357