Created
March 26, 2015 14:53
-
-
Save devxleo/91ed56b241848007de50 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
<!DOCTYPE html> | |
<html> | |
<head lang="en"> | |
<meta charset="UTF-8"> | |
<title>Demo</title> | |
<style> | |
.bar { | |
-webkit-user-select: none; | |
-moz-user-select: none; | |
-ms-user-select: none; | |
user-select: none; | |
display: inline-block; | |
width: 400px; | |
height: 20px; | |
background-color: azure; | |
} | |
.slider { | |
-webkit-user-select: none; | |
-moz-user-select: none; | |
-ms-user-select: none; | |
user-select: none; | |
position: absolute; | |
left: 0; | |
display: inline-block; | |
width: 20px; | |
height: 40px; | |
background-color: black; | |
cursor: move; | |
} | |
.container { | |
-webkit-user-select: none; | |
-moz-user-select: none; | |
-ms-user-select: none; | |
user-select: none; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="container" style="position: relative"> | |
<span class="bar"></span> | |
<span class="slider"></span> | |
</div> | |
<script src="jquery-1.11.2.js"></script> | |
<script> | |
var sliderX = 0; | |
var hold = false; | |
var requestAnimationFrame = window.requestAnimationFrame || | |
window.mozRequestAnimationFrame || | |
window.webkitRequestAnimationFrame || | |
window.msRequestAnimationFrame; | |
var repaint = function () { | |
$('.slider').css('left', sliderX + 'px'); | |
if (hold) { | |
requestAnimationFrame(repaint); | |
} | |
}; | |
$(document).on('mouseup', function () { | |
hold = false; | |
$(document).off('mousemove'); | |
}); | |
$('.slider').on('mousedown', function (e) { | |
hold = true; | |
var startX = e.screenX; | |
$(document).on('mousemove', function (e) { | |
sliderX = sliderX + e.screenX - startX; | |
startX = e.screenX; | |
}); | |
requestAnimationFrame(repaint); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment