【问题标题】:Round range slider with text and different color带有文本和不同颜色的圆形范围滑块
【发布时间】:2020-10-04 04:03:16
【问题描述】:

我想做一个有 4 种不同颜色的圆形滑块,基本上,圆形滑块应该是四个部分,每个部分代表 25% 不同的颜色,当有人触发 25% 时,滑块颜色应该改变,它的值在文本也应该改变,比如如果第一部分背景颜色是红色,那么它的文本是 name1,第二部分背景颜色应该是绿色,它的文本是 name2,第三和第四部分也应该有不同的颜色和文本。@987654321 @

作为参考,请看图片,我想实现与附图中相同的功能,请你帮忙,我该如何开发它?

【问题讨论】:

  • 我认为this 会帮助你

标签: javascript html jquery round-slider


【解决方案1】:

与其开发自己的插件,不如使用roundSlider 插件,然后我根据您的要求制作了一个演示。

由于编写自定义逻辑需要考虑更多代码和检查点。

查看下面的演示,您仍然可以进行更多自定义。

DEMO

截图:

【讨论】:

    【解决方案2】:

    如果您不能为此使用任何 3rd 方库,我想您可能会对 this 感兴趣

    最糟糕的部分是拖动按钮,所以它只能沿着圆形边框移动

    这里有点小问题

    $(function() {
    
      const svg = document.querySelector("svg");
      const circleButton = document.querySelector("#circle-button");
      const circle = document.querySelector("#circle2");
      const baseX = Number(circle.getAttributeNS(null, "cx"));
      const baseY = Number(circle.getAttributeNS(null, "cy"));
      const radius = Number(circle.getAttributeNS(null, "r"));
      
      let sliderHandle = null;
      let value = 0;
      let chunk = Math.PI * radius * 2 / 10;
      
      $("#start").click(function() {
      
        if(sliderHandle) {
          $(this).text('Start Slider');
          clearTimeout(sliderHandle);
          sliderHandle = null;
          return;
        }
      
        const loop = () => {
          circle.setAttributeNS(null, "stroke-dashoffset", value);
          value = value - chunk;
          sliderHandle = setTimeout(loop, 500);
        }
        
        $(this).text('Stop Slider');
        sliderHandle = setTimeout(loop, 500);
      })
      
      let handle = null;
      
      $("#move-button").click(function() {
        if(handle) {
          clearTimeout(handle);
          handle = null;
          $(this).text('Move Button');
          return;
        }
        $(this).text('Stop Button');
        runTimer();
      }) 
      
      
      svg.addEventListener('mousedown', startDrag);
      svg.addEventListener('mousemove', drag);
      svg.addEventListener('mouseup', endDrag);
      svg.addEventListener('mouseleave', endDrag);
      
      function getMousePosition({clientY, clientX}) {
        const {a, d, e, f} = svg.getScreenCTM();
    
        return {
          x: (clientX - e) / a,
          y: (clientY - f) / d
        };
      }
      
      let offset = {
        x: -1,
        y: -1
      }
      
      let isDragging = false;
      let selectedElement = null;
      
      function startDrag(event) {
        selectedElement = event.target;
        isDragging = true;
        offset = getMousePosition(event);
        offset.x -= parseFloat(circle.getAttributeNS(null, "cx"));
        offset.y -= parseFloat(circle.getAttributeNS(null, "cy"));
      }
      
      function drag(event) {
      
        if(!isDragging) {
          return;
        }
        
        event.preventDefault();
        const coord = getMousePosition(event);
        
        const cx = coord.x - offset.x;    
        const cy = coord.y - offset.y;    
        
        selectedElement.setAttributeNS(null, "cx", cx);
        selectedElement.setAttributeNS(null, "cy", cy);
      }
      
      const inc = Math.PI / 90;
      let t = 0;
      
      function runTimer() {
        const loop = () => {
          const cx = baseX + radius * Math.cos(t);
          const cy = baseY + radius * Math.sin(t);
          t = t + inc;
    
          circleButton.setAttributeNS(null, "cx", cx);
          circleButton.setAttributeNS(null, "cy", cy);
          handle = setTimeout(loop, 100);
        }
        
        handle = setTimeout(loop, 100);
      }
      
      function endDrag(event) {
        selectedElement = null;
        isDragging = false;
      }
      
    })
    #circle2, #circle-button {
      transition: all .2s ease-in;
    }
    
    #circle-button {
      cursor: pointer;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <svg xmlns="http://www.w3.org/2000/svg"
        width="200" height="200">
    <circle
      cx="100" cy="100" r="50"
      fill="none"
      stroke="gray"
      stroke-width="10"
      ></circle>
    <circle
      id="circle2"
      cx="100" cy="100" r="50"
      fill="none"
      stroke="red"
      stroke-width="10"
      stroke-dasharray="320"
      stroke-dashoffset="0"></circle>
      <circle
      id="circle-button"
      cx="150"
      cy="100"
      r="12"
      fill="black"></circle>
    </svg>
    <button id="start">Start Slider</button>
    <button id="move-button">Move Button</button>

    编码愉快!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-27
      • 2020-06-14
      • 1970-01-01
      • 2023-02-04
      • 2021-03-29
      • 2019-05-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多