【发布时间】:2015-07-11 12:03:23
【问题描述】:
我想在选中复选框时更改标签的位置。如果我不转换标签的 top 偏移属性,这可以正常工作。但是,如果我向该值添加转换(请参阅注释代码),请单击标签并且不要移动鼠标的光标,标签似乎仍处于悬停状态。这意味着,即使我没有将鼠标悬停在它上面,光标也是一个指针,背景颜色为绿色(标签的悬停状态)。
如果你看到the demo,你就会明白我的意思。
我的 HTML 代码如下:
<section>
<input type="checkbox" id="checkbox_id">
<label for="checkbox_id">Click me</label>
<div>
<a href="">This is the first link</a>
<a href="">This is the second link</a>
</div>
</section>
我的 CSS:
* {
margin: 0;
}
section {
position: relative;
padding: 20px;
background: yellow;
}
input[type="checkbox"] {
display: none;
}
label, a {
height: 30px;
padding: 10px 0;
margin: 10px;
}
label {
display: block;
background: tomato;
cursor: pointer;
width: 100%;
position: absolute;
top: 0;
/*transition: top .3s ease;*/
}
label:hover {
background: green;
}
input[type="checkbox"]:checked + label {
top: 100%;
}
a {
display: block;
background: tomato;
}
a:first-child {
margin-top: 50px;
}
知道为什么会这样吗?
【问题讨论】:
-
一旦过渡,它只等待鼠标指针最轻微的移动来改变它的颜色,你不想要这种行为吗?我理解正确吗?
-
是的,没错。实际上,我想要的是表现得就像没有过渡一样。否则,我必须移动鼠标才能实现该行为。
-
你介意用 jQuery 来解决这个问题吗?
-
好吧,我可以使用 jQuery,但我真正想知道的是为什么会这样。我在网上搜索,但没有找到类似的问题或类似的问题。奇怪的是为什么只有在添加过渡时才会发生这种情况。
-
我认为这并不奇怪,因为它是之后需要的最轻微的鼠标移动将一切再次设置为 正常,也许人们只是习惯于看到这个行为。我个人认为这只是一种正常的行为。我可能错了。
标签: html css checkbox label css-transitions