Created
August 1, 2011 19:45
-
-
Save kosso/1118840 to your computer and use it in GitHub Desktop.
A very simple basic 'slider' bar in JavaScript and CSS
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<style type="text/css"> | |
#bar{ | |
width:200px; | |
height:25px; | |
border:1px solid black; | |
position:relative; | |
} | |
#slider{ | |
width:0%; | |
height:100%; | |
background-color:red; | |
top:0px; | |
left:0px; | |
position:absolute; | |
cursor:pointer; | |
} | |
#info{ | |
width:200px; | |
height:25px; | |
border:1px solid black; | |
} | |
</style> | |
<script type="text/javascript"> | |
var bar, slider; | |
function init(){ | |
bar = document.getElementById('bar'); | |
slider = document.getElementById('slider'); | |
info = document.getElementById('info'); | |
bar.addEventListener('mousedown', startSlide, false); | |
bar.addEventListener('mouseup', stopSlide, false); | |
} | |
function startSlide(event){ | |
var set_perc = ((((event.clientX - bar.offsetLeft) / bar.offsetWidth)).toFixed(2)); | |
info.innerHTML = 'start' + set_perc + '%'; | |
bar.addEventListener('mousemove', moveSlide, false); | |
slider.style.width = (set_perc * 100) + '%'; | |
} | |
function moveSlide(event){ | |
var set_perc = ((((event.clientX - bar.offsetLeft) / bar.offsetWidth)).toFixed(2)); | |
info.innerHTML = 'moving : ' + set_perc + '%'; | |
slider.style.width = (set_perc * 100) + '%'; | |
} | |
function stopSlide(event){ | |
var set_perc = ((((event.clientX - bar.offsetLeft) / bar.offsetWidth)).toFixed(2)); | |
info.innerHTML = 'done : ' + set_perc + '%'; | |
bar.removeEventListener('mousemove', moveSlide, false); | |
slider.style.width = (set_perc * 100) + '%'; | |
} | |
</script> | |
</head> | |
<body onload='init()'> | |
<div id='bar'> | |
<div id='slider'> | |
</div> | |
</div> | |
<br /> | |
<div id='info'>info</div> | |
</body> | |
</html> |
Please take a look at my upgraded version of your code! :) https://gist.github.com/dzenkovich/5813630
Hi there, Thanks for posting this, but I cannot get the code to work on jsFiddle.
I am totally new to JScript and am just trying to start from a working example. Can you please take a look at http://jsfiddle.net/6PdVV/ and tell me what I'm doing wrong?
Thanks.
@DRudel: A little late but just incase anybody else finds this. http://jsfiddle.net/6PdVV/39/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice and simple.
i used bar.getBoundingRect() to allow it to work in a relatively positioned div.
<title>Slider</title> <style type="text/css"> #wrapper { width:500px; position:absolute; margin:auto; height:500px; background:#666; top:100px; left:200px; } #bar { width:200px; height:25px; border:1px solid black; position:relative; top:10px; left:10px; } #slider { width:0%; height:100%; background-color:red; top:0px; left:0px; position:absolute; cursor:pointer; } #info { width:200px; height:25px; border:1px solid black; } </style> <script type="text/javascript"> var bar, slider; function init() { bar = document.getElementById('bar'); barW = parseInt(bar.style.width = '200px', 10); slider = document.getElementById('slider'); info = document.getElementById('info'); bar.addEventListener('mousedown', startSlide, false); bar.addEventListener('mouseup', stopSlide, false); } function startSlide(event) { var pct = parseInt((((event.clientX - bar.getBoundingClientRect().left)/barW)*100), 10).toFixed(2); console.log(pct); info.innerHTML = 'start' + pct + '%'; bar.addEventListener('mousemove', moveSlide, false); slider.style.width = parseInt(event.clientX - bar.getBoundingClientRect().left, 10) +'px'; } function moveSlide(event) {//console.log('move: '); console.log(event); var pct = parseInt((((event.clientX - bar.getBoundingClientRect().left)/barW)*100), 10).toFixed(2); info.innerHTML = 'moving : ' + pct + '%'; slider.style.width = parseInt(event.clientX - bar.getBoundingClientRect().left, 10) +'px'; } function stopSlide(event) { var pct = parseInt((((event.clientX - bar.getBoundingClientRect().left)/barW)*100), 10).toFixed(2); info.innerHTML = 'done : ' + pct + '%'; bar.removeEventListener('mousemove', moveSlide, false); slider.style.width = parseInt(event.clientX - bar.getBoundingClientRect().left, 10) +'px'; } window.onload = function(){ init(); } </script>