Vue uses better-scroll to achieve horizontal scrolling method example

Vue uses better-scroll to achieve horizontal scrolling method example

1. Implementation principle of scrolling

The scrolling principle of better-scroll is the same as the native scrolling principle of the browser. When the height of the child box is greater than the height of the parent box, vertical scrolling will occur; similarly, if the width of the child box is greater than the width of the parent box, horizontal scrolling will occur.

2. Better-scroll HTML structure

Let's first look at the common HTML structure of better-scroll:

<div class="wrapper">
  <ul class="content">
    <li>...</li>
    <li>...</li>
    ...
  </ul>
</div>

BetterScroll is applied to the outer wrapper container, and the scrolling part is the content. Note that BetterScroll handles scrolling of the first child element (the content) of the container ( wrapper) by default, which means that other elements are ignored.

3. Using better-scroll in Vue

npm install better-scroll --save //npm install import BScroll from 'better-scroll' //Introduce better-scroll in the component file
<template>
   /* Horizontal scroll */
   /* This is the parent box */
<div
        class="wrapper_box"
        style="min-height:100vh;
        "
        ref="wrapper"
        v-else
      >
      /* This is the subbox, the scrolling area*/
        <div class="content" ref="wrapperChild">
          <div
            v-for="(item, index) in currentImgList"
            :key="index"
            class="imgItem"
          >
            <img :src="item.img" class="img" style="margin: 0 10px;" />
          </div>
        </div>
      </div>
</template>
<script>
 import BScroll from "better-scroll";
 export default {
  data() {
    return {
      currentImgList: [
        { img: require("../../assets/image/zzb_1.png") },
        { img: require("../../assets/image/zzb_2.png") },
        { img: require("../../assets/image/zzb_3.png") }
      ],
    };
  },
  mounted() {
    this.slide_x(); //Horizontal scroll},
  methods: {
     // Initialization_initScroll() {
      if (!this.scroll) {
        this.scroll = new BScroll(this.$refs.wrapper, { // Instantiate BScroll and accept two parameters, the first one is the DOM node of the parent box startX: 0, /// For detailed configuration information, please refer to the official documentation of better-scroll, which will not be repeated here click: true,
          scrollX: true,
          scrollY: false, // Ignore vertical scrolling eventPassthrough: "vertical",
          useTransition: false // Prevent abnormal rebound triggered by fast sliding});
      } else {
        this.scroll.refresh(); //If the DOM structure changes, call this method to recalculate to ensure the normal scrolling effect}
    },
    // Calculate width_calculateWidth() {
      // Get the tag with class name imgItem let rampageList = this.$refs.wrapperChild.getElementsByClassName(
        "imgItem"
      );
      // Set a starting width let initWidth = 0;
      // Traverse the labels for (let i = 0; i < rampageList.length; i++) {
        const item = rampageList[i];
        // Add each label width initWidth += item.clientWidth;
      }
      // Set the scrollable width this.$refs.wrapperChild.style.width = `${initWidth}px`;
    },
    slide_x() {
      this.$nextTick(() => { //this.$nextTick is an asynchronous function, to ensure that the DOM has been rendered this._initScroll(); // Initialization this._calculateWidth(); // Calculate width });
    },
    },
   

};
</script>

Here is what the original plugin author said:

4. Points where problems may occur:

  1. You must wait until the page DOM rendering is complete before executing the BScroll instantiation. It is recommended to execute it in the mounted hook function.
  2. The width of the child box is greater than the width of the parent box

at last

This is an article by the author of the better-scroll plugin. I found that better-scroll is really powerful!

When better-scroll meets Vue

Implementing horizontal scrolling with better-scroll in Vue

This is the end of this article about using better-scroll in vue to achieve horizontal scrolling. For more relevant content about using better-scroll in vue to achieve horizontal scrolling, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Vue implements horizontal scrolling of marquee style text
  • Using better-scroll component in Vue to realize horizontal scrolling function
  • Vue replaces the marquee tag to scroll horizontally when the text exceeds the width
  • The problem and solution of setInterval not being cleared when messages scroll horizontally in Vue
  • Vue.js+cube-ui (Scroll component) realizes a horizontal scrolling navigation bar similar to the headline effect
  • Vue sample code to achieve the effect of up and down scrolling of bulletin board text
  • Vue monitors scroll events to implement scroll monitoring
  • Vue.js practice: implementing dynamic anchor points by listening to scroll events
  • Sample code for Vue to achieve seamless scrolling effect of messages
  • Vue realizes horizontal screen scrolling announcement effect

<<:  Reasons and solutions for failure to insert emoji expressions in MySQL

>>:  Summary of Docker common commands and tips

Recommend

Detailed usage of kubernetes object Volume

Overview Volume is the abstraction and virtualiza...

mysql5.7.18 decompressed version to start mysql service

The decompressed version of mysql5.7.18 starts th...

SELinux Getting Started

Back in the Kernel 2.6 era, a new security system...

Introduction to the use of anchors (named anchors) in HTML web pages

The following information is compiled from the Int...

vue $set implements assignment of values ​​to array collection objects

Vue $set array collection object assignment In th...

Introduction to ufw firewall in Linux

Let's take a look at ufw (Uncomplicated Firew...

The difference between animation and transition

The difference between CSS3 animation and JS anim...

Detailed explanation of the wonderful uses of SUID, SGID and SBIT in Linux

Preface Linux's file permission management is...

Implementation of react automatic construction routing

Table of contents sequence 1. Centralized routing...

Using JS to implement a small game of aircraft war

This article example shares the specific code of ...

Analysis of the principle of Nginx using Lua module to implement WAF

Table of contents 1. Background of WAF 2. What is...

Detailed explanation of Apache website service configuration based on Linux

As an open source software, Apache is one of the ...

MySQL multi-instance configuration solution

1.1 What is MySQL multi-instance? Simply put, MyS...

Detailed explanation of how MySQL solves phantom reads

1. What is phantom reading? In a transaction, aft...