vue中的滚动事件以及requestAnimationFrame动画

接着上一章将,这一章主要讲编程时遇到的问题:
scroll事件:
scroll事件
我在vue中是这样使用的object.addEventListener(‘scroll’, e => {}, false);结果发现不行,于是查了很多资料,最后有3种解决方案:
①在节点上使用οnscrοll=“function”
②在节点上使用@scroll事件
③object.addEventListener的第三个参数改为true,即object.addEventListener(‘scroll’, e => {}, true)
我使用的是第三种方案,因为更加灵活。
selector[::before|::after] {content: attr();}
我们可以通过这种方式减少dom的层级,增加单个标签的作用。
requestAnimationFrame
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
// 保证requestAnimationFrame的兼容性
window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现图片无缝循环滚动,可以使用 Vue 的过渡动画和 CSS3 的 transform 属性。具体实现步骤如下: 1. 在 Vue 的模板使用 v-for 指令渲染图片列表,并添加一个包裹元素用于容纳图片。 2. 使用 CSS3 的 transform 属性将图片容器平移,使得第一张图片刚好显示在容器。 3. 使用 Vue 的过渡动画,在图片容器添加一个过渡组件,用于实现图片的滑动效果。 4. 使用定时器或者 requestAnimationFrame() 方法,定时更新图片容器的 transform 属性,实现图片循环滚动。 5. 为图片容器添加 touchstart、touchmove 和 touchend 事件,实现手动滑动滚动的效果。 下面是一个简单的实现示例: ```html <template> <div class="image-carousel"> <transition-group name="carousel-slide" tag="div"> <div class="image-item" v-for="(image, index) in images" :key="index"> <img :src="image" alt=""> </div> </transition-group> </div> </template> <script> export default { data() { return { images: [ 'image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg', 'image5.jpg' ], currentIndex: 0, startX: 0, startY: 0, endX: 0, endY: 0 } }, mounted() { this.startCarousel() this.initTouchEvent() }, methods: { startCarousel() { setInterval(() => { this.currentIndex = (this.currentIndex + 1) % this.images.length }, 3000) }, initTouchEvent() { const imageCarousel = this.$el imageCarousel.addEventListener('touchstart', e => { if (e.touches.length === 1) { this.startX = e.touches[0].clientX this.startY = e.touches[0].clientY } }) imageCarousel.addEventListener('touchmove', e => { if (e.touches.length === 1) { this.endX = e.touches[0].clientX this.endY = e.touches[0].clientY e.preventDefault() } }) imageCarousel.addEventListener('touchend', e => { const dx = this.endX - this.startX const dy = this.endY - this.startY if (Math.abs(dx) > Math.abs(dy) && Math.abs(dx) > 20) { if (dx > 0) { // 向右滑动 this.currentIndex = (this.currentIndex - 1 + this.images.length) % this.images.length } else { // 向左滑动 this.currentIndex = (this.currentIndex + 1) % this.images.length } } }) } }, computed: { transformStyle() { const translateX = -this.currentIndex * 100 return { transform: `translateX(${translateX}%)` } } } } </script> <style> .image-carousel { position: relative; overflow: hidden; width: 100%; height: 300px; } .carousel-slide-enter-active, .carousel-slide-leave-active { transition: transform 0.5s; } .carousel-slide-enter, .carousel-slide-leave-to { transform: translateX(100%); position: absolute; } .image-item { display: inline-block; width: 100%; height: 100%; vertical-align: top; } </style> ``` 在上面的代码使用了一个计算属性 transformStyle 来动态计算图片容器的 transform 属性,实现了自动循环滚动的效果。同时,使用了 touchstart、touchmove 和 touchend 事件来实现手动滑动滚动的效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值