代码之家  ›  专栏  ›  技术社区  ›  Seth Lutske

带有样式化组件的范围滑块手柄拇指中心位于边缘上方

  •  0
  • Seth Lutske  · 技术社区  · 5 年前

    我有一个范围滑块( <input type="range"> )我用react样式的组件来设计。我遵循了这本书的大意 Custom styling an input slider using styled-components ,并且我的样式正常工作:

    import styled, { css } from "styled-components";
    
    const trackH, thumbH, thumbD, thumbC, trackC, filllC; // define some values
    
    const track = css`
      box-sizing: border-box;
      height: 4px;
      background: ${trackC};
      border-radius: 0px;
      border: 1px solid rgb(155, 155, 155);
      margin-right: -1px;
    `;
    
    const trackFill = css`
      ${track};
      height: ${trackH};
    `;
    
    const fill = css`
      height: ${trackH};
      background: ${filllC};
      border-radius: 4px;
    `;
    
    const thumb = css`
      box-sizing: border-box;
      width: ${thumbD};
      height: ${thumbH};
      position: "absolute";
      border-radius: 3px;
      background: ${thumbC};
      box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.4);
      cursor: pointer;
    `;
    
    /**
     * Styled input slider component
     */
    const Input = styled.input`
      width: 100%;
    
      &,
      &::-webkit-slider-thumb {
        -webkit-appearance: none;
      }
    
      &:focus {
        outline: none;
      }
    
      &:focus::-webkit-slider-thumb {
        outline: -webkit-focus-ring-color auto 5px;
      }
    
      &:focus::-moz-range-thumb {
        outline: -webkit-focus-ring-color auto 5px;
      }
    
      &:focus::-ms-thumb {
        outline: -webkit-focus-ring-color auto 5px;
      }
    
      --range: calc(var(--max) - var(--min));
      --ratio: calc((var(--val) - var(--min)) / var(--range));
      --sx: calc(0.5 * ${thumbH} + var(--ratio) * (100% - ${thumbH}));
    
      margin: 0;
      padding: 0;
      height: ${thumbH};
      background: transparent;
      font: 1em/1 arial, sans-serif;
    
      // more firefox and ms specific styling here ...
    `;
    
    

    但是,我希望设置轨迹/拇指关系的样式,以便当值位于滑块的最小值或最大值时,拇指应该是 居中的 越过跑道的边缘。现在,当滑块处于最小值时,拇指的左边缘与轨迹的左端齐平,当滑块处于最大值时,拇指的右边缘与轨迹的右端齐平。我要拇指 居中的 在跑道的每一端,当在末端时。

    enter image description here

    enter image description here

    (拇指应位于上图中0或24的中心)

    我看到了这个问题 HTML Range styling firefox. Get the thumb to center over edges ,一个很好的解决方案就在那里 圆形 拇指手柄。我想要一个长方形的把手,所以这个解决方案不适合我。还有 How to offset a rectangular thumb on a Range Slider ,但这种解决方案与其说是以边缘为中心,不如说是防止溢出。我似乎无法让解决方案对我的情况产生任何影响。

    Here is a codesandbox showing my slider .为了阐明这一点,我使用 react-compound-slider ,但请记住,我的实际滑块完全是使用样式化组件自制的。但是,记号确实会高亮显示,虽然它们与轨迹对齐,但控制柄本身需要位于边缘的中心,滑块的值才能与记号对齐。我怎样才能做到这一点?

    0 回复  |  直到 5 年前