WeChat applet custom scroll-view example code

WeChat applet custom scroll-view example code

Mini Program Custom Scroll-View Scroll Bar

Without further ado, here are the renderings

Rendering

wxml code

 <scroll-view scroll-x 
  class="scroll-view" 
  bindscroll="bindScroll">
 <block wx:for="{{arr}}" wx:key="index">
 <view class="scroll-item">scroll-view{{index}}</view> 
 </block>
 </scroll-view>
 
 <!-- Scrollbar -->
 <view class="slide">
 <view class='slide-bar'>
 <view class="slide-action" 
  style="width:{{slideWidth}}rpx; margin-left:{{slideLeft<=1 ? 0 : slideLeft+'rpx'}};">
 </view>
 </view>
 </view>
 

wxss code

 page{
 height: 100vh;
 background: rgb(111, 80, 65)
 }
 .scroll-view{
 display: flex;
 width: 100%;
 white-space: nowrap;
 padding-top: 20rpx;
 } 

 .scroll-item:nth-child(1){
 margin-left: 40rpx;
 } 

 .scroll-item {
 display: inline-block;
 width: 550rpx;
 height: 463rpx;
 background: rgba(199, 180, 165);
 border-radius: 20rpx;
 margin-right: 30rpx;
 color: #fff;
 } 

 .slide{
 background:rgb(111, 80, 65);
 width:100%;
 padding-top:20rpx;
 }
 .slide .slide-bar{
 width:180rpx;
 margin:0 auto;
 height: 4rpx;
 background: rgba(255,255,255,.2);
 } 

 .slide .slide-bar .slide-action{
 height:100%;
 background:#fff;
 } 

js code

 /**
 * Initial data of the page */
 data: {
 arr: 10,
 slideWidth: '',
 slideLeft: ''
 },

 /**
 * Life cycle function--listen for page loading*/
 onLoad: function (options) {
 // Calculate the ratio this.calcRatio();
 },

 /**
 * Calculate the ratio */
 calcRatio() {
 var windowWidth = wx.getSystemInfoSync().windowWidth;
 // Calculate the total length of the list var totalLength = (this.data.arr * 580) + 40;
 // Calculate the ratio of the slider var slideRatio = 180 / totalLength * (750 / windowWidth);
 /**
 * Total screen length / total list length = ratio of slider to scroll bar length * ratio of slider to scroll bar length * length of scroll list = width of slider */
 var sliderWidth = 750 / totalLength * 180;
 this.setData({
 slideWidth: sliderWidth,
 totalLength: totalLength,
 slideRatio: slideRatio
 })
 },

 /**
 * Monitor scrolling */
 bindScroll(e) {
 this.setData({
 slideLeft: e.detail.scrollLeft * this.data.slideRatio
 })
 },

Appendix: scroll-view scrollable view area

Summarize

This is the end of this article about WeChat mini-program custom scroll-view. For more relevant WeChat mini-program custom scroll-view content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • WeChat applet implements a simple calculator
  • WeChat applet + mqtt, esp8266 temperature and humidity reading implementation method
  • WeChat applet to achieve the revolving lantern effect example
  • WeChat Mini Programs Achieve Seamless Scrolling
  • C language to achieve the whole process of recording the minesweeper game
  • Java simple game production code
  • Implementing a puzzle game with js
  • C# implements a simple flying chess game
  • Implementing the Snake Game in C Language under Linux
  • How to create WeChat games with CocosCreator

<<:  Detailed steps for quick installation of openshift

>>:  Summary of common problems in downloading and installing MySQL 5.7 on Win7 64-bit

Recommend

Solve the problem of ugly blue border after adding hyperlink to html image img

HTML img produces an ugly blue border after addin...

Text pop-up effects implemented with CSS3

Achieve resultsImplementation Code html <div&g...

Linux system file sharing samba configuration tutorial

Table of contents Uninstall and install samba Cre...

WeChat Mini Program video barrage position random

This article shares the specific code for randomi...

Two implementation solutions for vuex data persistence

Table of contents Business requirements: Solution...

How to clear the cache after using keep-alive in vue

What is keepalive? In normal development, some co...

HTML uses regular expressions to test table examples

Here is an example code for using regular express...

How to build sonarqube using docker

Table of contents 1. Install Docker 2. Install so...

Implementing a puzzle game with js

This article shares the specific code of js to im...

Summary of four ways to introduce CSS (sharing)

1. Inline reference: used directly on the label, ...

Detailed explanation of MySQL master-slave replication process

1. What is master-slave replication? The DDL and ...

Docker implements re-tagging and deleting the image of the original tag

The docker image id is unique and can physically ...