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

Detailed explanation of pure SQL statement method based on JPQL

JPQL stands for Java Persistence Query Language. ...

A brief analysis of Vue's asynchronous update of DOM

Table of contents The principle of Vue asynchrono...

Solution to the impact of empty paths on page performance

A few days ago, I saw a post shared by Yu Bo on G...

A brief discussion on two current limiting methods in Nginx

The load is generally estimated during system des...

Use of docker system command set

Table of contents docker system df docker system ...

After reading the introduction of CSS box model, you will not be confused

The property names often heard in web design: con...

Let's talk about the characteristics and isolation levels of MySQL transactions

The Internet is already saturated with articles o...

Summary of Git commit log modification methods

Case 1: Last submission and no push Execute the f...

How to configure multiple projects with the same domain name in Nginx

There are two ways to configure multiple projects...

MySQL 8.0 upgrade experience

Table of contents Preface 1. First completely uni...

HTML table tag tutorial (32): cell horizontal alignment attribute ALIGN

In the horizontal direction, you can set the cell...

Detailed explanation of transactions and indexes in MySQL database

Table of contents 1. Affairs: Four major characte...

Modify the style of HTML body in JS

Table of contents 1. Original Definition 2. JS op...