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

Solution to MySql Error 1698 (28000)

1. Problem description: MysqlERROR1698 (28000) so...

Server stress testing concepts and methods (TPS/concurrency)

Table of contents 1 Indicators in stress testing ...

Detailed explanation of common template commands in docker-compose.yml files

Note: When writing the docker-compose.yml file, a...

Practical TypeScript tips you may not know

Table of contents Preface Function Overloading Ma...

Configuring MySQL and Squel Pro on Mac

In response to the popularity of nodejs, we have ...

Briefly describe the difference between MySQL and Oracle

1. Oracle is a large database while MySQL is a sm...

Detailed explanation of mysql replication tool based on python

Table of contents 1. Introduction Second practice...

CSS stacking and z-index example code

Cascading and Cascading Levels HTML elements are ...

MySQL query learning basic query operations

Preface MySQL is the most popular relational data...

jQuery implements form validation function

jQuery form validation example / including userna...

Detailed explanation of common for loop in JavaScript statements

There are many loop statements in JavaScript, inc...

SQL Optimization Tutorial: IN and RANGE Queries

Preface "High Performance MySQL" mentio...

MySQL 5.7.17 installation and configuration method graphic tutorial (windows)

1. Download the software 1. Go to the MySQL offic...