Last active
July 15, 2024 14:39
-
-
Save unruthless/a590889aeb2255eab561 to your computer and use it in GitHub Desktop.
Simple scrubber UI component
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> | |
<meta charset="utf-8"> | |
<title>Scrubber.js</title> | |
<style type="text/css"> | |
.container { | |
background: #333; | |
height: 100%; | |
left: 0; | |
margin: 0; | |
padding: 0; | |
position: absolute; | |
top: 0; | |
width: 100%; | |
} | |
.line { | |
background: #aaa; | |
box-shadow: 0 3px 10px 1px #222; | |
height: 10px; | |
position: relative; | |
top: 100px; | |
width: 100%; | |
} | |
.scrubber { | |
background: #bbb; | |
border-radius: 5px; | |
box-shadow: 0 3px 10px 1px #222; | |
height: 40px; | |
left: 50%; | |
position: absolute; | |
top: -15px; /* -0.5 x (.line height - .toggle height) */ | |
width: 10px; | |
-webkit-transform: translateZ(0); | |
-webkit-transition: -webkit-transform 0.3s, background 0.4s; | |
} | |
.scrubber.draggable { | |
-webkit-transform: scale(1.3,1.3); | |
background: #fff; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="container"> | |
<div class="line"> | |
<div class="scrubber"></div> | |
</div> | |
</div> | |
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script> | |
<script> | |
(function($) { | |
$.fn.drags = function(opt) { | |
opt = $.extend({ cursor: 'move' }, opt); | |
var $el = this, | |
klass = 'draggable', | |
startDrag = function(ev) { | |
var $drag = $el.addClass(klass), | |
drg_h = $drag.outerHeight(), | |
drg_w = $drag.outerWidth(), | |
pos_x = $drag.offset().left + drg_w - ev.pageX; | |
$drag | |
.parents() | |
.on('mousemove', function(ev) { | |
$('.' + klass) | |
.offset({ left: ev.pageX + pos_x - drg_w }) | |
.on('mouseup', stopDrag); | |
}) | |
.on('mouseleave', function() { | |
$(window).one('mouseup', stopDrag); | |
}) | |
.on('mouseenter', function() { | |
$(window).off('mouseup'); | |
}); | |
ev.preventDefault(); | |
}, | |
stopDrag = function() { | |
$el.removeClass('draggable'); | |
}, | |
init = function() { | |
$el | |
.css('cursor', opt.cursor) | |
.on('mousedown',startDrag) | |
.on('mouseup', stopDrag); | |
}; | |
return init(); | |
}; | |
})(jQuery); | |
$(function(){ | |
var $scrubber = $('.scrubber'); | |
$scrubber.drags(); | |
}); | |
</script> | |
</body> | |
</html> |
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
.container { | |
background: #333; | |
height: 100%; | |
left: 0; | |
margin: 0; | |
padding: 0; | |
position: absolute; | |
top: 0; | |
width: 100%; | |
} | |
.line { | |
background: #aaa; | |
box-shadow: 0 3px 10px 1px #222; | |
height: 10px; | |
position: relative; | |
top: 100px; | |
width: 100%; | |
} | |
.scrubber { | |
background: #bbb; | |
border-radius: 5px; | |
box-shadow: 0 3px 10px 1px #222; | |
height: 40px; | |
left: 50%; | |
position: absolute; | |
top: -15px; /* -0.5 x (.line height - .toggle height) */ | |
width: 10px; | |
-webkit-transform: translateZ(0); | |
-webkit-transition: -webkit-transform 0.3s, background 0.4s; | |
} | |
.scrubber.draggable { | |
-webkit-transform: scale(1.3,1.3); | |
background: #fff; | |
} |
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
/** | |
* Simple scrubber UI component. | |
* | |
* @author Ruthie BenDor <[email protected]> | |
* @forked from http://css-tricks.com/snippets/jquery/draggable-without-jquery-ui/ | |
* @changes | |
* -- Unline original, only x-direction movement permitted, since intended for a video or timeline scrubber. | |
* -- Correctly unbinds dragging in situation where user begins dragging, moves the mouse off the timeline, and then releases the mouse. | |
* -- Refactored for DRYer and easier reading. | |
*/ | |
(function($) { | |
$.fn.drags = function(opt) { | |
opt = $.extend({ cursor: 'move' }, opt); | |
var $el = this, | |
klass = 'draggable', | |
startDrag = function(ev) { | |
var $drag = $el.addClass(klass), | |
drg_h = $drag.outerHeight(), | |
drg_w = $drag.outerWidth(), | |
pos_x = $drag.offset().left + drg_w - ev.pageX; | |
$drag | |
.parents() | |
.on('mousemove', function(ev) { | |
$('.' + klass) | |
.offset({ left: ev.pageX + pos_x - drg_w }) | |
.on('mouseup', stopDrag); | |
}) | |
.on('mouseleave', function() { | |
$(window).one('mouseup', stopDrag); | |
}) | |
.on('mouseenter', function() { | |
$(window).off('mouseup'); | |
}); | |
ev.preventDefault(); | |
}, | |
stopDrag = function() { | |
$el.removeClass('draggable'); | |
}, | |
init = function() { | |
$el | |
.css('cursor', opt.cursor) | |
.on('mousedown',startDrag) | |
.on('mouseup', stopDrag); | |
}; | |
return init(); | |
}; | |
})(jQuery); | |
$(function(){ | |
var $scrubber = $('.scrubber'); | |
$scrubber.drags(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment