当前位置:主页 > 查看内容

纯css3实现横向无限滚动的示例代码

发布时间:2021-04-19 00:00| 位朋友查看

简介:本文示例都是用小程序写的,但是不影响要实现的功能。 wxml 装图片的盒子多复制一份,让循环图片的首尾相接 view class=scrollbox dis-flex view class=imgItem dis-flex style=animation: {{computedAni}}; image src=../img/{{index + 1}}.jpg wx:for={{im……

本文示例都是用小程序写的,但是不影响要实现的功能。

wxml

装图片的盒子多复制一份,让循环图片的首尾相接

<view class="scrollbox dis-flex">
    <view class="imgItem dis-flex" style="animation: {{computedAni}};">
      <image src="../img/{{index + 1}}.jpg" wx:for="{{images}}" mode="aspectFill" wx:key="index"></image>
    </view>
    <view class="imgItem dis-flex" style="animation: {{computedAni}};">
      <image src="../img/{{index + 1}}.jpg" wx:for="{{images}}" mode="aspectFill" wx:key="index"></image>
    </view>
</view>

wxss

.dis-flex {
  display: flex;
  display: -webkit-flex;
}
.scrollbox {
  margin: 30px;
  text-align: center;
  border: 1px solid blue;
  height: 220rpx;
  align-items: center;
  overflow: hidden;
}
.imgItem {
  animation: 24s rowup linear infinite normal;
}
.imgItem image {
  width: 200rpx;
  height: 200rpx;
  margin: 0 20rpx;
}
@keyframes rowup {
  0% {
      -webkit-transform: translate3d(0, 0, 0);
      transform: translate3d(0, 0, 0);
  }
  100% {
      -webkit-transform: translate3d(-100%, 0, 0);
      transform: translate3d(-100%, 0, 0);
  }
}
@-webkit-keyframes rowup {
  0% {
      -webkit-transform: translate3d(0, 0, 0);
      transform: translate3d(0, 0, 0);
  }
  100% {
    -webkit-transform: translate3d(-1000px, 0, 0);
    transform: translate3d(-1000px, 0, 0);
  }
}

js

调整速度的关键就在,动画的时间是由循环的项目个数动态控制的

Page({
  data: {
    images: new Array(4),
    computedAni: ''
  },
  onLoad: function () {
    this.setAniSpeed(this.data.images.length)
  },
  setAniSpeed (num) {
    let time = Math.ceil(num / 5 * 15) // 这里是以5张图片的时候,动画时间15s为基准,可以自己调节
    this.setData({
      computedAni: `${time}s rowup linear infinite normal`
    })
  }
})

点击这里可以查看代码片段

https://developers.weixin.qq.com/s/4gGngEm67Zlh

到此这篇关于纯css3实现横向无限滚动的示例代码的文章就介绍到这了,更多相关css3横向无限滚动内容请搜索脚本之家以前的文章或继续浏览下面的相关文章,希望大家以后多多支持脚本之家!


原文链接:https://m.jb51.net/css/751236.html
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文


随机推荐