
* **`preventDefault` 的使用:** 需要谨慎使用`preventDefault`,因为它会阻止元素的默认行为,可能会影响其他功能的正常使用。
swiper事件结合状态控制:
提问者使用onBeforeTransitionStart和onTransitionEnd结合mobileScrollLock状态来尝试锁定滚动,这是一个很好的思路。可以进一步优化,例如:
import { useState, useRef } from 'react';
import { Swiper, SwiperSlide } from 'swiper/react';
import 'swiper/css';
function MySwiper() {
const [isSwiping, setIsSwiping] = useState(false);
const scrollPosition = useRef(0);
const handleTouchStart = () => {
scrollPosition.current = window.pageYOffset;
setIsSwiping(true);
};
const handleTouchMove = (e) => {
if (isSwiping) {
e.preventDefault(); // 阻止默认滚动
window.scrollTo(0, scrollPosition.current); // 保持滚动位置
}
};
const handleTouchEnd = () => {
setIsSwiping(false);
};
return (
<div
onTouchStart={handleTouchStart}
onTouchMove={handleTouchMove}
onTouchEnd={handleTouchEnd}
>
<Swiper /* Swiper配置 */ >
{/* Swiper slides */}
</Swiper>
</div>
);
}
export default MySwiper;注意事项
总结
解决Swiper在移动端水平滚动时垂直页面滚动的问题,需要综合考虑Swiper的配置、CSS样式和事件处理。 虽然该问题可能在特定版本中得到修复,但了解这些解决方案仍然有助于应对其他类似的触摸事件冲突问题。 重要的是,在解决问题的过程中,要充分考虑用户体验,并进行充分的测试。
以上就是解决Swiper在移动端水平滚动时垂直页面滚动的问题的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号