英文:
How can I add a value to center of a Input Slider (Range Slider)
问题
如何将这个数值文本添加到输入滑块的中心位置
<div class="range-wrap"><div class="bubble"></div><br>
<input type="range" min="30" max="400" value="50" class="range" style="text-align:right;">
英文:
How can i add this value text to the center of the inupt slider
<div class="range-wrap"><div class="bubble"></div><br>
<input type="range" min="30" max="400" value="50" class="range" style="text-align:right;">
答案1
得分: 1
- 使用
data-*
属性来存储当前值(以及所需的前缀,如 "music:) - 使用
::after
伪元素来创建文本提示的工具提示 - 使用 CSS
content
属性和attr(data-*)
来输出数据属性
<!-- 开始代码片段: js 隐藏: false 控制台: true babel: false -->
<!-- 语言: lang-js -->
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);
<!-- 语言: lang-css -->
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; /* 防止指针交互 */
}
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;
}
<!-- 语言: lang-html -->
<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 代替。- 避免内联的
style
和on*
处理程序属性。
英文:
- Use
data-*
attribute to store the current value (and the desired prefix like "music:) - use
::after
pseudo to create your tooltip for the textual tooltip value - use CSS
content
property andattr(data-*)
to print the data attributes
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
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);
<!-- language: lang-css -->
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;
}
<!-- language: lang-html -->
<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>
<!-- end snippet -->
Also:
<center>
is deprecated. Use CSS instead- avoid inline
style
andon*
handler attributes
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论