卡片左右滑动 带动任务条滑动 vue

接到一个任务,做一个活动卡片,卡片左右滑动带动卡片上方的任务条走到对应节点:
1、卡片和任务条均为动态生成
2、卡片底部可以左右滑动
3、当一张卡片超过50%的区域展示出来后放手,定位到对应卡片
4、卡片更换完成后,上方任务轴同时更换

这个任务的主要就在数据格式的制定与滑动的判断,不是很难、用vue写的

1.首先写出一个卡片的样式,得到其宽度,获取共有多少张卡片。所有卡片放在一个div里面,div的宽度为(卡片宽度+卡片间距)*卡片数量 width:${cardcount*97}%
2.滑动开始,获取起始位置startX
滑动时,使卡片跟着滑动,transform:translateX(${cardLeft}px),滑动距离超过一个卡片的宽度时,滑动宽度为原宽度+卡片的宽度
3.滑动结束时,获取结束位置endX,得到滑动的距离disX = endX - startX,判断超过卡片的50%,则滑动一个卡片,否则不滑动。如果此时是第一个卡片还要右滑,或者是最后一个卡片还要左滑,则不滑动
4.滑动一个卡片,任务轴传一个变量,改变当前任务点
核心:

      cardLeft: 0, //现移动距离
      startX: 0, //起始位置
      endX: 0, //结束位置
      nowLeft: 0, //移动时距离
      disX: 0, //总移动距离
      cardWidth: 0,//卡片(移动)宽度
    touc
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现Vue卡片左右重叠并且左右滑动,可以使用CSS3的transform属性和transition属性,以及Vue的动态绑定class属性。以下是一个简单的实现思路: 1. 在组件中添加一个卡片容器,设置overflow: hidden属性和white-space: nowrap属性,使卡片可以水平滚动并且超出容器范围的部分被隐藏。 2. 使用flex布局,将卡片设置为均匀分布,并且设置卡片的z-index属性和transform属性,使卡片左右重叠。 3. 在卡片容器上添加touchstart、touchmove、touchend事件的监听函数。 4. 在touchstart事件中记录起始位置的横坐标和当前卡片的索引。 5. 在touchmove事件中计算当前位置的横坐标与起始位置的横坐标的差值,并根据差值设置卡片的transform属性。 6. 在touchend事件中判断差值是否超过一定的阈值,如果是则根据差值的正负来判断卡片是向左滑动还是向右滑动,并根据卡片的位置来设置是否切换到下一个卡片。 7. 使用Vue的动态绑定class属性来根据当前卡片的索引来设置卡片的z-index属性和transform属性,以实现左右重叠效果。 8. 使用CSS3的transition属性实现卡片的平滑过渡效果。 以下是一个简单的代码示例: ```html <template> <div class="card-container" @touchstart="onTouchStart" @touchmove="onTouchMove" @touchend="onTouchEnd"> <div class="card" :style="{ transform: `translateX(${translateX}px)`, zIndex: currentIndex }" :class="{ 'active': currentIndex === index }" v-for="(item, index) in items" :key="index"> {{ item }} </div> </div> </template> <script> export default { data() { return { items: ['Card 1', 'Card 2', 'Card 3', 'Card 4'], currentIndex: 0, startX: 0, translateX: 0 }; }, methods: { onTouchStart(event) { this.startX = event.touches[0].clientX; this.currentIndex = Math.floor(this.startX / (window.innerWidth / this.items.length)); }, onTouchMove(event) { const currentX = event.touches[0].clientX; this.translateX = currentX - this.startX; }, onTouchEnd(event) { const threshold = 50; if (Math.abs(this.translateX) > threshold) { this.currentIndex += this.translateX > 0 ? -1 : 1; if (this.currentIndex < 0) { this.currentIndex = 0; } else if (this.currentIndex >= this.items.length) { this.currentIndex = this.items.length - 1; } } this.startX = 0; this.translateX = 0; } } }; </script> <style scoped> .card-container { display: flex; overflow: hidden; white-space: nowrap; padding: 10px; } .card { flex: 0 0 80%; height: 200px; padding: 20px; background-color: #fff; box-shadow: 0 0 5px rgba(0, 0, 0, 0.3); transition: transform 0.3s ease-out, z-index 0s linear; } .card.active { flex: 0 0 100%; z-index: 2; transform: translateX(0) scale(1.1); } .card.active + .card { z-index: 1; transform: translateX(50%) scale(0.9); } .card.active + .card + .card { z-index: 0; transform: translateX(100%) scale(0.8); } </style> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值