>> My Input Slider <<

>> I want to do this <<

我怎样才能添加这个value text to the center of the inupt滑块

<div class="range-wrap"><div class="bubble"></div><br>
<input type="range" min="30" max="400" value="50" class="range" style="text-align:right;">

推荐答案

  • 使用data-*属性存储当前值(以及所需的前缀,如"music:)
  • 使用::after伪创建文本工具提示值的工具提示
  • 使用CSScontent属性和attr(data-*)打印数据属性

const rangeTooltip = (elRange) => {
  elRange.dataset.value = `${elRange.querySelector("[type=range]").value}%`;
};

const range = (elRange) => {
  elRange.addEventListener("input", () => rangeTooltip(elRange));
  rangeTooltip(elRange);
};

document.querySelectorAll(".range").forEach(range);
body {
  background: #2b2219;
  font: 16px/1.4 sans-serif;
}

.range {
  position: relative;
  width: 220px;
}

.range::after {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  content: attr(data-prefix) attr(data-value);
  color: #fff;
  opacity: 0.7;
  pointer-events: none; /* prevent pointer interaction */
}

input[type=range] {
  appearance: none;
  width: inherit;
  cursor: pointer;
}

input[type=range]:focus {
  outline: none;
}

input[type=range]::-webkit-slider-runnable-track {
  height: 2rem;
  box-shadow: 0 0 0 2px #000, inset 0 0 0 1px #fff1;
  background: #2b2b2b;
}

input[type=range]::-webkit-slider-thumb {
  appearance: none;
  box-shadow: 0 0 0 2px #000, inset 0 0 0 1px #fff2;
  height: 100%;
  width: 1rem;
  border-radius: 3px;
  background: #6d6e70;
  cursor: pointer;
  border-radius: 0;
}

input[type=range]:focus::-webkit-slider-runnable-track {
  background: #313131;
}
<div class="range" data-prefix="Music:">
  <input type="range" min="0" max="100" value="100">
</div>

<div class="range" data-prefix="Effects:">
  <input type="range" min="0" max="100" value="100">
</div>

<div class="range" data-prefix="Difficulty:">
  <input type="range" min="0" max="100" value="50">
</div>

所以:

  • <center>已弃用.改用css
  • 避免内联styleon*处理程序属性

Javascript相关问答推荐

ReactJS中的material UI自动完成类别

传递一个大对象以在Express布局中呈现

D3 Scale在v6中工作,但在v7中不工作

如何在不创建新键的情况下动态更改 map 中的项目?

如何使覆盖div与可水平滚动的父div相关?

使用Java脚本根据按下的按钮更改S文本

在Three JS中看不到补间不透明度更改效果

如何在JavaScript文件中使用Json文件

Angular 订阅部分相互依赖并返回数组多个异步Http调用

TypeError:无法分解';React2.useContext(...)';的属性';basename';,因为它为空

将数组扩展到对象中

FireBase FiRestore安全规则-嵌套对象的MapDiff

每隔3个项目交替显示,然后每1个项目交替显示

P5.js中矩形内的圆弧

使用createBrowserRoutVS BrowserRouter的Reaction路由

如何在Reaction中清除输入字段

在传单的图像覆盖中重新着色特定 colored颜色 的所有像素

在对象的嵌套数组中添加两个属性

Reaction路由v6.4+数据API:重试加载程序而不显示错误

带元素数组的Mongo聚合