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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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="main">
  <h4 class="price-slider_title">Стоимость</h4>

  <!-- ТЕКСТОВЫЕ МЕТКИ -->
  <div class="price-slider__text">
    <p class="price-slider__text_from">от</p>
    <p class="price-slider__text_to">до</p>
  </div>

  <!-- СЛАЙДЕР С ПОЛЗУНКАМИ -->
  <div class="price-slider">
    <div class='price-slider__range-between'></div>
    <div class="price-slider_first"></div>
    <div class="price-slider_second"></div>
  </div>

  <!-- ВЫВОД ЗНАЧЕНИЙ ПОЛЗУНКОВ -->
  <div class="price-slider__values">
    <input class="price-slider__value_min" type="text" min="0" max="100" readonly>
    <input class="price-slider__value_max" type="text" min="0" max="100" readonly>
  </div>
</div>
              
            
!

CSS

              
                * {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  background: #212830;
  color: #4c5566;
}

/* общий блок */
.main {
  width: 360px;
  background: #262d37;
  border: 1px solid #C4C4C4;
  border-radius: 2px;
  margin: 30px auto;
  padding: 10px 20px;
  color: #fff;
}

/* заголовок блока */
.price-slider_title {
  font-size: 30px;
  line-height: 35px;
  color: #fff;
  margin-bottom: 15px;
}

/* выравнивание и отступ для текста и вывода значений */
.price-slider__text, 
.price-slider__values {
  display: flex;
  justify-content: space-between;
  margin-bottom: 10px;
}

/* слова "от" и "до" */
.price-slider__text_from, 
.price-slider__text_to {
  font-size: 18px;
  line-height: 21px;
}

/* основной диапазон ползунков */
.price-slider {
  position: relative;
  height: 19px; /* должна быть 19 */
  border-radius: 8px;
  margin-bottom: 10px;
/*   background: #C4C4C4; */
  border: 1px solid #C4C4C4;
}

/* активный диапазон ползунков */
.price-slider__range-between {
  position: absolute;
  height: 18px;
  border-radius: 8px;
/*   top: 1.1px; */
  background: #FFA800;
}

/* ползунки */
.price-slider_first,
.price-slider_second {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 24px;
  height: 24px;
  cursor: pointer;
  border-radius: 50%;
  background: #fff;
}

.price-slider_first {
  left: -2px;
}

/* вывод значений ползунков */
.price-slider__value_min, 
.price-slider__value_max {
  width: 35px;
  background: inherit;
  color: #fff;
  border: none;
}

.price-slider__value_max {
  text-align: right;
}
              
            
!

JS

              
                setTimeout(initSliders('.price-slider', '.price-slider__range-between', '.price-slider_first', '.price-slider_second', '.price-slider__value_min', '.price-slider__value_max'), 0);

function initSliders(idX, btwX, btn1X, btn2X, input1, input2) {
  const slider = document.querySelector(idX); /* пассивный диапазон */
  const between = document.querySelector(btwX); /* активный диапазон */
  const button1 = document.querySelector(btn1X); /* первый ползунок */
  const button2 = document.querySelector(btn2X); /* второй ползунок */
  const inpt1 = document.querySelector(input1); /* вывод значения первого ползунка */
  const inpt2 = document.querySelector(input2);	/* вывод значения второго ползунка */
  const min = inpt1.min;
  const max = inpt1.max;
    
  /* инициализация */
  const sliderCoords = getCoords(slider);
  button1.style.marginLeft = '0px';
  button2.style.marginLeft = (slider.offsetWidth - button1.offsetWidth) + 'px';
  between.style.width = slider.offsetWidth + 'px'; /* ширина активного трека */
  inpt1.value = min; /* отображение начального минимального значения */
  inpt2.value = max; /* отображение начального максимального значения */
  
  /* первый вывод */
  inpt1.onchange = (evt) => {
    if (parseInt(inpt1.value) < min) {
      inpt1.value = min;
    }

    if (parseInt(inpt1.value) > max) {
      inpt1.value = max;
    }
    
    /* переопределение вывода значений: первый стал вторым и наоборот */
    if (parseInt(inpt1.value) > parseInt(inpt2.value)) {
      const temp = inpt1.value;
      inpt1.value = inpt2.value;
      inpt2.value = temp;
    }

    // const sliderCoords = getCoords(slider);
    // const per1 = parseInt(inpt1.value - min) * 100 / (max - min);
    // const per2 = parseInt(inpt2.value - min) * 100 / (max - min);
    const left1 = parseInt(inpt1.value) * (slider.offsetWidth - button1.offsetWidth) / 100;
    const left2 = parseInt(inpt2.value) * (slider.offsetWidth - button1.offsetWidth) / 100;

    // button1.style.marginLeft = left1 + 'px'; 
    // button2.style.marginLeft = left2 + 'px';
    
    /* вычисление диапазона активного трека (оранжевого цвета) */
    calcActiveTrack(left1, left2, between);
  }
  
  /* второй вывод */
  inpt2.onchange = (evt) => {
    if (parseInt(inpt2.value) < min) {
      inpt2.value = min;
    }

    if (parseInt(inpt2.value) > max) {
      inpt2.value = max;
    }
    
    /* переопределение вывода значений: первый стал вторым и наоборот */
    if (parseInt(inpt1.value) > parseInt(inpt2.value)) {
      const temp = inpt1.value;
      inpt1.value = inpt2.value;
      inpt2.value = temp;
    }

    // const sliderCoords = getCoords(slider);
    // const per1 = parseInt(inpt1.value - min) * 100 / (max - min);
    // const per2 = parseInt(inpt2.value - min) * 100 / (max - min);
    const left1 = parseInt(inpt1.value) * (slider.offsetWidth - button1.offsetWidth) / 100;
    const left2 = parseInt(inpt2.value) * (slider.offsetWidth - button1.offsetWidth) / 100;

    // button1.style.marginLeft = left1 + 'px'; 
    // button2.style.marginLeft = left2 + 'px';
    
    /* вычисление диапазона активного трека (оранжевого цвета) */
    calcActiveTrack(left1, left2, between);
  }
  
  /* События мыши */
  button1.onmousedown = (evt) => {       
    const sliderCoords = getCoords(slider);
    const betweenCoords = getCoords(between); 
    const buttonCoords1 = getCoords(button1);
    const buttonCoords2 = getCoords(button2);
    let shiftX1 = evt.pageX - buttonCoords1.left;
    let shiftX2 = evt.pageX - buttonCoords2.left; 

    document.onmousemove = (evt) => {
      let left1 = evt.pageX - shiftX1 - sliderCoords.left;
      let right1 = slider.offsetWidth - button1.offsetWidth;

      if (left1 < 0) {
        left1 = 0;
      }

      if (left1 > right1) {
        left1 = right1;
      }

      button1.style.marginLeft = left1 + 'px';  
      shiftX2 = evt.pageX - buttonCoords2.left;

      let left2 = evt.pageX - shiftX2 - sliderCoords.left;
      let right2 = slider.offsetWidth - button2.offsetWidth;

      if (left2 < 0) {
        left2 = 0;
      }

      if (left2 > right2) {
        left2 = right2;
      }

      let per_min = 0;
      let per_max = 0;

      if (left1 > left2) {
        between.style.width = (left1 - left2 + 22) + 'px';
        between.style.marginLeft = left2 + 'px';
        per_min = left2 * 100 / (slider.offsetWidth - button1.offsetWidth);
        per_max = left1 * 100 / (slider.offsetWidth - button1.offsetWidth);
      } else {
        between.style.width = (left2 - left1 + 22) + 'px';
        between.style.marginLeft = left1 + 'px';                
        per_min = left1 * 100 / (slider.offsetWidth - button1.offsetWidth);
        per_max = left2 * 100 / (slider.offsetWidth - button1.offsetWidth);
      }

      inpt1.value = (parseInt(min) + Math.round((max - min) * per_min / 100));
      inpt2.value = (parseInt(min) + Math.round((max - min) * per_max / 100));
    };
    
    document.onmouseup = () => document.onmousemove = document.onmouseup = null;

    return false;
  };
  
  button2.onmousedown = (evt) => {       
    const sliderCoords = getCoords(slider);
    const betweenCoords = getCoords(between); 
    const buttonCoords1 = getCoords(button1);
    const buttonCoords2 = getCoords(button2);
    let shiftX1 = evt.pageX - buttonCoords1.left;
    let shiftX2 = evt.pageX - buttonCoords2.left;  
    
    document.onmousemove = (evt) => {
      let left2 = evt.pageX - shiftX2 - sliderCoords.left;
      let right2 = slider.offsetWidth - button2.offsetWidth;

      if (left2 < 0) {
        left2 = 0;
      }

      if (left2 > right2) {
        left2 = right2;
      }

      button2.style.marginLeft = left2 + 'px';                      
      shiftX1 = evt.pageX - buttonCoords1.left; 

      let left1 = evt.pageX - shiftX1 - sliderCoords.left;
      let right1 = slider.offsetWidth - button1.offsetWidth;  

      if (left1 < 0) {
        left1 = 0;
      }

      if (left1 > right1) {
        left1 = right1;
      }                      

      let per_min = 0;
      let per_max = 0;

      if (left1 > left2) {
        between.style.width = (left1 - left2 + 22) + 'px';
        between.style.marginLeft = left2 + 'px';
        per_min = left2 * 100 / (slider.offsetWidth - button1.offsetWidth);
        per_max = left1 * 100 / (slider.offsetWidth - button1.offsetWidth);
      } else {
        between.style.width = (left2 - left1 + 22) + 'px';
        between.style.marginLeft = left1 + 'px';
        per_min = left1 * 100 / (slider.offsetWidth - button1.offsetWidth);
        per_max = left2 * 100 / (slider.offsetWidth - button1.offsetWidth);
      }

      inpt1.value = (parseInt(min) + Math.round((max - min) * per_min / 100));
      inpt2.value = (parseInt(min) + Math.round((max - min) * per_max / 100));
    };

    document.onmouseup = () => document.onmousemove = document.onmouseup = null;

    return false;
  };
  
  /* отключение Drag’n’Drop браузера, чтобы не было конфликта */
  button1.ondragstart = () => {
    return false;
  };
  
  button2.ondragstart = () => {
    return false;
  };
}

/* Получение координат элемента */
function getCoords(elem) {
  const box = elem.getBoundingClientRect();
  
  return {
    top: box.top + pageYOffset,
    left: box.left + pageXOffset,
  };
}

/* вычисление диапазона активного трека (оранжевого цвета) */
function calcActiveTrack(value1, value2, track) {
  if (value1 > value2) {
    track.style.width = (value1 - value2) + 'px';
    track.style.marginLeft = value2 + 'px';
  } else {
    track.style.width = (value2 - value1) + 'px';
    track.style.marginLeft = value1 + 'px';  
  }
}
              
            
!
999px

Console