使用曲线将输入范围弧形拇指连接到滑块

Posted

技术标签:

【中文标题】使用曲线将输入范围弧形拇指连接到滑块【英文标题】:Connecting input range arc thumb to slider with curve 【发布时间】:2020-10-24 15:18:02 【问题描述】:

我有一个已解决的问题Animating range ticks when moving range thumb。从那个问题,我有一个具有自定义外观的input type="range" - 拇指被做成一个弧形(半圆),span 采用范围的值并被设计为一个圆圈拇指和divmask 就像滴答声 - 步骤。


从这个预期的结果

我试图用曲线将该弧线连接到滑块。我尝试使用伪元素,但渐变与滑块上的渐变不同步,我无法制作如图所示的曲线。我还尝试使用 JS 画布 来绘制该曲线并将其放置在所需的位置,但渐变再次不同步 - 变为固定颜色。

我想使用 CSS 掩码,但我不确定是否可以用它制作想要的曲线。


这些是我的主要研究点:

CSS Tricks - Styling Cross-Browser Compatible Range Inputs Gradient Stroke Along Curve in Canvas - 试图用画布制作类似的曲线(连接点)

这是我的CodePen 和代码

// Position of span that shows range value and tick curve position
const tickContainer = document.getElementById('tickContainer');

const range = document.getElementById('range');
const rangeV = document.getElementById('rangeValue');
const setValue = () => 
  // Span position and inner value
  const newValue = Number((range.value - range.min) * 100 / (range.max - range.min));
  const newPosition = 35 - (newValue * 0.7);
  rangeV.style.left = `calc($newValue% + ($newPositionpx))`;
  rangeV.innerhtml = `<span>$range.value%</span>`;
  
  // Tick curve position
  tickContainer.style.setProperty('--p', `calc($newValue%)`);
;

// Initialize setValue onload and oninput
document.addEventListener("DOMContentLoaded", setValue);
range.addEventListener('input', setValue);
body 
  font-family: Arial;
  margin: 50px;


.range-wrap 
  position: relative;


/* Styling of ticks (lines) over the range */
.ticks 
  position: absolute;
  left: -15px;
  right: -15px;
  padding:0 15px;
  top: -30px;
  height: 45px;
  background: repeating-linear-gradient(to right, #D3D3D3 0 1px, transparent 1px 9px);
  background-clip:content-box;
  -webkit-mask: 
    radial-gradient(farthest-side at bottom,transparent 75%, #fff 76% 98%, transparent) 
      var(--p) 0px/100px 50px, 
    linear-gradient(#fff, #fff) var(--p) 100%/95px 10px,
    linear-gradient(#fff, #fff) bottom       /100% 10px;
  -webkit-mask-repeat: no-repeat;
  -webkit-mask-composite: source-over,destination-out;
  mask: 
    radial-gradient(farthest-side at bottom,transparent 75%, #fff 76% 98%, transparent) 
      var(--p) 0px/100px 50px, 
    linear-gradient(#fff, #fff) var(--p) 100%/95px 10px,
    linear-gradient(#fff, #fff) bottom       /100% 10px;
  mask-repeat: no-repeat;
  mask-composite: exclude;


/* Styling the range */
input[type=range] 
  -webkit-appearance: none;
  appearance: none;
  margin: 20px 0;
  width: 100%;
  height: 4px;
  background-image: linear-gradient(125deg, #e0e0e0 34%, rgb(0,12,110) 100%);
  outline: none;
  transition: all 100ms ease;


/* Range track */
input[type=range]::-webkit-slider-runnable-track 
  width: 100%;
  height: 4px;
  cursor: pointer;
  border-radius: 25px;


input[type=range]::-moz-range-track 
  width: 100%;
  height: 4px;
  cursor: pointer;
  border-radius: 25px;


/* Range thumb */
input[type=range]::-webkit-slider-thumb 
  height: 70px;
  width: 70px;
  -webkit-transform: translateY(-44.3%) rotate(-45deg);
          transform: translateY(-44.3%) rotate(-45deg);
  -webkit-appearance: none;
  appearance: none;
  background: #ddd;
  border: 3px solid transparent;
  border-color: transparent transparent #fff #fff;
  border-radius: 50%;
  cursor: pointer;
  background-image: linear-gradient(white, white), linear-gradient(to right, #e0e0e0 34%, rgb(0,12,110) 100%);
  background-attachment: fixed, fixed;
  background-clip: padding-box, border-box;
  transition: all 200ms ease;


input[type=range]::-moz-range-thumb 
  height: 63px;
  width: 63px;
  appearance: none;
  background: #ddd;
  border: 3px solid transparent;
  transition: all 200ms ease;
  border-color: transparent transparent #fff #fff;
  border-radius: 50%;
  cursor: pointer;
  background-image: linear-gradient(white, white), linear-gradient(to right, #e0e0e0 34%, rgb(0,12,110) 100%);
  background-attachment: fixed, fixed;
  background-clip: padding-box, border-box;


/* Range value (label) inside of range thumb */
.range-value 
  position: absolute;
  top: -50%;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  z-index: 99;
  user-select: none;
  select: none;
  pointer-events: none;


.range-value span 
  width: 50px;
  height: 50px;
  line-height: 50px;
  text-align: center;
  color: #fff;
  background: rgb(0,12,110);
  font-size: 18px;
  display: block;
  position: absolute;
  top: 20px;
  border-radius: 50%;
  user-select: none;
  select: none;
  pointer-events: none;
  z-index: 100;
<div class="range-wrap">
  <!-- Ticks (lines) over slider -->
  <div class="ticks" id="tickContainer">
  </div>
  <!-- Range value inside of range thumb -->
  <div class="range-value" id="rangeValue"></div>
  <!-- Range itself -->
  <input id="range" type="range" min="1" max="100" value="5" step="1">
</div>

【问题讨论】:

【参考方案1】:

这是一个不同的想法,我将像以前的答案一样依赖蒙版,但这次我将介绍弯曲部分的 SVG。我也会稍微优化一下代码,减少代码。

您会注意到,我对刻度和范围元素使用了相同的掩码,但使用了一些不同的值,因为刻度需要有更大的曲线。

// Position of span that shows range value and tick curve position
const tickContainer = document.querySelector('.range-wrap');

const range = document.getElementById('range');
const rangeV = document.getElementById('rangeValue');
const setValue = () => 
  // Span position and inner value
  const newValue = Number((range.value - range.min) * 100 / (range.max - range.min));
  const newPosition = 30 - (newValue * 0.6);
  rangeV.style.left = `calc($newValue% + ($newPositionpx))`;
  rangeV.innerHTML = `$range.value%`;
  
  // Tick curve position
  tickContainer.style.setProperty('--p', `calc($newValue%)`);
;

// Initialize setValue onload and oninput
document.addEventListener("DOMContentLoaded", setValue);
range.addEventListener('input', setValue);
body 
  font-family: Arial;
  margin: 50px;


.range-wrap 
  position: relative;
  --svg:url("data:image/svg+xml;utf8, <svg xmlns='http://www.w3.org/2000/svg' viewBox='0 15 64 50' preserveAspectRatio='none' ><path d='M0 64 C16 64 16 32 32 32 C48 32 48 64 64 64 L64 48 C52 48 52 16 32 16 C12 16 12 48 0 48 L0 64 Z' fill='white'/></svg>") var(--p,0) 0;


/* Styling of ticks (lines) over the range */
.ticks 
  --sw:120px; /* control the width of the curve */
  
  position: absolute;
  left: -30px;
  right: -30px;
  padding:0 10px;
  height: 50px;
  background: repeating-linear-gradient(to right, red 0 3px, transparent 1px 9px) content-box;
  -webkit-mask:var(--svg) /var(--sw) 50px,
     linear-gradient(to right, #fff calc(50% - var(--sw)/2 + 1px), transparent 0 calc(50% + var(--sw)/2 - 1px), #fff 0) 
     right var(--p) top 33px/calc(200% - var(--sw)) 16px;
  -webkit-mask-repeat:no-repeat;
  z-index:999;


/* Styling the range */
input[type=range] 
  --sw:100px; /* control the width of the curve */
  
  -webkit-appearance: none;
  appearance: none;
  margin: 20px 0 20px -20px;
  padding:0 20px;
  width:100%;
  height: 90px;
  -webkit-mask: 
    var(--svg) /var(--sw) 50px,
    linear-gradient(to right, #fff calc(50% - var(--sw)/2 + 1px), transparent 0 calc(50% + var(--sw)/2  - 1px), #fff 0) 
    right var(--p) top 33px/calc(200% - var(--sw)) 16px;
  -webkit-mask-repeat:no-repeat;
  background: linear-gradient(125deg, #e0e0e0 34%, rgb(0,12,110) 100%);
  outline: none;


/* Range track */
input[type=range]::-webkit-slider-runnable-track 
  width: 100%;
  height: 50px;
  cursor: pointer;
  border-radius: 25px;


input[type=range]::-moz-range-track 
  width: 100%;
  height: 50px;
  cursor: pointer;
  border-radius: 25px;


/* Range thumb */
input[type=range]::-webkit-slider-thumb 
  height: 60px;
  width: 60px;
  -webkit-appearance: none;
  appearance: none;
  border-radius: 50%;
  cursor: pointer;
  opacity:0;


input[type=range]::-moz-range-thumb 
  height: 60px;
  width: 60px;
  appearance: none;
  border-radius: 50%;
  cursor: pointer;
  opacity:0;


/* Range value (label) inside of range thumb */
.range-value 
  width: 50px;
  height: 50px;
  line-height: 50px;
  text-align: center;
  color: #fff;
  background: rgb(0,12,110);
  font-size: 18px;
  position: absolute;
  transform:translateX(-50%);
  top: 45px;
  border-radius: 50%;
  user-select: none;
  select: none;
  pointer-events: none;
<div class="range-wrap">
  <!-- Ticks (lines) over slider -->
  <div class="ticks" id="tickContainer">
  </div>
  <!-- Range value inside of range thumb -->
  <div class="range-value" id="rangeValue"></div>
  <!-- Range itself -->
  <input id="range" type="range" min="1" max="100" value="5" step="1">
</div>

更新

OP使用的最终版本:

// Position of span that shows range value and tick curve position
const tickContainer = document.querySelector('.range-wrap');

const range = document.getElementById('range');
const rangeV = document.getElementById('rangeValue');
const setValue = () => 
  // Span position and inner value
  const newValue = Number((range.value - range.min) * 100 / (range.max - range.min));
  const newPosition = 30 - (newValue * 0.6);
  rangeV.style.left = `calc($newValue% + ($newPositionpx))`;
  rangeV.innerHTML = `$range.value%`;
  
  // Tick curve position
  tickContainer.style.setProperty('--p', `calc($newValue%)`);
;

// Initialize setValue onload and oninput
document.addEventListener("DOMContentLoaded", setValue);
range.addEventListener('input', setValue);
body 
  font-family: Arial;
  margin: 0;
  min-height: 100vh;
  padding: 50px;
  box-sizing: border-box;
  text-align:center;


.range-wrap 
  position: relative;
  --svg:url("data:image/svg+xml;utf8, <svg width='97' height='37' viewBox='0 1.5 97 37' xmlns='http://www.w3.org/2000/svg'><path d='M0 35C14 35 13 2 48.5 2C84 2 80.5 35 97 35' fill='none' stroke='white' stroke-width='4'/></svg>") var(--p,0) 0;
</svg>


/* Styling of ticks (lines) over the range */
.ticks 
  --sw:120px; /* control the width of the curve */
  
  position: absolute;
  left: -30px;
  right: -30px;
  top: 0px;
  padding:0 10px;
  height: 50px;
  background: repeating-linear-gradient(to right, #D3D3D3 0 1px, transparent 1px 10px) content-box;
  -webkit-mask:var(--svg) /var(--sw) 50px,
     linear-gradient(to right, #fff calc(50% - var(--sw)/2 + 1px), transparent 0 calc(50% + var(--sw)/2 - 1px), #fff 0) 
     right var(--p) top 38px/calc(200% - var(--sw)) 6px;
  -webkit-mask-repeat:no-repeat;
  z-index:999;


/* Styling the range */
input[type=range] 
  --sw:100px; /* control the width of the curve */
  
  -webkit-appearance: none;
  appearance: none;
  margin: 20px 0 20px -20px;
  padding:0 20px;
  width: 100%;
  height: 60px;
  -webkit-mask: 
    var(--svg) /var(--sw) 41px,
    linear-gradient(to right, #fff calc(50% - var(--sw)/2 + 1px), transparent 0 calc(50% + var(--sw)/2  - 1px), #fff 0) 
    right var(--p) top 34.45px/calc(200% - var(--sw)) 4px;
  -webkit-mask-repeat:no-repeat;
  background: linear-gradient(125deg, #e0e0e0 34%, rgb(0,12,110) 100%);
  outline: none;


/* Range track */
input[type=range]::-webkit-slider-runnable-track 
  width: 100%;
  height: 50px;
  cursor: pointer;
  border-radius: 25px;


input[type=range]::-moz-range-track 
  width: 100%;
  height: 50px;
  cursor: pointer;
  border-radius: 25px;


/* Range thumb */
input[type=range]::-webkit-slider-thumb 
  height: 60px;
  width: 60px;
  -webkit-appearance: none;
  appearance: none;
  border-radius: 50%;
  cursor: pointer;
  opacity:0;


input[type=range]::-moz-range-thumb 
  height: 60px;
  width: 60px;
  appearance: none;
  border-radius: 50%;
  cursor: pointer;
  opacity:0;


/* Range value (label) inside of range thumb */
.range-value 
  width: 55px;
  height: 55px;
  line-height: 60px;
  text-align: center;
  color: #fff;
  background: rgb(0,12,110);
  font-size: 18px;
  position: absolute;
  transform:translateX(-50%);
  top: 32px;
  border-radius: 50%;
  user-select: none;
  select: none;
  pointer-events: none;
<h2>Custom range slider with ticks</h2>

<div class="range-wrap">
  <!-- Ticks (lines) over slider -->
  <div class="ticks" id="tickContainer">
  </div>
  <!-- Range value inside of range thumb -->
  <div class="range-value" id="rangeValue"></div>
  <!-- Range itself -->
  <input id="range" type="range" min="1" max="100" value="5" step="1">
</div>

【讨论】:

不错的答案。不过,是否有可能将圆弧和滑块的厚度更改为原来的 4px 或一般的任何其他厚度? @Vepthy 是的,你可以,你必须使用掩码大小和其他值。将尝试编辑以显示如何完成,但您可以开始使用该值,您会理解 另外,如果我要降低刻度或使它们和滑块之间的间隙更大,我应该用弯曲的刻度控制什么以获得相同的空间?这是我一直在尝试的。 CodePen 我尝试将滑块的高度从 50px 降低到 4px 并调整其范围,但它只是消失在刻度后面。 @Vepthy 不,就像更改高度一样简单,您需要更改蒙版内的很多值,高度需要保持不变。对于间隙,例如添加刻度`top:-2px;`并使用它来控制间隙

以上是关于使用曲线将输入范围弧形拇指连接到滑块的主要内容,如果未能解决你的问题,请参考以下文章

移动范围拇指时动画范围刻度

IE和Edge中的范围滑块拇指css问题

在 React 中,如何在范围滑块的拇指上创建一个气泡头

如何找到滑块的拇指来设置其宽度

如何使用步进滑块在滑块拇指上添加 UILabel?

如何将另一个图像添加到滑块的拇指或如何添加两个拇指图像?