轮播----无缝滚动轮播

目标效果:

1:图片自动的滚动图片,依次显示。

2:鼠标悬停图片上能暂停滚动,离开继续滚动。

原理:绝对定位了的元素可以运动(不占用标准流)。利用setInterval()定时器,让整个运动区域每一段时间调用函数,往左(或右,上,下)移动一段距离,让图片动起来。

注意:运动区域一定要比显示区域大。好比:排队的人群,一个个的在窗口照相,依次显示;

           记得要隐藏溢出的区域。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
可以使用scroll-view和定时器结合实现类似轮播图的无缝滚动。具体步骤如下: 1. 在scroll-view中放置多张图片或者其他需要滚动的内容。 2. 使用定时器,定时调用scroll-view的scrollLeft或scrollTop方法,实现滚动效果。 3. 当滚动到最后一张图片时,将scroll-view的scrollLeft或scrollTop设置为0,实现无缝滚动。 4. 在滚动过程中,需要监听scroll-view的scroll事件,当滚动到一定位置时,自动调整滚动位置,以实现无缝滚动效果。 下面是一个示例代码,可以参考实现: ```html <scroll-view class="swiper" scroll-x="{{true}}" style="width:100%;height:300rpx;white-space: nowrap;" bindscroll="onScroll"> <block wx:for="{{imageUrls}}" wx:key="index"> <image class="swiper-item" src="{{item}}" mode="widthFix"></image> </block> </scroll-view> ``` ```javascript Page({ data: { imageUrls: [ 'https://example.com/1.jpg', 'https://example.com/2.jpg', 'https://example.com/3.jpg', 'https://example.com/4.jpg' ], currentIndex: 0 // 当前显示的图片索引 }, onLoad() { // 启动定时器,每隔3秒滚动一次 this.timer = setInterval(() => { this.scrollNext() }, 3000) }, // 滚动到下一张图片 scrollNext() { const { currentIndex, imageUrls } = this.data const nextIndex = currentIndex + 1 >= imageUrls.length ? 0 : currentIndex + 1 this.setData({ currentIndex: nextIndex }) // 滚动到下一张图片 this.scrollToIndex(nextIndex) }, // 滚动到指定图片 scrollToIndex(index) { const query = wx.createSelectorQuery() query.select(`.swiper-item-${index}`).boundingClientRect() query.select('.swiper').boundingClientRect() query.exec(res => { const itemRect = res[0] const swiperRect = res[1] if (!itemRect || !swiperRect) { return } const offsetLeft = itemRect.left - swiperRect.left this.setData({ scrollLeft: offsetLeft }) }) }, // 监听scroll事件,在滚动到最后一张图片时,自动调整滚动位置 onScroll(e) { const { scrollLeft } = e.detail const { imageUrls } = this.data const lastIndex = imageUrls.length - 1 const lastItemRect = wx.createSelectorQuery().select(`.swiper-item-${lastIndex}`).boundingClientRectSync() if (!lastItemRect) { return } if (scrollLeft >= lastItemRect.left) { // 滚动到最后一张图片,重新滚动到第一张图片 this.scrollToIndex(0) } }, onUnload() { // 关闭定时器 clearInterval(this.timer) } }) ``` 注意,上面的示例代码仅供参考,实际开发中还需要考虑更多细节,例如:滚动动画、手势滑动等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值