Add these properties when initializing
click: true, // 鼠标点击滚动
// tap: true,
// mouseWheel: true, //鼠标滚轮滚动
disableMouse: false,// 启用鼠标拖动
disableTouch: false,// 启用手指触摸
better-scroll fails to slide during initialization, and needs to change the window size for normal sliding
project running after opening Google cannot slide
When I arbitrarily modify the window size, the normal sliding
How to change
<template>
<div class="study">
<Nav/>
<div class="list">
<ly-tab
v-model="selectedId"
:items="items"
:options="options"
>
</ly-tab>
<div class="wrapper" ref="wrapper">
<ul class="content">
<li v-for="(item,index) in data"
:key="index"
>
<div class="img">
<img :src="item.BookImg" alt="">
</div>
<div class="text"></div>
<div class="author">
<h4>{{item.BookName}}</h4>
<p>{{item.BookAuthor}}</p>
</div>
</li>
</ul>
</div>
</div>
</div>
</template>
<script>
import Nav from '../common/NavBack.vue'
import http from '../../assets/api/request'
import BScroll from 'better-scroll'
export default {
components:{
Nav,
BScroll
},
data(){
return{
selectedId:0,
items:[
{id:1,label:"全部"},
{id:2,label:"乌托邦"},
{id:3,label:"心理"},
{id:4,label:"哲学"},
{id:5,label:"犯罪"},
{id:6,label:"科幻"},
{id:6,label:"传记"},
{id:6,label:"科普"},
],
options: {
activeColor: '#1d98bd'
// 可在这里指定labelKey为你数据里文字对应的字段
},
data:[]
}
},
methods:{
async getData(){
let res = await http.$axios({
url:'/api/booklist_all',
})
this.data = res
console.log(this.data)
},
},
mounted(){
this.getData();
this.$nextTick(()=>{
this.scroll = new BScroll(this.$refs.wrapper, {
click:true,
})
})
}
}
</script>
<style scoped lang='scss'>
.wrapper{
width: 100vw;
height: 85vh;
background-color: rgb(255, 255, 255);
overflow: hidden;
}
.study{
width: 100vw;
height: 100vh;
overflow: hidden;
.list{
width: 100vw;
height: 8vh;
margin: 8vh 0 0 0;
}
}
.content{
width: 100vw;
display: flex;
flex-direction:column;
align-items:center;
justify-content:space-between;
li{
width: 95vw;
height: 30vh;
padding: 10px;
display: flex;
flex-wrap:wrap;
.img{
background-color: coral;
flex-grow:1;
border-radius: 15px;
height: 20vh;
img{
width: 100%;
height: 100%;
}
}
.text{
height: 20vh;
flex-grow:2;
background-color: deepskyblue;
margin-left: 15px;
border-radius: 15px;
}
.author{
width:100%;
height: 9vh;
padding-left: 5px;
h4{
font-size: 25px;
font-weight:normal;
}
p{
margin: 0;
padding: 0;
}
}
}
}
</style>
0 Answer
Add these properties when initializing
click: true, // 鼠标点击滚动
// tap: true,
// mouseWheel: true, //鼠标滚轮滚动
disableMouse: false,// 启用鼠标拖动
disableTouch: false,// 启用手指触摸
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.
Calculate the container size when initializing better-scroll.
You can try:
mounted() {
this.getData();
this.$nextTick(() => {
this.scroll = new BScroll(this.$refs.wrapper, {
click: true,
});
this.scroll.refresh();
});
}
you're welcome
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.
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!
这家伙很懒,什么都没留下...