Solution to click event failure when using better-scroll on vue mobile development

Solution to click event failure when using better-scroll on vue mobile development

Recently I used vue to learn to develop mobile projects and used the better-scroll plug-in for scrolling.

When using the @click event in the component introduced into better-scroll, the click event fails. v-on:click, v-bind:click, and @click.native all work. However, I tried @touchstart and it works. I found that click: true is not set in the configuration of better-scroll. After setting it, the click event succeeds.

Later, when using vuex, I kept reporting

[vuex] unknown mutation type: changeCity

I just realized that I wrote mutations in the state in the store file, and I was so stupid that I cried. . .

The correct way to write it is as follows:

Encapsulation and use of vue better-scroll

I believe that many mobile projects use the better-scroll plug-in, which can make the scrolling of our pages less rigid and also make it easier for us to implement some functions such as anchors.

Note: This plugin only works if the height of the content you want to scroll is greater than the height of your parent box

First, install this plugin with npm

npm i better-scroll --save

Next, we encapsulate a Better component so that it can be reused globally

<template>
  <div class="wrapper" ref="wrapper" :style="{height:allHeight}">
   <slot></slot>
  </div>
</template>

<script>
import BScroll from 'better-scroll'
export default {
  data() {
    return {
      allHeight:''
    }
  },
  props:{
    handleToScroll:{
      type:Function,
      default:function(){}
    },
    handleToTouchEnd:{
      type:Function,
      default:function(){}
    }
  },
  methods: {
    handleToScrolltop(y){
      this.scroll.scrollTo(0,y)
    }
  },
  mounted() {
    var scroll = new BScroll(this.$refs.wrapper,{
      tap:true,
      probeType:1
    })
    this.scroll = scroll
     scroll.on('scroll',(pos)=>{
       this.handleToScroll(pos)
     })
     scroll.on('touchEnd',(pos)=>{
       this.handleToTouchEnd(pos)
     })

  let dangqian = this.$refs.wrapper.offsetTop
  let zongaodu = document.documentElement.clientHeight
  let all = zongaodu-dangqian
  this.allHeight = all + "px"
  },
}
</script>
<style>
</style>   

Dynamic binding of allHeight is to make the height of the parent box always the height from the bottom of the page, which is more conducive to implementation through calculation.

Two methods are encapsulated at the same time. One is scroll, which is a pull-down refresh method, and touchEnd is a function triggered after the pull-down refresh. There are also many other methods, such as pull-up to load more, etc. You can check it in the official website document

Next, register this component globally in main.js

import Scroller from './components/content/Scroller'
Vue.component('Scroller',Scroller)

Use in other components

For example, if we want the content in the ul tag to have a scrolling effect, we can write it like this

 <Scroller>
 <ul>
 </ul>
</Scroller>

If you want to use encapsulation, you can communicate through props

<Scroller :handleToScroll="handleToScroll" :handleToTouchEnd="handleToTouchEnd">

In this way, the page will have this scrolling effect, but please note that it will cover many of our native methods, but there is also a corresponding solution, using its internal properties. For example, the anchor point jump of the page

I wrote a method in the encapsulated component to make the y-axis move with

  methods: {
     handleToScrolltop(y){
       this.scroll.scrollTo(0,y)
     }
   },

How to use it?

By calling this method on the page you want to use

handleToScrolltop(), put the corresponding offsetTop in the brackets

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Solve the problem of click event failure in Vue component
  • Vue's custom components cannot use the click method solution
  • Vue custom component @click click failure problem and solution

<<:  MySql import CSV file or tab-delimited file

>>:  Tutorial on installing Ubuntu 1804 in VMware Workstation 15 Pro (with pictures and text)

Recommend

Vue3+el-table realizes row and column conversion

Table of contents Row-Column Conversion Analyze t...

How to add default time to a field in MySQL

Date type differences and uses MySQL has five dat...

Summary of three rules for React state management

Table of contents Preface No.1 A focus No.2 Extra...

MySQL 5.7.10 installation and configuration tutorial under Windows

MySQL provides two different versions for differe...

MySQL UNION operator basic knowledge points

MySQL UNION Operator This tutorial introduces the...

Using puppeteer to implement webpage screenshot function on linux (centos)

You may encounter the following problems when ins...

Implementation of docker redis5.0 cluster cluster construction

System environment: Ubuntu 16.04LTS This article ...

Encapsulate the navigation bar component with Vue

Preface: Fully encapsulating a functional module ...

How to handle super large form examples with Vue+ElementUI

Recently, due to business adjustments in the comp...

Summary of bootstrap learning experience-css style design sharing

Due to the needs of the project, I plan to study ...

Token verification login in Vue project (front-end part)

This article example shares the specific code of ...

In-depth explanation of closure in JavaScript

Introduction Closure is a very powerful feature i...

Summary of various methods for JS data type detection

Table of contents background What are the methods...