Jam*_*3sn 5 html css css3 css-transitions
我有一个消息空间,响应字段很好,底部很小.当用户点击时,它会展开.然而,当我专注时,如果我然后单击我的按钮,它会失去焦点 - 正如预期的那样,但是按钮没有被触发,我必须再次点击它.我该如何最好地解决这个问题?
.wrap {
align-items: center;
display: flex;
justify-content: flex-end;
}
textarea {
height: 40px;
width: 100%;
transition: height .2s ease;
}
textarea:focus {
height: 150px;
}
.fancy-button {
background-color: red;
cursor: pointer;
margin-left: 20px;
height: 30px;
width: 30px;
}Run Code Online (Sandbox Code Playgroud)
<div class="wrap">
<textarea></textarea>
<div class="fancy-button" onclick="alert('click');">
</div>Run Code Online (Sandbox Code Playgroud)
我认为问题在于,当您单击时,按钮在技术上并不位于这个位置,而只是在视觉上。如果您添加一个覆盖所有空间的包装器,您可能能够从第一次就捕捉到点击声。
当您进行快速转换时,浏览器正在计算并更新按钮的位置;因此您的点击位于按钮区域之外。
您可能还注意到,在此解决方案中,如果您单击按钮下方,则由于相同的原因,可能不会触发警报。包装纸的高度迅速下降,并且咔哒声可能在外面。
.wrap {
display: flex;
}
textarea {
height: 40px;
width: 100%;
transition: height .2s ease;
}
textarea:focus {
height: 150px;
}
.wrap > div {
display: flex;
align-items:center;
}
.fancy-button {
background-color: red;
cursor: pointer;
margin-left: 20px;
height: 30px;
width: 30px;
}Run Code Online (Sandbox Code Playgroud)
<div class="wrap">
<textarea></textarea>
<div onclick="alert('click');">
<div class="fancy-button" >
</div>
</div>
</div>Run Code Online (Sandbox Code Playgroud)
如果您只减少过渡时间,您也可以第一次捕获点击:
.wrap {
display: flex;
align-items: center;
}
textarea {
height: 40px;
width: 100%;
transition: height 2s ease;
}
textarea:focus {
height: 150px;
}
.fancy-button {
background-color: red;
cursor: pointer;
margin-left: 20px;
height: 30px;
width: 30px;
}Run Code Online (Sandbox Code Playgroud)
<div class="wrap">
<textarea></textarea>
<div onclick="alert('click');" class="fancy-button">
</div>
</div>Run Code Online (Sandbox Code Playgroud)
您还可以保留持续时间并添加延迟:
.wrap {
display: flex;
align-items: center;
}
textarea {
height: 40px;
width: 100%;
transition: height .2s ease;
transition-delay:0.1s;
}
textarea:focus {
height: 150px;
transition-delay:0s;
}
.fancy-button {
background-color: red;
cursor: pointer;
margin-left: 20px;
height: 30px;
width: 30px;
}Run Code Online (Sandbox Code Playgroud)
<div class="wrap">
<textarea></textarea>
<div onclick="alert('click');" class="fancy-button">
</div>
</div>Run Code Online (Sandbox Code Playgroud)