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

Mini Program to Implement Slider Effect

This article example shares the specific code for...

Detailed steps for running springboot project in Linux Docker

Introduction: The configuration of Docker running...

CSS to achieve the small sharp corner effect of bubbles

Effect picture (the border color is too light, pu...

Introducing icons by implementing custom components based on Vue

Preface In project development, there are many wa...

The whole process of configuring hive metadata to MySQL

In the hive installation directory, enter the con...

Detailed explanation of long transaction examples in MySQL

Preface: The "Getting Started with MySQL&quo...

MySQL online DDL tool gh-ost principle analysis

Table of contents 1. Introduction 1.1 Principle 1...

UTF-8 and GB2312 web encoding

Recently, many students have asked me about web p...

The difference between datatime and timestamp in MySQL

There are three date types in MySQL: date(year-mo...

CSS and HTML and front-end technology layer diagram

Front-end technology layer (The picture is a bit e...

Specific usage of textarea's disabled and readonly attributes

disabled definition and usage The disabled attrib...

Detailed explanation of ECharts mouse event processing method

An event is an action performed by the user or th...

Discussion on the problem of garbled characters in iframe page parameters

I encountered a very unusual parameter garbled pro...