gpt4 book ai didi

css - 使用 CSS 模拟点击和取消点击

转载 作者:行者123 更新时间:2023-11-28 15:06:45 32 4
gpt4 key购买 nike

有没有办法只用 CSS 模拟“取消点击”事件?我能够通过具有时间延迟值的过渡属性来模拟“点击”。该 Action 会保持很长时间,就好像按钮已被单击一样。如何使此元素“未单击”或将其收回到原始状态?例如,有没有一种方法可以单击屏幕上的其他位置并让 .sidebar(参见包含的代码)处于“未单击”状态?

.sidebar > p {
background: blue;
width: 15em;
-webkit-transition: all 0s 1000s;
transition: all 0s 1000s;
position: relative;
}
.sidebar:active > p {
background: blue;
transform: translate(60px, -40px);
width: 40em;
transition: all 0s;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class='wrapper'>
<div class='sidebar'> Sidebar
<p>This is paragraph 1 inside sidebar </p>
<p>This is paragraph 2 inside sidebar </p>
</div>
<div class='other'></div>
</div>
</body>
</html>

非常感谢您的意见。

仅供引用

  1. 这不是家庭作业元素。

  2. 我了解 JavaScript。本练习用于学习 CSS。

  3. 我知道其他可能的模仿点击的方法,如此处所述 Can I have an onclick effect in CSS?或者在这里更好 https://tympanus.net/codrops/2012/12/17/css-click-events/

最佳答案

如果您不想要额外的元素,您可以使用 :focus 结合 tabindex 属性来获得输入元素的行为。因此,当您在外部单击时,您会失去焦点,这就像您想要的“取消单击”:

.sidebar {
outline:none;
}

.sidebar > p {
background: blue;
width: 15em;
position: relative;
transition:1s;
}
.sidebar:focus > p {
background: blue;
transform: translate(60px, -40px);
width: 40em;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class='sidebar' tabindex="-1"> Sidebar
<p>This is paragraph 1 inside sidebar </p>
<p>This is paragraph 2 inside sidebar </p>
</div>
<div class='other'></div>
</body>
</html>

关于css - 使用 CSS 模拟点击和取消点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49243443/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com