0
Follow
8
View

How to solve the problem that better-scroll initialization fails to slide?

deathxc 注册会员
2023-02-28 21:47

Add these properties when initializing

    click: true, // 鼠标点击滚动
        // tap: true,
        // mouseWheel: true, //鼠标滚轮滚动
        disableMouse: false,// 启用鼠标拖动
        disableTouch: false,// 启用手指触摸
cupidfang8 注册会员
2023-02-28 21:47

This answer quotes ChatGPT

Based on the code and screenshots you provided, try adding some options to the initialization of Better-Scroll to try to solve this problem. Specifically, you can try adding the probeType option, setting it to 3, and the click option to true to handle click events on your mobile device. These options should solve your problem as follows:


this.scroll = new BScroll(this.$refs.wrapper, {
  click: true,
  probeType: 3,
})

In practice, you can try adding these options in the mounted() hook function as follows:


mounted() {
  this.getData();
  this.$nextTick(() => {
    this.scroll = new BScroll(this.$refs.wrapper, {
      click: true,
      probeType: 3,
    })
  })
}

This way, you should be able to swipe the page normally without having to resize the window manually.

debbug 注册会员
2023-02-28 21:47

Calculate the container size when initializing better-scroll.
You can try:

  1. In the mounted hook function, make sure that the data is obtained before initializing better-scroll.
  2. sets the height of the container wrapper to a fixed value, such as 500px, and ensures that the height of its immediate parent.study is 100vh.
  3. If none of the above methods work, you can try manually firing the better-scroll refresh() method in the mounted hook function to recalculate the container size as follows:
mounted() {
  this.getData();
  this.$nextTick(() => {
    this.scroll = new BScroll(this.$refs.wrapper, {
      click: true,
    });
    this.scroll.refresh();
  });
}

denzhen33 注册会员
2023-02-28 21:47
< div class = "md_content_show e397 data - v - 3967" = "" >

you're welcome

diaochenlong 注册会员
2023-02-28 21:47

the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > according to the code, you use the better - scroll plug-in for sliding effect. However, when you initialize better-scroll, you do not specify the direction of the slide(landscape or portrait), nor the size of the content area(wrapper) and the size of the content, which may cause better-scroll to not calculate and handle the slide effect correctly. Therefore, you need to specify the better-scroll option parameter according to your actual situation. Here's what might need to be changed:

1. Specify the sliding direction: In new BScroll() you can specify the sliding direction property as follows:

this.scroll = new BScroll(this.$refs.wrapper, {
  direction: 'vertical', // 纵向滑动
  click: true
})


2. Specify wrapper size: Your wrapper style specifies a height of 85vh, but not a width. Therefore, you need to specify the wrapper width and height when initializing better-scroll as follows:

this.scroll = new BScroll(this.$refs.wrapper, {
  direction: 'vertical',
  click: true,
  // 指定wrapper的宽度和高度
  probeType: 3,
  scrollY: true,
  scrollX: false,
  scrollbar: {
    fade: true
  }
})


In this example, probeType: 3 means to dispatch scroll event in real time while sliding, which can be used to obtain the sliding position in real time. scrollY: true and scrollX: false indicate sliding vertically but not horizontally. scrollbar: {fade: true} Indicates that the scrollbar fades in and out.

3. Specify the size of the content: Your content area is id out using flex. You need to specify the height and width of the content area in the style as follows:

.content{
  width: 95vw; /* 修改为95vw */
  height: auto; /* 修改为auto */
  ...
}


This allows the content area to adapt to the height of the content.

Note: If you still can't swipe, you can determine the cause of the problem by checking the browser console for relevant error messages.

dqj0618 注册会员
2023-02-28 21:47

Referring to gpt and my own ideas, I found that you initialized better-scroll in the component mounted hook function, But the height of your.wrapper container is set to 85vh, and your entire component is also set to height: 100vh, which may cause better-scroll to not scroll properly.

It is recommended that you perform the following steps:

Check the style Settings of the component to make sure that the height Settings are appropriate. If the height is set too small, better-scroll may fail to scroll properly.
In the initialization of better-scroll, you need to pass some parameters to specify the width and height of the container, such as:


this.scroll = new BScroll(this.$refs.wrapper, {
    scrollY: true,
    scrollX: false,
    click: true,
    probeType: 3
})

Where scrollY and scrollX specify whether vertical and horizontal scrolling are enabled respectively, click specifies whether click events are enabled, and probeType specifies the frequency of better-scroll events to be dispatched during scrolling.

In the component's updated hook function, if better-scroll has been initialized, its refresh method needs to be called to update the container's width and height, for example:


updated() {
    if (this.scroll) {
        this.scroll.refresh();
    }
}


If the preceding methods do not solve the problem, you can use the debug mode provided by better-scroll to output more detailed debugging information, for example:


this.scroll = new BScroll(this.$refs.wrapper, {
    debug: true,
    ...
})

Hope to help you, please adopt!

dytcc23 注册会员
2023-02-28 21:47

When updated with the updated hook, thank you so much

About the Author

Question Info

Publish Time
2023-02-28 21:47
Update Time
2023-02-28 21:47