cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <input 
  id="range-slider-input"
  type="range"
  role="slider"
  aria-valuemin="100"
  aria-valuemax="1000"
  aria-valuetext="$500"
  min="100"
  max="1000"
  step="100"
  value="400"
  labels='{
    "100": "$100",
    "200": "$200",
    "300": "$300",
    "400": "$400",
    "500": "$500",
    "600": "$600",
    "700": "$700",
    "800": "$800",
    "900": "$900",
    "1000": "$1000"
  }'
 >
            
          
!
            
              @import url('https://fonts.googleapis.com/css?family=Montserrat:300,300i,400,400i,500,500i,600,600i,700,700i,800,800i,900,900i|Zilla+Slab:300,400,500,600,700');

$blue: #2294d6;
$light-blue: #d8e0f3;
$light-gray: #eee;

@function remify($values) {
    $rems: ();

    @each $value in $values {
        @if $value == 0 or $value == auto {
            $rems: append($rems, $value);
        } @else {
            $rems: append(
                $rems,
                ($value / 17) + rem
            );
        }
    }

    @return $rems;
}

body {
  font-family: 'Montserrat', Arial, Helvetica, sans-serif;
  font-weight: 300;
  padding: remify(32);
}

.range-slider {
  display: flex;
  align-items: center;
  position: relative;
  width: 100%;
  
  input[type='range'] {
    -webkit-appearance: none;
    -moz-appearance: none;
    background: $light-blue;
    width: calc(100% - 1px);
    height: remify(5);
    border-radius: remify(5);
    border: 0;
    margin: 0;
    padding: 0;
    position: relative;
    z-index: 3;

    &:focus {
      outline: 0;
      border-color: lighten($blue, 35%);
      
      &::-webkit-slider-thumb {
        background-color: darken($blue, 10%);
      }
      
      &::-moz-range-thumb {
        background-color: darken($blue, 10%);
      }
      
      &::-ms-thumb {
        background-color: darken($blue, 10%);
      }
    }
  }

  ::-webkit-slider-thumb {
    -webkit-appearance: none;
    border: remify(13) solid $blue;
    background-color: white;
    border-radius: 50%;
    width: remify(36);
    height: remify(36);
    box-shadow: remify(0 0 0 2) white;
    cursor: pointer;
  }

  ::-moz-range-thumb {
    appearance: none;
    -moz-appearance: none;
    border: remify(13) solid $blue;
    background-color: white;
    border-radius: 50%;
    width: remify(16);
    height: remify(16);
    box-shadow: remify(0 0 0 2) white;
    cursor: pointer;
  }

  ::-ms-thumb {
    border: remify(2) solid $blue;
    background-color: white;
    border-radius: 50%;
    width: remify(8);
    height: remify(8);
    cursor: pointer;
  }

  ::-webkit-runnable-track {
    -webkit-appearance: none;
    width: 0;
  }

  ::-moz-range-track {
    -moz-appearance: none;
    width: 0; // Firefox needs an explicit width for this to not show.
  }

  ::-ms-track {
    border-color: transparent;
    color: transparent;
    background-color: transparent;
  }

  ::-ms-fill-upper {
    background: transparent;
  }

  ::-ms-fill-lower {
    background: $blue;
    margin-left: remify(-2); // Fixes a gap in rendering in IE 11.
    height: remify(20);
    border-color: $blue;
    border-width: remify(20 0);
  }

  ::-ms-tooltip {
    display: none;
  }
}

.range-slider-ticks {
  position: absolute;
  top: remify(-5); 
  z-index: 2;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-between;
  margin: 0 9px;
  
  &__tick {
    background: $light-blue;
    display: inline-flex;
    justify-content: center;
    width: remify(15);
    height: remify(15);
    border-radius: 50%;
    position: relative;
    cursor: pointer;
  }
  
  &__label {
    font-size: remify(14);
    padding-top: remify(28);
    
    &.is-selected {
      font-weight: 600;
    }
  }
}
            
          
!
            
              const createUniqueId = prefix => {
  return `${prefix}-${Math.round(Math.random() * 100000)}`;
}

class RangeSlider {
  
  constructor(el) {
    this.input = document.querySelector(el);
    this.minValue = this.input.getAttribute('min');
    this.maxValue = this.input.getAttribute('max');
    this.numTicks = this.maxValue / this.minValue;
    
    this.input.addEventListener('change', (e) => this.handleInputChange(e));
    
    this.wrapInput().then(res => {
      if (this.input.hasAttribute('labels')) {
        this.labels = JSON.parse(this.input.getAttribute('labels'));
        this.createTicks(res);
      }
    });
  }
  
  handleInputChange(e) {
    const value = e.target.value;
    const ariaValueText = !this.labels ? value : this.labels[value];
    
    this.input.setAttribute('aria-valuetext', ariaValueText);
    
    if (this.input.hasAttribute('labels')) {
      this.setSelectedLabel(this.labels[value]); 
    }
  }
  
  handleLabelClick(label, value, e) {
    this.input.focus();
    this.input.value = value;
    this.input.setAttribute('value', value);
    this.input.setAttribute('aria-valuetext', label); 
    this.setSelectedLabel(label);
  }
  
  setSelectedLabel(label) {
    const selectedLabels = document.querySelectorAll('.range-slider-ticks__label');
    
    [].forEach.call(selectedLabels, function(el) {
      el.classList[el.innerText === label ? 'add' : 'remove']('is-selected'); 
    });
  }
  
  wrapInput() {
    return new Promise(resolve => {
      const wrapper = document.createElement('div');
      const wrapperId = createUniqueId('range-slider');

      wrapper.id = wrapperId;
      wrapper.className = 'range-slider';
      
      this.input.parentNode.replaceChild(wrapper, this.input);

      document.querySelector(`#${wrapperId}`).appendChild(this.input);
      
      resolve(wrapperId);
    });
  }
  
  createTicks(wrapperId) {
    return new Promise(resolve => {
      const tickList = document.createElement('div');
      const tickListId = createUniqueId('tick-list');

      tickList.id = tickListId;
      tickList.className = 'range-slider-ticks';

      document.querySelector(`#${wrapperId}`).appendChild(tickList);
      
      for (const prop in this.labels) {
        if (this.labels.hasOwnProperty(prop)) {
           
          const tick = document.createElement('div'); 
          const tickLabel = document.createElement('span'); 
          const tickLabelText = document.createTextNode(this.labels[prop]);

          tickLabel.className = `
            range-slider-ticks__label ${this.input.value === prop ? 'is-selected' : ''}
          `;
          
          tickLabel.appendChild(tickLabelText);

          tick.className = 'range-slider-ticks__tick'; 
          tick.addEventListener('click', (e) => this.handleLabelClick(this.labels[prop], prop, e));
          tick.appendChild(tickLabel);
          
          document.querySelector(`#${tickListId}`).appendChild(tick);
        }
      }

      resolve();
    });
  }
}

new RangeSlider('#range-slider-input');
            
          
!
999px
Loading ..................

Console