Created
July 31, 2019 02:45
-
-
Save vNNi/c2db9a83f9a85d9195e7e407f03c1418 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="filter__price-values"> | |
<span>R$1</span> | |
<span>R$2</span> | |
</div> | |
<div class="content"> | |
<div id='id66' class="range"> | |
<div id='id66b' class='range__between'></div> | |
<button id='id661' class="range__button_1"></button> | |
<button id='id662' class="range__button_2"></button> | |
<input id='id66i1' class='range_inpt1' type='number' min='20' max='700'> | |
- <input id='id66i2' class='range_inpt2' type='number' min='20' max='700'> | |
</div> | |
</div> | |
<style> | |
.content { | |
position: relative; | |
display: flex; | |
align-items: center; | |
justify-content: center; | |
margin-top: 20px; | |
} | |
.range { | |
width: 100%; | |
height: 2px; | |
border-radius: 4px; | |
background-color: #ddd; | |
} | |
.range__button_1, | |
.range__button_2 { | |
position: absolute; | |
top: 50%; | |
width: 20px; | |
height: 20px; | |
cursor: move; | |
transform: translateY(-50%); | |
border: none; | |
border-radius: 50%; | |
background-color: white; | |
border: 1.5px solid black; | |
} | |
.range__between { | |
position: absolute; | |
height: 2px; | |
width: 0px; | |
border: none; | |
background-color: black; | |
} | |
.range_inpt1, | |
.range_inpt2 { | |
margin-top: 20px; | |
width: 50px; | |
} | |
.filter__price-values { | |
display: flex; | |
justify-content: space-between; | |
width: 100%; | |
} | |
</style> | |
<script> | |
setTimeout(init2slider('id66', 'id66b', 'id661', 'id662', 'id66i1', 'id66i2'), 0); | |
function init2slider(idX, btwX, btn1X, btn2X, input1, input2) { | |
var slider = document.getElementById(idX); | |
var between = document.getElementById(btwX); | |
var button1 = document.getElementById(btn1X); | |
var button2 = document.getElementById(btn2X); | |
var inpt1 = document.getElementById(input1); | |
var inpt2 = document.getElementById(input2); | |
var min = inpt1.min; | |
var max = inpt1.max; | |
/*init*/ | |
var sliderCoords = getCoords(slider); | |
button1.style.marginLeft = '0px'; | |
button2.style.marginLeft = (slider.offsetWidth - button1.offsetWidth) + 'px'; | |
between.style.width = (slider.offsetWidth - button1.offsetWidth) + 'px'; | |
inpt1.value = min; | |
inpt2.value = max; | |
/*mouse*/ | |
button1.onmousedown = function(evt) { | |
var sliderCoords = getCoords(slider); | |
var betweenCoords = getCoords(between); | |
var buttonCoords1 = getCoords(button1); | |
var buttonCoords2 = getCoords(button2); | |
var shiftX2 = evt.pageX - buttonCoords2.left; | |
var shiftX1 = evt.pageX - buttonCoords1.left; | |
document.onmousemove = function(evt) { | |
var left1 = evt.pageX - shiftX1 - sliderCoords.left; | |
var 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; | |
var left2 = evt.pageX - shiftX2 - sliderCoords.left; | |
var right2 = slider.offsetWidth - button2.offsetWidth; | |
if (left2 < 0) left2 = 0; | |
if (left2 > right2) left2 = right2; | |
var per_min = 0; | |
var per_max = 0; | |
if (left1 > left2) { | |
between.style.width = (left1 - left2) + '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) + '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 = function() { | |
document.onmousemove = document.onmouseup = null; | |
}; | |
return false; | |
}; | |
button2.onmousedown = function(evt) { | |
var sliderCoords = getCoords(slider); | |
var betweenCoords = getCoords(between); | |
var buttonCoords1 = getCoords(button1); | |
var buttonCoords2 = getCoords(button2); | |
var shiftX2 = evt.pageX - buttonCoords2.left; | |
var shiftX1 = evt.pageX - buttonCoords1.left; | |
document.onmousemove = function(evt) { | |
var left2 = evt.pageX - shiftX2 - sliderCoords.left; | |
var 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; | |
var left1 = evt.pageX - shiftX1 - sliderCoords.left; | |
var right1 = slider.offsetWidth - button1.offsetWidth; | |
if (left1 < 0) left1 = 0; | |
if (left1 > right1) left1 = right1; | |
var per_min = 0; | |
var per_max = 0; | |
if (left1 > left2) { | |
between.style.width = (left1 - left2) + '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) + '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 = function() { | |
document.onmousemove = document.onmouseup = null; | |
}; | |
return false; | |
}; | |
button1.ondragstart = function() { | |
return false; | |
}; | |
button2.ondragstart = function() { | |
return false; | |
}; | |
function getCoords(elem) { | |
var box = elem.getBoundingClientRect(); | |
return { | |
top: box.top + pageYOffset, | |
left: box.left + pageXOffset | |
}; | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment