【问题标题】:Button not clicking while leaving textarea focus离开文本区域焦点时按钮未单击
【发布时间】:2018-04-20 19:07:59
【问题描述】:

我有一个消息空间,底部的响应字段又小又好。当用户单击时,它会展开。但是,当聚焦时,如果我然后单击我的按钮,它会失去焦点 - 正如预期的那样,但按钮没有被触发,我必须再次单击它。我怎样才能最好地解决这个问题?

.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;
}
<div class="wrap">
   <textarea></textarea>
   <div class="fancy-button" onclick="alert('click');">
</div>

【问题讨论】:

  • 是的,因为第一次点击离开textarea:focus,只有第二次点击其他地方的其他地方是“免费的”。
  • 您可以在此处使用 javascript 解决方案在聚焦时向 textarea 添加一个类,然后当它不在聚焦时检查 textarea 中是否有任何文本,如果有,请保留类,如果不删除类

标签: html css css-transitions


【解决方案1】:

我认为问题在于,当您单击时,该按钮不是技术上的,而只是视觉上的。如果您添加一个覆盖所有空间的包装器,您可能能够从第一次就捕捉到点击。

当您进行快速转换时,浏览器正在计算并更新按钮的位置;因此您的点击位于按钮区域之外。

您可能还会注意到,在此解决方案中,如果您单击按钮下方的按钮,由于同样的原因,可能不会触发警报。包装器的高度迅速降低,点击可能在外面。

.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;
}
<div class="wrap">
  <textarea></textarea>
  <div onclick="alert('click');">
    <div class="fancy-button" >
    </div>
  </div>
</div>

如果你只减少转换时间,你也可以在第一时间捕捉到点击:

.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;
}
<div class="wrap">
  <textarea></textarea>
  <div onclick="alert('click');" class="fancy-button">
  </div>
</div>

你也可以保持持续时间并添加延迟:

.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;
}
<div class="wrap">
  <textarea></textarea>
  <div onclick="alert('click');" class="fancy-button">
  </div>
</div>

【讨论】:

    【解决方案2】:

    我不知道这是否适合您,因为我不知道您的需求,但如果您使用hover 而不是focus,则可以免费点击该按钮

    .wrap {
      align-items: center;
      display: flex;
      justify-content: flex-end;
    }
    
    textarea {
      height: 40px;
      width: 100%;
      transition: height .2s ease;
    }
    
    textarea:hover {
      height: 150px;
    }
    
    .fancy-button {
      background-color: red;
      cursor: pointer;
      margin-left: 20px;
      height: 30px;
      width: 30px;
    }
    <div class="wrap">
       <textarea></textarea>
       <a class="fancy-button" onclick="alert('click');"><a/>
    </div>

    【讨论】:

    • 如果可以保持焦点就好了,但是当 unhover 时会失去焦点……因为实际上我们在悬停时可能会有不需要的过渡。
    • 是的,你的答案是完美的,否则我们必须使用一些js来制作动画。
    猜你喜欢
    • 2015-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多