按照this教程的要求,我能够创建一个范围滑块,并根据我的需要进行定制

var inputs = document.querySelectorAll("input");

for (var input of inputs)
{
    console.log("processing")
    input.oninput = progressScript
    input.oninput()
}

function progressScript() {
    const sliderValue = this.value;
    this.style.background = `linear-gradient(to right, white ${sliderValue}%, rgb(44, 160, 255) ${sliderValue}%)`;
}
body
{
    background: rgb(41, 62, 72);
    color: white;
    font-size: 30px;
}

input[type=range] {
   -webkit-appearance: none;
   appearance: none;
   width: 300px;
   height: 10px;
   background: rgb(44, 160, 255);
   border-radius: 15px;
}

input[type=range]::-webkit-slider-thumb {
   -webkit-appearance: none;
   appearance: none;
   height: 20px;
   width: 20px;
   border-radius: 50%;
   background: rgb(41, 62, 72);
   border: 3px solid white;
   box-shadow: 0 0 0 1px rgb(41, 62, 72);
}

div
{
   height: 30px;
   margin-top: 30px;
   margin-bottom: 30px;
}

span
{
   margin-left: 10px;
   margin-right: 10px;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="styles.css">
</head>

<body>
    <p>Skills:</p>

    <div>
        <span>C++</span>
        <input type="range" name="volume" />
    </div>

    <div>
        <span>C#/.NET</span>
        <input type="range" name="cowbell" />
    </div>
    <script src="script.js"></script>
</body>

</html>

现在,我想确保我的跨度的中心线与范围滑块的中心线对齐,这样最终的结果看起来像这样,没有红线(包括只是为了清晰):

如何才能使两个内联元素从它们的中心线对齐?

enter image description here

推荐答案

您可以在div中添加一个类来包装您的元素(在行中),并向其中添加这些属性:

    display: flex;
    align-items: center;

它将如下所示:

enter image description here

在新代码中,我将div类简单地命名为container:

var inputs = document.querySelectorAll("input");

for (var input of inputs)
{
    console.log("processing")
    input.oninput = progressScript
    input.oninput()
}

function progressScript() {
    const sliderValue = this.value;
    this.style.background = `linear-gradient(to right, white ${sliderValue}%, rgb(44, 160, 255) ${sliderValue}%)`;
}
body
{
    background: rgb(41, 62, 72);
    color: white;
    font-size: 30px;
}

.container{
    display: flex;
    align-items: center;
   
}

input[type=range] {
   -webkit-appearance: none;
   appearance: none;
   width: 300px;
   height: 10px;
   background: rgb(44, 160, 255);
   border-radius: 15px;
}

input[type=range]::-webkit-slider-thumb {
   -webkit-appearance: none;
   appearance: none;
   height: 20px;
   width: 20px;
   border-radius: 50%;
   background: rgb(41, 62, 72);
   border: 3px solid white;
   box-shadow: 0 0 0 1px rgb(41, 62, 72);
}

div
{
   height: 30px;
   margin-top: 30px;
   margin-bottom: 30px;
}

span
{
   margin-left: 10px;
   margin-right: 10px;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="styles.css">
</head>

<body>
    <p>Skills:</p>

    <div class='container'>
        <span>C++</span>
        <input type="range" name="volume" />
    </div>

    <div class='container'>
        <span>C#/.NET</span>
        <input type="range" name="cowbell" />
    </div>
    <script src="script.js"></script>
</body>

</html>


如果你不想使用flex,你可以使用position,你可以添加:

    position: relative;
    top: -7px;

在你的input[type=range] Select 器里.

现在它看起来如下所示:

enter image description here

您还可以通过更改top: -7px;个像素量来更改输入的增减幅度.

下面是更新后的代码:

var inputs = document.querySelectorAll("input");

for (var input of inputs)
{
    console.log("processing")
    input.oninput = progressScript
    input.oninput()
}

function progressScript() {
    const sliderValue = this.value;
    this.style.background = `linear-gradient(to right, white ${sliderValue}%, rgb(44, 160, 255) ${sliderValue}%)`;
}
body
{
    background: rgb(41, 62, 72);
    color: white;
    font-size: 30px;
}

input[type=range] {
   -webkit-appearance: none;
   appearance: none;
   width: 300px;
   height: 10px;
   position: relative;
   top: -7px;
   background: rgb(44, 160, 255);
   border-radius: 15px;
}

input[type=range]::-webkit-slider-thumb {
   -webkit-appearance: none;
   appearance: none;
   height: 20px;
   width: 20px;
   border-radius: 50%;
   background: rgb(41, 62, 72);
   border: 3px solid white;
   box-shadow: 0 0 0 1px rgb(41, 62, 72);
}

div
{
   height: 30px;
   margin-top: 30px;
   margin-bottom: 30px;
}

span
{
   margin-left: 10px;
   margin-right: 10px;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="styles.css">
</head>

<body>
    <p>Skills:</p>

    <div class='container'>
        <span>C++</span>
        <input type="range" name="volume" />
    </div>

    <div class='container'>
        <span>C#/.NET</span>
        <input type="range" name="cowbell" />
    </div>
    <script src="script.js"></script>
</body>

</html>

Javascript相关问答推荐

React对话框模式在用户单击预期按钮之前出现

脚本.js:3:20未捕获的类型错误:无法读取空的属性(读取addEventHandler)

使用typeof运算符获取对象值类型-接收字符串而不是数组

Express.js:使用Passport.js实现基于角色的身份验证时出现太多重定向问题

积分计算和 colored颜色 判断错误

从PWA中的内部存储读取文件

Redux查询多个数据库Api reducerPath&

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

如何为我的astro页面中的相同组件自动创建不同的内容?

如何将Openjphjs与next.js一起使用?

无法访问Vue 3深度监视器中对象数组的特定对象值'

第二次更新文本输入字段后,Reaction崩溃

获取';无法解决导入和导入";slick-carousel/slick/slick-theme.css";';错误

从逗号和破折号分隔的给定字符串中查找所有有效的星期几

将基元传递给THEN处理程序

使每个<;li>;元素的 colored颜色 与随机生成的 colored颜色 列表不同(不重复

在Vercel中部署Next.js项目时获取`ReferenceError:未定义文档`

处理app.param()中的多个参数

用另一个带有类名的div包装元素

需要从对象生成列表