Sample code for implementing horizontal infinite scrolling with pure CSS3

Sample code for implementing horizontal infinite scrolling with pure CSS3

The examples in this article are all written in small programs, but this does not affect the functions to be implemented.

wxml

Make one more copy of the box containing the picture so that the beginning and end of the looped picture are connected

<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

The key to adjusting the speed is that the animation time is dynamically controlled by the number of looped items.

Page({
  data: {
    images: new Array(4),
    computedAni: ''
  },
  onLoad: function () {
    this.setAniSpeed(this.data.images.length)
  },
  setAniSpeed(num) {
    let time = Math.ceil(num / 5 * 15) // Here, the animation time is 15s when there are 5 pictures. You can adjust it yourself this.setData({
      computedAni: `${time}s rowup linear infinite normal`
    })
  }
})

Click here to view the code snippet

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

This concludes this article about the sample code for implementing horizontal infinite scrolling with pure CSS3. For more relevant CSS3 horizontal infinite scrolling content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

<<:  Double loading issue when the page contains img src

>>:  How to use the href attribute of the HTML a tag to specify relative and absolute paths

Recommend

Summary of webpack's mobile adaptation solution

Table of contents rem vw Adapt to third-party UI ...

How to build a React project with Vite

Table of contents Preface Create a Vite project R...

Experience in solving tomcat memory overflow problem

Some time ago, I submitted a product version to t...

Detailed explanation of the platform bus of Linux driver

Table of contents 1. Introduction to platform bus...

Let’s talk in detail about how browsers view closures

Table of contents Preface Introduction to Closure...

CentOS 8.0.1905 installs ZABBIX 4.4 version (verified)

Zabbix Server Environment Platform Version: ZABBI...

Docker+nacos+seata1.3.0 installation and usage configuration tutorial

I spent a day on it before this. Although Seata i...

Avoid abusing this to read data in data in Vue

Table of contents Preface 1. The process of using...

MySQL parameter related concepts and query change methods

Preface: In some previous articles, we often see ...

How to import and export Docker images

This article introduces the import and export of ...

Solution to the problem that input in form cannot be submitted when disabled

I wrote a test program before, in which adding and...

Dockerfile text file usage example analysis

Dockerfile is a text file used to build an image....