反应可单击的元素在触摸容器中
我正在尝试设置带有可单击元素的可滑动容器。该组件如下:
const SectionBar = (): JSX.Element => {
const [translatePercent, setTranslatePercent] = useState(0);
const [x0, setX0] = useState(0);
const [isMediumOrLarger, setIsMediumOrLarger] = useState(false);
useEffect(() => {
const mediaQuery = window.matchMedia("(min-width: 768px)");
setIsMediumOrLarger(mediaQuery.matches);
mediaQuery.addEventListener("change", (event) =>
setIsMediumOrLarger(event.matches)
);
}, []);
const setSnapPercent = (index: number) => {
console.log(index);
if (index === 3) {
setTranslatePercent(index * 10 + 6);
} else if (index >= 4) {
setTranslatePercent((index + 1) * 10);
} else {
setTranslatePercent(index * 10);
}
};
const swipe: TouchEventHandler<HTMLUListElement> = (
event: TouchEvent<HTMLUListElement>
) => {
const dx = -(event.changedTouches[0].clientX - x0) / 5;
const percent = Math.max(0, Math.min(dx, MAX_PERCENT));
setTranslatePercent(percent);
};
return (
<nav className={classes["section-bar"]}>
<ul
className={classes["section-list"]}
style={
isMediumOrLarger
? {}
: {
transform: `translateX(-${translatePercent}%)`,
}
}
onTouchStart={(event) => setX0(event.changedTouches[0].clientX)}
onTouchEnd={swipe}
onTouchMove={swipe}
>
{Object.entries(createSections).map(([key, value], index) => (
<li key={key}>
<Link href={`/create/race#`}>
<a className={classes.link} onClick={() => setSnapPercent(index)}>
{value}
</a>
</Link>
</li>
))}
</ul>
</nav>
);
};
当我尝试在Chrome开发人员工具中的模拟电话中测试组件时,我可以刷新,但是敲击其中一个链接似乎只能在索引0上登录链接,而有时1,而不管有时1我按哪个链接。当我不将触摸事件道具传递给ul
链接工作正常时,我认为问题与触摸事件有关。
I'm trying to set up a swipeable container with clickable elements. The component is the following:
const SectionBar = (): JSX.Element => {
const [translatePercent, setTranslatePercent] = useState(0);
const [x0, setX0] = useState(0);
const [isMediumOrLarger, setIsMediumOrLarger] = useState(false);
useEffect(() => {
const mediaQuery = window.matchMedia("(min-width: 768px)");
setIsMediumOrLarger(mediaQuery.matches);
mediaQuery.addEventListener("change", (event) =>
setIsMediumOrLarger(event.matches)
);
}, []);
const setSnapPercent = (index: number) => {
console.log(index);
if (index === 3) {
setTranslatePercent(index * 10 + 6);
} else if (index >= 4) {
setTranslatePercent((index + 1) * 10);
} else {
setTranslatePercent(index * 10);
}
};
const swipe: TouchEventHandler<HTMLUListElement> = (
event: TouchEvent<HTMLUListElement>
) => {
const dx = -(event.changedTouches[0].clientX - x0) / 5;
const percent = Math.max(0, Math.min(dx, MAX_PERCENT));
setTranslatePercent(percent);
};
return (
<nav className={classes["section-bar"]}>
<ul
className={classes["section-list"]}
style={
isMediumOrLarger
? {}
: {
transform: `translateX(-${translatePercent}%)`,
}
}
onTouchStart={(event) => setX0(event.changedTouches[0].clientX)}
onTouchEnd={swipe}
onTouchMove={swipe}
>
{Object.entries(createSections).map(([key, value], index) => (
<li key={key}>
<Link href={`/create/race#`}>
<a className={classes.link} onClick={() => setSnapPercent(index)}>
{value}
</a>
</Link>
</li>
))}
</ul>
</nav>
);
};
When I try testing the component out in the simulated phone in Chrome developer tools I can swipe just fine, but tapping on one of the links only seems to register clicks on the links at index 0 and sometimes 1 regardless of which link I press. When I don't pass the touch event props to the ul
the links work just fine, so I think the problem has something to do with the touch events.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我让它起作用。在
Swipe
中,我从当前变换百分比中减去X中的更改。我不知道为什么修复了它。I got it working. in
swipe
, I subtracted the change in x from the current transform percent. I have no idea why that fixed it.