Pen Settings

HTML

CSS

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.

+ add another resource

JavaScript

Babel includes JSX processing.

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.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="content">
  <div id="my-slider" se-min="-100" se-step="1" se-min-value="40" se-max-value="40" se-max="100" class="slider">
    <div class="slider-touch-left">
      <span></span>
    </div>
    <div class="slider-touch-right">
      <span></span>
    </div>
    <div class="slider-line">
      <span></span>
    </div>
  </div>
</div>
<div id="result">Min: 0 Max: 140</div>
              
            
!

CSS

              
                .border-box(@val: border-box) {
    -webkit-box-sizing: @val;
    -moz-box-sizing:    @val;
    box-sizing:         @val;
}

.content {
  width: 320px;
}

.slider {
	display: block;
	position: relative;
	height: 36px;
	width: 100%;
	-webkit-user-select: none;
	-moz-user-select: none;
	-ms-user-select:none;
	-o-user-select:none;
	user-select:none;

	.slider-touch-left, .slider-touch-right {
		.border-box();
		display: block;
		position: absolute;
		height: 36px;
		width: 36px;
		padding: 6px;
		z-index: 2;
		
		span {
			display: block;
			width: 100%;
			height: 100%;
			background: #F0F0F0;
			border: 1px solid #A4A4A4;
			border-radius: 50%;
		}
	}

	.slider-line {
		.border-box();
		position: absolute;
		width: calc(~"100% - 36px");
		left: 18px;
		top: 16px;
		height: 4px;
		border-radius: 4px;
		background: #F0F0F0;
		z-index: 0;
		overflow: hidden;

		span {
			display: block;
			height: 100%;
			width: 0%;
			background: orange;
		}
	}
}
              
            
!

JS

              
                // THIS IS THE RANGE SLIDER LOGIC DO NOT CHANGE !!
var ZBRangeSlider = function(id) { 
  var self = this;
  var startX = 0, x = 0;

  // retrieve touch button
  var slider     = document.getElementById(id)
  var touchLeft  = slider.querySelector('.slider-touch-left');
  var touchRight = slider.querySelector('.slider-touch-right');
  var lineSpan   = slider.querySelector('.slider-line span');
  
  // get some properties
  var min   = parseFloat(slider.getAttribute('se-min'));
  var max   = parseFloat(slider.getAttribute('se-max'));
  
  // retrieve default values
  var defaultMinValue = min;
  if(slider.hasAttribute('se-min-value'))
  {
    defaultMinValue = parseFloat(slider.getAttribute('se-min-value'));  
  }
  var defaultMaxValue = max;
  
  if(slider.hasAttribute('se-max-value'))
  {
    defaultMaxValue = parseFloat(slider.getAttribute('se-max-value'));  
  }
  
  // check values are correct
  if(defaultMinValue < min)
  {
    defaultMinValue = min;
  }
  
  if(defaultMaxValue > max)
  {
    defaultMaxValue = max;
  }
  
  if(defaultMinValue > defaultMaxValue)
  {
    defaultMinValue = defaultMaxValue;
  }
  
  var step  = 0.0;
  
  if (slider.getAttribute('se-step'))
  {
    step  = Math.abs(parseFloat(slider.getAttribute('se-step')));
  }
  
  // normalize flag
  var normalizeFact = 26;
  
  self.slider = slider;
  self.reset = function() {
    touchLeft.style.left = '0px';
    touchRight.style.left = (slider.offsetWidth - touchLeft.offsetWidth) + 'px';
    lineSpan.style.marginLeft = '0px';
    lineSpan.style.width = (slider.offsetWidth - touchLeft.offsetWidth) + 'px';
    startX = 0;
    x = 0;
  };
  
  self.setMinValue = function(minValue)
  {
    var ratio = ((minValue - min) / (max - min));
    touchLeft.style.left = Math.ceil(ratio * (slider.offsetWidth - (touchLeft.offsetWidth + normalizeFact))) + 'px';
    lineSpan.style.marginLeft = touchLeft.offsetLeft + 'px';
    lineSpan.style.width = (touchRight.offsetLeft - touchLeft.offsetLeft) + 'px';
    slider.setAttribute('se-min-value', minValue);
  }
  
  self.setMaxValue = function(maxValue)
  {
    var ratio = ((maxValue - min) / (max - min));
    touchRight.style.left = Math.ceil(ratio * (slider.offsetWidth - (touchLeft.offsetWidth + normalizeFact)) + normalizeFact) + 'px';
    lineSpan.style.marginLeft = touchLeft.offsetLeft + 'px';
    lineSpan.style.width = (touchRight.offsetLeft - touchLeft.offsetLeft) + 'px';
    slider.setAttribute('se-max-value', maxValue);
  }
  
  // initial reset
  self.reset();
  
  // usefull values, min, max, normalize fact is the width of both touch buttons
  var maxX = slider.offsetWidth - touchRight.offsetWidth;
  var selectedTouch = null;
  var initialValue = (lineSpan.offsetWidth - normalizeFact);

  // set defualt values
  self.setMinValue(defaultMinValue);
  self.setMaxValue(defaultMaxValue);
  
  // setup touch/click events
  function onStart(event) {
    
    // Prevent default dragging of selected content
    event.preventDefault();
    var eventTouch = event;

    if (event.touches)
    {
      eventTouch = event.touches[0];
    }
    
    if(this === touchLeft)
    {
      x = touchLeft.offsetLeft;
    }
    else
    {
      x = touchRight.offsetLeft;
    }

    startX = eventTouch.pageX - x;
    selectedTouch = this;
    document.addEventListener('mousemove', onMove);
    document.addEventListener('mouseup', onStop);
    document.addEventListener('touchmove', onMove);
    document.addEventListener('touchend', onStop);
    

  }
  
  function onMove(event) {
    var eventTouch = event;

    if (event.touches)
    {
      eventTouch = event.touches[0];
    }

    x = eventTouch.pageX - startX;
    
    if (selectedTouch === touchLeft)
    {
      if(x > (touchRight.offsetLeft - selectedTouch.offsetWidth + 10))
      {
        x = (touchRight.offsetLeft - selectedTouch.offsetWidth + 10)
      }
      else if(x < 0)
      {
        x = 0;
      }
      
      selectedTouch.style.left = x + 'px';
    }
    else if (selectedTouch === touchRight)
    {
      if(x < (touchLeft.offsetLeft + touchLeft.offsetWidth - 10))
      {
        x = (touchLeft.offsetLeft + touchLeft.offsetWidth - 10)
      }
      else if(x > maxX)
      {
        x = maxX;
      }
      selectedTouch.style.left = x + 'px';
    }
    
    // update line span
    lineSpan.style.marginLeft = touchLeft.offsetLeft + 'px';
    lineSpan.style.width = (touchRight.offsetLeft - touchLeft.offsetLeft) + 'px';
    
    // write new value
    calculateValue();
    
    // call on change
    if(slider.getAttribute('on-change'))
    {
      var fn = new Function('min, max', slider.getAttribute('on-change'));
      fn(slider.getAttribute('se-min-value'), slider.getAttribute('se-max-value'));
    }
    
    if(self.onChange)
    {
      self.onChange(slider.getAttribute('se-min-value'), slider.getAttribute('se-max-value'));
    }
    
  }
  
  function onStop(event) {
    document.removeEventListener('mousemove', onMove);
    document.removeEventListener('mouseup', onStop);
    document.removeEventListener('touchmove', onMove);
    document.removeEventListener('touchend', onStop);
    
    selectedTouch = null;

    // write new value
    calculateValue();
    
    // call did changed
    if(slider.getAttribute('did-changed'))
    {
      var fn = new Function('min, max', slider.getAttribute('did-changed'));
      fn(slider.getAttribute('se-min-value'), slider.getAttribute('se-max-value'));
    }
    
    if(self.didChanged)
    {
      self.didChanged(slider.getAttribute('se-min-value'), slider.getAttribute('se-max-value'));
    }
  }
  
  function calculateValue() {
    var newValue = (lineSpan.offsetWidth - normalizeFact) / initialValue;
    var minValue = lineSpan.offsetLeft / initialValue;
    var maxValue = minValue + newValue;

    var minValue = minValue * (max - min) + min;
    var maxValue = maxValue * (max - min) + min;
    
    console.log(step);
    if (step !== 0.0)
    {
      var multi = Math.floor((minValue / step));
      minValue = step * multi;
      
      multi = Math.floor((maxValue / step));
      maxValue = step * multi;
    }
    
    slider.setAttribute('se-min-value', minValue);
    slider.setAttribute('se-max-value', maxValue);
  }
  
  // link events
  touchLeft.addEventListener('mousedown', onStart);
  touchRight.addEventListener('mousedown', onStart);
  touchLeft.addEventListener('touchstart', onStart);
  touchRight.addEventListener('touchstart', onStart);
};

// -------------------
// How to use? 
var newRangeSlider = new ZBRangeSlider('my-slider');

newRangeSlider.onChange = function(min, max)
{
  console.log(min, max, this);
  document.getElementById('result').innerHTML = 'Min: ' + min + ' Max: ' + max;
}

newRangeSlider.didChanged = function(min, max)
{
  console.log(min,max, this);
  document.getElementById('result').innerHTML = 'Min: ' + min + ' Max: ' + max;
}

// call reset if needed
// newRangeSlider.reset();
              
            
!
999px

Console