gpt4 book ai didi

javascript - 范围 slider - 向下箭头跟随拇指

转载 作者:太空宇宙 更新时间:2023-11-04 06:23:55 25 4
gpt4 key购买 nike

我有一个范围 slider ,上面有一个输出元素。输出元素下方是一个向下箭头,它位于 slider 上的任何位置。

我正在使用 CSS 来放置箭头并为其设置动画。

不幸的是,考虑到 slider 是响应式的,箭头并不总是在拇指上方。

感谢您的帮助!

$(function() {
var slider = document.getElementById("range-slider");
var output = document.getElementById("range-slider-output");

output.innerHTML = slider.value;

slider.oninput = function() {
output.innerHTML = this.value;
// resets state of user icon
$('.fa-user').removeClass("disabled");

if (output.innerHTML == 1) {
document.getElementById("range-slider").setAttribute("aria-valuenow", "1");
$('.range-slider-icons li:nth-of-type(2) span, .range-slider-icons li:nth-of-type(3) span, .range-slider-icons li:nth-of-type(4) span').addClass('disabled');

} else if (output.innerHTML == 2) {
document.getElementById("range-slider").setAttribute("aria-valuenow", "2");
$('.range-slider-icons li:nth-of-type(3) span, .range-slider-icons li:nth-of-type(4) span').addClass('disabled');

} else if (output.innerHTML == 3) {
document.getElementById("range-slider").setAttribute("aria-valuenow", "3");
$('.range-slider-icons li:nth-of-type(4) span').addClass('disabled');

} else {
document.getElementById("range-slider").setAttribute("aria-valuenow", "4");
}

}


});
.range-slider-output {
position: relative;
display: inline-block;
padding: 0.2em 0.75em 0.25em;
color: #fff;
text-align: center;
background: #666;
border-radius: 3px;
width: 100%;
left: calc(50%);
flex: 0 0 5%;
align-self: center;
margin: 0;
font-size: 28px;
-webkit-transform: translateX(-50%);
transform: translateX(-50%);
top: -92px;
}

.range-slider-output::before {
position: absolute;
top: 50%;
left: 0;
content: "";
}

.range-slider-lg .range-slider-output::before {
top: 48px;
width: 0;
height: 0;
border-style: solid;
border-width: 12px 12px 0 12px;
border-color: #666 transparent transparent transparent;
transition: all 0.7s ease-out;
}

.range-slider-wrap {
min-width: 250px;
}

input[aria-valuenow='1']+.range-slider-output::before {
left: 1rem;
}

input[aria-valuenow='2']+.range-slider-output::before {
left: 32.5%;
}

input[aria-valuenow='3']+.range-slider-output::before {
left: 64%;
}

input[aria-valuenow='4']+.range-slider-output::before {
left: 94%;
}

input[type='range'] {
width: 100%;
cursor: pointer;
padding-top: 90px;
}

input[type='range'] {
-webkit-appearance: none;
}

input[type='range']::-webkit-slider-runnable-track {
width: 100%;
height: 5px;
background: #e6e5e5;
border: 1px solid #999;
border-radius: 3px;
-webkit-appearance: none;
padding: 0 0.5rem;
}

input[type='range']::-moz-range-track {
width: 100%;
height: 5px;
background: #e6e5e5;
border: 1px solid #999;
border-radius: 3px;
}

input[type=range]::-webkit-slider-thumb {
width: 28px;
height: 28px;
margin-top: -11px;
background: #999;
border: 1px solid #666;
border-radius: 50%;
-webkit-appearance: none;
}

input[type='range']::-moz-range-thumb {
width: 28px;
height: 28px;
margin-top: -11px;
background: #999;
border: 1px solid #666;
border-radius: 50%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="row">
<div class="col-md-12">

<div class="form-group">

<div class="range-slider-wrap range-slider-lg">

<input class="range-slider ng-valid ng-dirty ng-touched" step="1" type="range" id="range-slider" min="1" max="4" data-tooltip-top="true" aria-valuenow="3" aria-valuemin="1" aria-valuemax="4">

<output class="range-slider-output num" id="range-slider-output" for="range-slider"></output>

</div>

</div>

</div>
</div>
</div>

最佳答案

关键是凹口不是简单地按百分比定位的,例如:

  • 第一 = 0%
  • 第二 = 33.3%
  • 第三 = 66.6%
  • 第四 = 100%

slider-thumb 的尺寸...在您的情况下为 28px。
并且在第一个和最后一个缺口之间有间距......
箭头也有宽度!

所以对齐计算会变得曲折...

我没有找到任何接近所有情况公式的东西。但是使用 calc()具有 “逻辑百分比” 和以 px 为单位的偏移量,它可以正确完成工作并且响应迅速。这只是几次尝试但未能找到正确偏移量的问题。

这是我从您的代码片段中更改的唯一几行:

input[aria-valuenow='1'] + .range-slider-output::before {
left: 11px;
}
input[aria-valuenow='2'] + .range-slider-output::before {
left: calc(33.3% - 4px);
}
input[aria-valuenow='3'] + .range-slider-output::before {
left: calc(66.6% - 20px);
}
input[aria-valuenow='4'] + .range-slider-output::before {
left: calc(100% - 36px);
}

我还添加了 Bootstrap 文件(在您的 fiddle 中)。

已更新 Fiddle

工作片段

$(function() {
var slider = document.getElementById("range-slider");
var output = document.getElementById("range-slider-output");

output.innerHTML = slider.value;

slider.oninput = function() {
output.innerHTML = this.value;
// resets state of user icon
$('.fa-user').removeClass("disabled");

if (output.innerHTML == 1) {
document.getElementById("range-slider").setAttribute("aria-valuenow", "1");
$('.range-slider-icons li:nth-of-type(2) span, .range-slider-icons li:nth-of-type(3) span, .range-slider-icons li:nth-of-type(4) span').addClass('disabled');

} else if (output.innerHTML == 2) {
document.getElementById("range-slider").setAttribute("aria-valuenow", "2");
$('.range-slider-icons li:nth-of-type(3) span, .range-slider-icons li:nth-of-type(4) span').addClass('disabled');

} else if (output.innerHTML == 3) {
document.getElementById("range-slider").setAttribute("aria-valuenow", "3");
$('.range-slider-icons li:nth-of-type(4) span').addClass('disabled');

} else {
document.getElementById("range-slider").setAttribute("aria-valuenow", "4");
}

}


});
.range-slider-output {
position: relative;
display: inline-block;
padding: 0.2em 0.75em 0.25em;
color: #fff;
text-align: center;
background: #666;
border-radius: 3px;
width: 100%;
left: calc(50%);
flex: 0 0 5%;
align-self: center;
margin: 0;
font-size: 28px;
-webkit-transform: translateX(-50%);
transform: translateX(-50%);
top: -92px;
}

.range-slider-output::before {
position: absolute;
top: 50%;
left: 0;
content: "";
}

.range-slider-lg .range-slider-output::before {
top: 48px;
width: 0;
height: 0;
border-style: solid;
border-width: 12px 12px 0 12px;
border-color: #666 transparent transparent transparent;
transition: all 0.7s ease-out;
}


.range-slider-wrap {
min-width: 250px;
}


input[aria-valuenow='1'] + .range-slider-output::before {
left: 11px;
}
input[aria-valuenow='2'] + .range-slider-output::before {
left: calc(33.3% - 4px);
}
input[aria-valuenow='3'] + .range-slider-output::before {
left: calc(66.6% - 20px);
}
input[aria-valuenow='4'] + .range-slider-output::before {
left: calc(100% - 36px);
}



input[type='range'] {
width: 100%;
cursor: pointer;
padding-top: 90px;
}
input[type='range'] {
-webkit-appearance: none;
}
input[type='range']::-webkit-slider-runnable-track {
width: 100%;
height: 5px;
background: #e6e5e5;
border: 1px solid #999;
border-radius: 3px;
-webkit-appearance: none;
padding: 0 0.5rem;
}

input[type='range']::-moz-range-track {
width: 100%;
height: 5px;
background: #e6e5e5;
border: 1px solid #999;
border-radius: 3px;
}

input[type=range]::-webkit-slider-thumb {
width: 28px;
height: 28px;
margin-top: -11px;
background: #999;
border: 1px solid #666;
border-radius: 50%;
-webkit-appearance: none;
}

input[type='range']::-moz-range-thumb {
width: 28px;
height: 28px;
margin-top: -11px;
background: #999;
border: 1px solid #666;
border-radius: 50%;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

<div class="container">
<div class="row">
<div class="col-md-12">

<div class="form-group">

<div class="range-slider-wrap range-slider-lg">

<input class="range-slider ng-valid ng-dirty ng-touched" step="1" type="range" id="range-slider" min="1" max="4" data-tooltip-top="true" aria-valuenow="3" aria-valuemin="1" aria-valuemax="4">

<output class="range-slider-output num" id="range-slider-output" for="range-slider"></output>

</div>

</div>

</div>
</div>
</div>

关于javascript - 范围 slider - 向下箭头跟随拇指,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55251118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com