Home > Article > Web Front-end > Scrolling fails in uniapp component
With the widespread use of mobile web applications, uniapp, as a cross-platform development framework based on Vue.js, has attracted more and more attention and use by developers. In the process of developing uniapp applications, we often use various components to implement different functions. Among them, the scroll component is a commonly used component to implement functions such as long lists and paging data. However, in actual development, sometimes we encounter some strange problems, such as scrolling failure and inability to scroll. This article will introduce a common rolling failure problem and its solution.
1. Problem description
When using the scroll-view component in uniapp, we often encounter a problem: when other components (such as swiper, list, etc.) are nested in the scroll-view component ), scrolling fails. Normally, we set the "scroll-y" attribute and height to the scroll-view component, but when we slide on the page, the interface does not scroll with the finger, but the entire page scrolls together. If we try to use the scroll-view component alone, we can scroll normally. There seems to be no obvious solution to this problem, but we can find the reasons and solutions based on experience and trial.
2. Problem Analysis
By excluding some common causes, we can think that the essence of this problem is caused by the scroll-view component nesting other components. Under normal circumstances, the scroll-view component should be able to respond to scroll events. However, when other components are nested, these components will prioritize sliding events, causing the scroll-view component to be unable to respond to scroll events. Therefore, we need to find a way to prevent other nested components from processing sliding events, so that the scroll-view component can respond to scrolling events normally.
3. Solution
Through the above analysis, we can get a solution: set the nested components to disable sliding events. We can use the "catchtouchmove" attribute to achieve this purpose. This attribute can intercept the browser's default touchmove event and prevent the event from being passed to the parent element. We only need to set the "catchtouchmove" attribute to the nested components to prevent them from handling sliding events.
The following is a simple sample code:
<scroll-view scroll-y style="height: 300rpx;"> <swiper catchtouchmove> <swiper-item> <view style="height: 100rpx; background-color: red;"></view> </swiper-item> <swiper-item> <view style="height: 100rpx; background-color: blue;"></view> </swiper-item> </swiper> <list catchtouchmove> <view class="list-item" v-for="(item, index) in list" :key="index">{{item}}</view> </list> </scroll-view>
In this code, we set the "catchtouchmove" attribute to both the swiper and list components so that they will not handle sliding events. The scroll-view component can respond to scroll events normally.
We can find that this solution is very simple, but very practical. If you encounter similar problems in uniapp development, you might as well try this method.
4. Summary
In the uniapp component, scrolling failure is a common and troublesome problem. This article introduces a solution to prohibit nested components from handling sliding events through the "catchtouchmove" attribute, so that the scroll-view component can respond to scrolling events normally. We believe that this method is not only helpful in solving problems, but also helps developers better understand component nesting and event delivery mechanisms.
The above is the detailed content of Scrolling fails in uniapp component. For more information, please follow other related articles on the PHP Chinese website!