我正在构建一个表单中的输入,其中包含一个个人资料图片,当点击时,将允许用户 Select 一个新的图像.看起来常见的做法是使用"文件"类型的输入,然后通过各种方法隐藏它.对我来说,设置opacity:0似乎是唯一合法的 Select ,因为这将使元素保留在DOM中,并且它仍然会检测点击或焦点.

然而,没有不透明度的输入也不会有可见的焦点环,即使该元素在技术上处于焦点中.我有什么 Select 可以 Select ,以便在聚焦时有一个焦点环秀?提前谢谢您.

.profile-pic {
  width: 156px;
  height: 156px;
  border-radius: 50%;
  position: relative;
  /* overflow: hidden; */
}

.profile-pic input[type='file'] {
  opacity: 0;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  padding: 0;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  z-index: 10;
}

.profile-pic__img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  overflow: hidden;
  z-index: 1;
}

.profile-pic__img img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  filter: brightness(0.65);
  transition: filter 0.2s ease-in-out;
  cursor: pointer;
}

.profile-pic__img img:hover {
  filter: brightness(0.5);
}

.profile-pic__text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 90%;
  display: flex;
  flex-direction: column;
  align-items: center;
  z-index: 5;
}

.profile-pic__text .fa-camera {
  color: #fff;
}

.profile-pic span {
  color: #fff;
  text-align: center;
  font-size: 16px;
  font-style: normal;
  font-weight: 400;
  line-height: normal;
  width: 90%;
}
<div class="profile-pic">
  <input type="file" />
  <div class="profile-pic__img">
    <img src="[TEST_URL]" alt="" />
  </div>
  <div class="profile-pic__text">
    <i class="fas fa-camera"></i>
    <span>Click to change photo</span>
  </div>
</div>

推荐答案

the :focus-within pseudo-class应用于.profile-pic,并在透明输入具有焦点时为其赋予独特的样式.

.profile-pic {
  width: 156px;
  height: 156px;
  border-radius: 50%;
  position: relative;
  /* overflow: hidden; */
}

.profile-pic:focus-within {
    outline: dotted #777 2px;
}

.profile-pic input[type='file'] {
  opacity: 0;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  padding: 0;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  z-index: 10;
}

.profile-pic__img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  overflow: hidden;
  z-index: 1;
}

.profile-pic__img img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  filter: brightness(0.65);
  transition: filter 0.2s ease-in-out;
  cursor: pointer;
}

.profile-pic__img img:hover {
  filter: brightness(0.5);
}

.profile-pic__text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 90%;
  display: flex;
  flex-direction: column;
  align-items: center;
  z-index: 5;
}

.profile-pic__text .fa-camera {
  color: #fff;
}

.profile-pic span {
  color: #fff;
  text-align: center;
  font-size: 16px;
  font-style: normal;
  font-weight: 400;
  line-height: normal;
  width: 90%;
}
<div class="profile-pic">
  <input type="file" />
  <div class="profile-pic__img">
    <img src="[TEST_URL]" alt="" />
  </div>
  <div class="profile-pic__text">
    <i class="fas fa-camera"></i>
    <span>Click to change photo</span>
  </div>
</div>

Javascript相关问答推荐

我可以在useState中调用函数并使用其数据吗

如何计算数组子级的深度- Vue.js

为什么我无法使用useMemo()停止脚本渲染?

Toast函数找不到其dis

使用JavaScript重命名对象数组中的键

如何通过onClick为一组按钮分配功能;

Next.js Next/Image图像隐含性有任何类型-如何修复?

React存档iframe点击行为

docx.js:如何在客户端使用文档修补程序

为什么我的列表直到下一次提交才更新值/onChange

如何通过使用vanilla JS限制字体大小增加或减少两次来改变字体大小

在使用HighChats时如何避免Datatables重新初始化错误?

更新动态数据中对象或数组中的所有值字符串

扩展类型的联合被解析为基类型

OpenAI转录API错误请求

面对代码中的错误作为前端与后端的集成

Reaction-SWR-无更新组件

在查看网页时,如何使HTML中的按钮工作方式类似于鼠标上的滚轮或箭头键?

在SuperBase JS客户端中寻址JSON数据

如何使用[ModelJSON,ArrayBuffer]调用tf.loadGraphModelSync