I want to make a slider with jquery. but when I pulled it out the frame "silderContainer", "sliders" will stop moving and when it returned through the "sliderContainer", "slider" will move again. What I want is how "slider" still moving when I pulled the mouse out of the frame.
Here the code:
<style>
body{
padding:60px;
height:800px;
}
.sliderContainer{
//position:relative;
width:200px;
background-color:#CCC;
height:30px;
margin-top:16px;
cursor:pointer;
}
.slider{
position:absolute;
width:50px;
height:30px;
background:#fa565a;
float:right;
}
</style>
</head>
<body>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(document).on('ready', function(){
function moveSlider(e){
e.preventDefault();
var pos = $(e.currentTarget).offset()
, posX = e.pageX - pos.left;
if(posX >= 0 && posX <= $(e.currentTarget).outerWidth()){
$('.slider').css('width', posX+'px');
}
}
$('.sliderContainer').on('mousedown', function(e){
moveSlider(e);
$(this).on('mousemove', function(e){
moveSlider(e);
});
}).on('mouseup', function(){
$(this).off('mousemove');
});
});
</script>
<div class='sliderContainer'>
<div class='slider'></div>
</div>
</body>
Any help would be greatly appreciated. Thank you.