javascript - 箭头位置问题 - 范围输入
问题描述
我在网上找到了一个符合我期望的输入范围脚本。我开始自己处理。一切正常,除了点下的云位置。我尝试了不同的方法,但脚本仍然无法正常工作。无论位置如何,我都希望云中的三角形始终位于点下方。
$.fn.WBslider = function () {
return this.each(function () {
var $_this = $(this),
$_date = $('input', $_this),
$_title = $('.setyear', $_this),
thumbwidth = 85, // set this to the pixel width of the thumb
yrnow = 200;
$_date.on('input change keyup', function () {
var $_this = $(this),
val = parseInt($_date.val(), 10);
if (val < 70) {
val = '< 70';
}
if (val === '') { // Stop IE8 displaying NaN
val = 0;
}
$_title.text(val);
var pos = (val - $_date.attr('min')) / ($_date.attr('max') - $_date.attr('min'));
// position the title with the thumb
var thumbCorrect = thumbwidth * (pos - 0.5) * -1,
titlepos = Math.round((pos * $_date.width()) - thumbwidth / 4 + thumbCorrect);
$_title.css({
'left': titlepos
});
// show "progress" on the track
pos = Math.round(pos * 99); // to hide stuff behide the thumb
var grad = 'linear-gradient(90deg, #fb824f ' + pos + '%,#e2e5ec ' + (pos + 1) + '%)';
$_date.css({
'background': grad
});
}).on('focus', function () {
if (isNaN($(this).val())) {
$(this).val(0);
}
}).trigger('change');
$(window).on('resize', function () {
$_date.trigger('change');
});
});
};
$(function () {
$('.slider').WBslider();
});
.startyear,
.endyear {
float: left;
color: #333;
text-align: right;
font-weight: bold;
}
.endyear {
text-align: left;
}
.setyear {
position: absolute;
bottom: -65px;
left: 50%;
text-align: center;
font-weight: bold;
white-space: nowrap;
min-width: 85px;
box-shadow: 1px 3px 5px #d5d7db;
border-radius: 5px;
border: 1px solid #e4e7eb;
background-color: #ffffff;
padding: 10px 25px;
color: #494e53;
font-size: 12px;
font-weight: 600;
}
.setyear:before {
position: absolute;
margin-top: -21px;
content: URL("https://image.ibb.co/eSib69/range_arrow.png");
background-color: transparent;
}
.range {
position: relative;
float: left;
padding: 0 0.9375rem;
}
input[type=range] {
-webkit-appearance: none;
display: block;
height: 4px;
padding: 0;
background: #FFE014;
box-sizing: content-box;
border-right: 0 !important;
}
input[type=range]:focus {
outline: none;
}
input[type=range]::-webkit-slider-thumb {
-webkit-appearance: none;
width: 20px;
height: 20px;
border: 7px solid #fff;
border-radius: 25px;
background: #fb824f;
box-shadow: 0 2px 9px rgba(185, 185, 185, 0.75);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<div class="slider">
<span class="startyear">max. 250 kg</span>
<div class="range">
<input type="range" name="date" id="date1" min="70" max="200" step="1" value="Please enter the year of build" required>
<span class="setyear"></span>
</div>
<span class="endyear">max. 250 kg</span>
</div>
</form>
解决方案
我试图通过线性渐变找出类型范围的位置。它可能不准确,但在某种程度上,它确实有效。
$.fn.WBslider = function() {
return this.each(function() {
var $_this = $(this),
$_date = $('input', $_this),
$_title = $('.setyear', $_this),
thumbwidth = 85, // set this to the pixel width of the thumb
yrnow = 200;
$_date.on('input change keyup', function() {
var $_this = $(this),
val = parseInt($_date.val(), 10);
if (val < 70) {
val = '< 70';
}
if (val === '') { // Stop IE8 displaying NaN
val = 0;
}
$_title.text(val);
var pos = (val - $_date.attr('min')) / ($_date.attr('max') - $_date.attr('min'));
// position the title with the thumb
var thumbCorrect = thumbwidth * (pos - 0.5) * -1,
titlepos = Math.round((pos * $_date.width()) - thumbwidth / 4 + thumbCorrect);
if ($("#date1").attr("style") != undefined) {
titlepos = 23 + parseFloat($("#date1").attr("style").split(" ").pop().split("%")[0]);
} else {
titlepos = 70;
}
$_title.css({
'left': titlepos
});
// show "progress" on the track
pos = Math.round(pos * 99); // to hide stuff behide the thumb
var grad = 'linear-gradient(90deg, #fb824f ' + pos + '%,#e2e5ec ' + (pos + 1) + '%)';
$_date.css({
'background': grad
});
}).on('focus', function() {
if (isNaN($(this).val())) {
$(this).val(0);
}
}).trigger('change');
$(window).on('resize', function() {
$_date.trigger('change');
});
});
};
$(function() {
$('.slider').WBslider();
});
推荐阅读
- javascript - 无法用更具体的样式覆盖 CSS 模块类样式
- html - 导出html时,如何在org-export-dispatch中使用htlatex或xhlatex?
- python - 如何从 Flask GET 请求中提取 JSON 列表值?
- image-processing - 如何将 image::ImageBuffer 转换为 image::DynamicImage?
- r - : 数据必须是数据框,或其他可被 fortify() 强制转换的对象,而不是 gg/ggplot 类的 S3 对象
- c# - 从 Activator.CreateInstance() 处理实例
- reactjs - 在 react 和 json.server 中更新对象内的数组
- architecture - 您可以使用转发删除所有依赖项吗?
- c# - 合并接收函数参数的函数
- python - 仅在特定点工作的两个图像之间使用蒙版的碰撞