javascript - 还可以通过重置按钮更改自定义滑块的渐变
问题描述
//Start FOnt Size Slider
function isOlderEdgeOrIE() {
return (
window.navigator.userAgent.indexOf("MSIE ") > -1 ||
!!navigator.userAgent.match(/Trident.*rv\:11\./) ||
window.navigator.userAgent.indexOf("Edge") > -1
);
}
function valueTotalRatio(value, min, max) {
return ((value - min) / (max - min)).toFixed(2);
}
function getLinearGradientCSS(ratio, leftColor, rightColor) {
return [
'-webkit-gradient(',
'linear, ',
'left top, ',
'right top, ',
'color-stop(' + ratio + ', ' + leftColor + '), ',
'color-stop(' + ratio + ', ' + rightColor + ')',
')'
].join('');
}
function updateRangeEl(rangeEl) {
var ratio = valueTotalRatio(rangeEl.value, rangeEl.min, rangeEl.max);
rangeEl.style.backgroundImage = getLinearGradientCSS(ratio, '#8e2f58', '#c5c5c5');
}
function initRangeEl() {
var rangeEl = document.querySelector('input[type=range]');
var textEl = document.querySelector('.fntSz');
/**
* IE/Older Edge FIX
* On IE/Older Edge the height of the <input type="range" />
* is the whole element as oposed to Chrome/Moz
* where the height is applied to the track.
*
*/
if (isOlderEdgeOrIE()) {
rangeEl.style.height = "20px";
// IE 11/10 fires change instead of input
// https://stackoverflow.com/a/50887531/3528132
rangeEl.addEventListener("change", function(e) {
textEl.value = e.target.value;
});
rangeEl.addEventListener("input", function(e) {
textEl.value = e.target.value;
});
} else {
updateRangeEl(rangeEl);
rangeEl.addEventListener("input", function(e) {
updateRangeEl(e.target);
textEl.value = e.target.value;
});
}
}
initRangeEl();
//End Font Size Slider
$('.resetbtn').click(function(){
$('#slider, .fntSz').val(40);
});
/*Start Font Size Slilder*/
.fntSz{
outline: none;
text-align: center;
font-family: 'Roboto';
font-weight: 400;
font-size: initial;
color: #757575;
}
input[type="range"] {
-webkit-appearance: none;
-moz-appearance: none;
width: 100%;
height: 2px;
padding: 0;
border-radius: 2px;
outline: none;
cursor: pointer;
}
/*Chrome thumb*/
input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none;
-moz-appearance: none;
-webkit-border-radius: 5px;
/*16x16px adjusted to be same as 14x14px on moz*/
height: 15px;
width: 15px;
border-radius: 50%;
background: #8e2f58;
}
/*Mozilla thumb*/
input[type="range"]::-moz-range-thumb {
-webkit-appearance: none;
-moz-appearance: none;
-moz-border-radius: 5px;
height: 15px;
width: 15px;
border-radius: 50%;
background: #8e2f58;
}
/*IE & Edge input*/
input[type=range]::-ms-track {
width: 300px;
height: 6px;
/*remove bg colour from the track, we'll use ms-fill-lower and ms-fill-upper instead */
background: transparent;
/*leave room for the larger thumb to overflow with a transparent border */
border-color: transparent;
border-width: 2px 0;
/*remove default tick marks*/
color: transparent;
}
/*IE & Edge thumb*/
input[type=range]::-ms-thumb {
height: 14px;
width: 14px;
border-radius: 50%;
background: #8e2f58;
}
/*IE & Edge left side*/
input[type=range]::-ms-fill-lower {
background: #8e2f58;
border-radius: 2px;
}
/*IE & Edge right side*/
input[type=range]::-ms-fill-upper {
background: #8e2f58;
border-radius: 50%;
}
/*IE disable tooltip*/
input[type=range]::-ms-tooltip {
display: none;
}
input[type="text"] {
border: none;
}
/*End Font Size Slilder*/
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="range" value="40" min="10" max="100" step="1" id="slider" />
<button type="button" name="button" class="resetbtn" value="Reset form"> Reset </button>
你好,这里我使用一个滑块,一个自定义滑块,我在指针之前和指针之后使用两种颜色,然后我创建一个重置按钮,它重置输入(滑块输入)的值,它也很好用,但问题在于颜色当我们点击重置按钮颜色没有重置。我该如何解决?
解决方案
您应该initRangeEl();
在单击重置按钮时再次调用,让它根据滑块值重新绘制元素的样式。
$('.resetbtn').click(function(){
$('#slider, .fntSz').val(40);
initRangeEl();
});
//Start FOnt Size Slider
function isOlderEdgeOrIE() {
return (
window.navigator.userAgent.indexOf("MSIE ") > -1 ||
!!navigator.userAgent.match(/Trident.*rv\:11\./) ||
window.navigator.userAgent.indexOf("Edge") > -1
);
}
function valueTotalRatio(value, min, max) {
return ((value - min) / (max - min)).toFixed(2);
}
function getLinearGradientCSS(ratio, leftColor, rightColor) {
return [
'-webkit-gradient(',
'linear, ',
'left top, ',
'right top, ',
'color-stop(' + ratio + ', ' + leftColor + '), ',
'color-stop(' + ratio + ', ' + rightColor + ')',
')'
].join('');
}
function updateRangeEl(rangeEl) {
var ratio = valueTotalRatio(rangeEl.value, rangeEl.min, rangeEl.max);
rangeEl.style.backgroundImage = getLinearGradientCSS(ratio, '#8e2f58', '#c5c5c5');
}
function initRangeEl() {
var rangeEl = document.querySelector('input[type=range]');
var textEl = document.querySelector('.fntSz');
/**
* IE/Older Edge FIX
* On IE/Older Edge the height of the <input type="range" />
* is the whole element as oposed to Chrome/Moz
* where the height is applied to the track.
*
*/
if (isOlderEdgeOrIE()) {
rangeEl.style.height = "20px";
// IE 11/10 fires change instead of input
// https://stackoverflow.com/a/50887531/3528132
rangeEl.addEventListener("change", function(e) {
textEl.value = e.target.value;
});
rangeEl.addEventListener("input", function(e) {
textEl.value = e.target.value;
});
} else {
updateRangeEl(rangeEl);
rangeEl.addEventListener("input", function(e) {
updateRangeEl(e.target);
textEl.value = e.target.value;
});
}
}
initRangeEl();
//End Font Size Slider
$('.resetbtn').click(function(){
$('#slider, .fntSz').val(40);
initRangeEl();
});
/*Start Font Size Slilder*/
.fntSz{
outline: none;
text-align: center;
font-family: 'Roboto';
font-weight: 400;
font-size: initial;
color: #757575;
}
input[type="range"] {
-webkit-appearance: none;
-moz-appearance: none;
width: 100%;
height: 2px;
padding: 0;
border-radius: 2px;
outline: none;
cursor: pointer;
}
/*Chrome thumb*/
input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none;
-moz-appearance: none;
-webkit-border-radius: 5px;
/*16x16px adjusted to be same as 14x14px on moz*/
height: 15px;
width: 15px;
border-radius: 50%;
background: #8e2f58;
}
/*Mozilla thumb*/
input[type="range"]::-moz-range-thumb {
-webkit-appearance: none;
-moz-appearance: none;
-moz-border-radius: 5px;
height: 15px;
width: 15px;
border-radius: 50%;
background: #8e2f58;
}
/*IE & Edge input*/
input[type=range]::-ms-track {
width: 300px;
height: 6px;
/*remove bg colour from the track, we'll use ms-fill-lower and ms-fill-upper instead */
background: transparent;
/*leave room for the larger thumb to overflow with a transparent border */
border-color: transparent;
border-width: 2px 0;
/*remove default tick marks*/
color: transparent;
}
/*IE & Edge thumb*/
input[type=range]::-ms-thumb {
height: 14px;
width: 14px;
border-radius: 50%;
background: #8e2f58;
}
/*IE & Edge left side*/
input[type=range]::-ms-fill-lower {
background: #8e2f58;
border-radius: 2px;
}
/*IE & Edge right side*/
input[type=range]::-ms-fill-upper {
background: #8e2f58;
border-radius: 50%;
}
/*IE disable tooltip*/
input[type=range]::-ms-tooltip {
display: none;
}
input[type="text"] {
border: none;
}
/*End Font Size Slilder*/
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="range" value="40" min="10" max="100" step="1" id="slider" />
<button type="button" name="button" class="resetbtn" value="Reset form"> Reset </button>
推荐阅读
- windows - 使用 Powershell 创建交互式计划任务
- r - 使用 sep=" \ " 时出现单独的 {tidyr} 错误的问题
- python - 如何键入仅使用构造函数可能返回类型的子集的函数?
- json - 如何使用 jq 计算中位数?
- python - 尝试通过 pygame 播放歌曲但出现 libmpg123-0.dll 错误
- java - 可空的原始长函数参数,你能检查传递的值是否确实为空吗?
- knockout.js - 为什么 Knockoutjs 不能使用单独文件中的脚本?
- haskell - 为什么 putStrLn 每次迭代都会变慢?
- angular - 在 mat-table 中添加新行时出现错误
- oracle - 在 BLOB for Work 或 Excel 文件中查找和替换字符串