Link 在 TailwindCSS group:focus 元素内失败,因为点击事件被阻止
Link inside a TailwindCSS group:focus element fails as click event prevented
我有一个常见问题解答部分,其中包含手风琴式弹簧部分,全部使用 TailwindCSS 制作(即没有 Javascript)。打开部分内的链接不起作用(它们只是在问题失去焦点时关闭常见问题解答)。
有谁知道如何在失去焦点之前触发 link 事件?
这是一个演示问题的简单的最小示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test TailwindCSS Group:Focus</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link href="tailwind.css" rel="stylesheet">
<style>
/* ----- ACCORDION ----- */
.group:focus .group-focus\:max-h-screen {
max-height: 100vh;
}
.group:focus .group-focus\:text-white {
--text-opacity: 1;
color: #02455a; /*petrol_blue*/
color: rgba(255, 255, 255, var(--text-opacity));
}
.group:focus .group-focus\:-rotate-90 {
--transform-rotate: -90deg;
}
</style>
</head>
<body>
<div class="group" tabindex="1">
<div class="group flex justify-between px-4 py-2 items-center transition cursor-pointer pr-10 relative">
<div class="h-8 w-8 items-center inline-flex justify-center rotate-180 transform transition group-focus:-rotate-90 absolute top-0 left-0 mb-auto ml-auto mt-2 mr-2">
<svg fill="#02455a" height='100px' width='100px' xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 847 847" x="0px" y="0px">
<g><polygon points="670,584 423,263 176,584 "></polygon></g></svg>
</div>
<div class="transition pl-4 hover:opacity-50">Question</div>
</div>
<div class="group-focus:max-h-screen max-h-0 px-4 overflow-hidden">
<p class="pl-4 pr-4 pt-0 pb-2">Answer: <a href="https://whosebug.com">Stack Overflow</a></p>
</div>
</div>
</body>
</html>
tailwind.css
正在创建使用:
npx tailwindcss-cli@latest build -o tailwind.css
(顺便说一下,我意识到 svg 箭头本身不正常,但这是另一个问题...)
除了您的 group-focus:max-h-screen
之外,您还可以添加 focus-within:max-h-screen
以便在锚获得焦点时保持最大高度。
要修复箭头动画,只需将 CSS 中的 --transform-rotate
替换为 --tw-rotate
。
.group:focus .group-focus\:max-h-screen {
max-height: 100vh;
}
.group:focus .group-focus\:text-white {
--text-opacity: 1;
color: #02455a;
}
.group:focus .group-focus\:-rotate-90 {
--tw-rotate: 90deg;
}
.focus-within\:max-h-screen:focus-within {
max-height: 100vh;
}
<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
<div class="group" tabindex="1">
<div class="group flex justify-between px-4 py-2 items-center transition cursor-pointer pr-10 relative">
<div class="h-8 w-8 items-center inline-flex justify-center rotate-180 transform transition group-focus:-rotate-90 absolute top-0 left-0 mb-auto ml-auto mt-2 mr-2">
<svg fill="#02455a" height="100px" width="100px" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 847 847" x="0px" y="0px">
<g><polygon points="670,584 423,263 176,584 "></polygon></g>
</svg>
</div>
<div class="transition pl-4 hover:opacity-50">Question</div>
</div>
<div class="group-focus:max-h-screen focus-within:max-h-screen max-h-0 px-4 overflow-hidden">
<p class="pl-4 pr-4 pt-0 pb-2">Answer: <a href="https://whosebug.com">Stack Overflow</a></p>
</div>
</div>
我有一个常见问题解答部分,其中包含手风琴式弹簧部分,全部使用 TailwindCSS 制作(即没有 Javascript)。打开部分内的链接不起作用(它们只是在问题失去焦点时关闭常见问题解答)。
有谁知道如何在失去焦点之前触发 link 事件?
这是一个演示问题的简单的最小示例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Test TailwindCSS Group:Focus</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link href="tailwind.css" rel="stylesheet">
<style>
/* ----- ACCORDION ----- */
.group:focus .group-focus\:max-h-screen {
max-height: 100vh;
}
.group:focus .group-focus\:text-white {
--text-opacity: 1;
color: #02455a; /*petrol_blue*/
color: rgba(255, 255, 255, var(--text-opacity));
}
.group:focus .group-focus\:-rotate-90 {
--transform-rotate: -90deg;
}
</style>
</head>
<body>
<div class="group" tabindex="1">
<div class="group flex justify-between px-4 py-2 items-center transition cursor-pointer pr-10 relative">
<div class="h-8 w-8 items-center inline-flex justify-center rotate-180 transform transition group-focus:-rotate-90 absolute top-0 left-0 mb-auto ml-auto mt-2 mr-2">
<svg fill="#02455a" height='100px' width='100px' xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 847 847" x="0px" y="0px">
<g><polygon points="670,584 423,263 176,584 "></polygon></g></svg>
</div>
<div class="transition pl-4 hover:opacity-50">Question</div>
</div>
<div class="group-focus:max-h-screen max-h-0 px-4 overflow-hidden">
<p class="pl-4 pr-4 pt-0 pb-2">Answer: <a href="https://whosebug.com">Stack Overflow</a></p>
</div>
</div>
</body>
</html>
tailwind.css
正在创建使用:
npx tailwindcss-cli@latest build -o tailwind.css
(顺便说一下,我意识到 svg 箭头本身不正常,但这是另一个问题...)
除了您的 group-focus:max-h-screen
之外,您还可以添加 focus-within:max-h-screen
以便在锚获得焦点时保持最大高度。
要修复箭头动画,只需将 CSS 中的 --transform-rotate
替换为 --tw-rotate
。
.group:focus .group-focus\:max-h-screen {
max-height: 100vh;
}
.group:focus .group-focus\:text-white {
--text-opacity: 1;
color: #02455a;
}
.group:focus .group-focus\:-rotate-90 {
--tw-rotate: 90deg;
}
.focus-within\:max-h-screen:focus-within {
max-height: 100vh;
}
<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
<div class="group" tabindex="1">
<div class="group flex justify-between px-4 py-2 items-center transition cursor-pointer pr-10 relative">
<div class="h-8 w-8 items-center inline-flex justify-center rotate-180 transform transition group-focus:-rotate-90 absolute top-0 left-0 mb-auto ml-auto mt-2 mr-2">
<svg fill="#02455a" height="100px" width="100px" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 847 847" x="0px" y="0px">
<g><polygon points="670,584 423,263 176,584 "></polygon></g>
</svg>
</div>
<div class="transition pl-4 hover:opacity-50">Question</div>
</div>
<div class="group-focus:max-h-screen focus-within:max-h-screen max-h-0 px-4 overflow-hidden">
<p class="pl-4 pr-4 pt-0 pb-2">Answer: <a href="https://whosebug.com">Stack Overflow</a></p>
</div>
</div>