Skip to content

Instantly share code, notes, and snippets.

@cornellsteven
Created June 22, 2012 14:20
Show Gist options
  • Save cornellsteven/2973013 to your computer and use it in GitHub Desktop.
Save cornellsteven/2973013 to your computer and use it in GitHub Desktop.
A web page created at CodePen.io
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Light Metal Volume Dial &middot; CodePen</title>
<style>
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
body {
background-color: #f1f1f1;
background-image: linear-gradient(top, #f7f7f7, #e8e8e8);
background-repeat: repeat-x;
position: relative;
overflow: hidden;
}
#dial-holder {
width: 150px;
height: 150px;
position: absolute;
top: 50%;
left: 50%;
margin-top: -75px;
margin-left: -75px;
}
#dial-holder .key {
width: 250px;
height: 50px;
position: absolute;
left: 50%;
margin-left: -125px;
bottom: -50px;
}
#dial-holder .key div {
position: absolute;
top: 0;
display: inline-block;
color: #909090;
text-shadow: 0 2px 0 white;
font-size: 12px;
line-height: 12px;
font-family: "Gotham";
font-weight: 500;
}
#dial-holder .key div span {
display: inline-block;
position: absolute;
top: -16px;
color: #c2c2c2;
font-size: 17px;
font-weight: 500;
}
#dial-holder .key .min {
left: 0;
}
#dial-holder .key .min span {
right: -19px;
transform: rotate(20deg);
}
#dial-holder .key .max {
right: 0;
}
#dial-holder .key .max span {
left: -15px;
transform: rotate(-20deg);
}
#dial-holder .outer-circle {
width: 150px;
height: 150px;
position: relative;
border-radius: 150px;
box-shadow: inset 0 2px 1px 0px rgba(0, 0, 0, 0.2), 0 0 2px 0 #ffffff;
background-color: #f3f3f3;
background-image: linear-gradient(top, #ebebeb, #ffffff);
background-repeat: repeat-x;
}
#dial-holder .dial {
width: 130px;
height: 130px;
position: relative;
top: 10px;
left: 10px;
border-radius: 130px;
box-shadow: 0 0 2px 1px rgba(0, 0, 0, 0.1), 0 5px 3px 1px rgba(0, 0, 0, 0.1), 0 3px 3px 0px rgba(0, 0, 0, 0.2);
background: linear-gradient(bottom, #f4f4f4, #ffffff 40%);
}
#dial-holder .dial .dial-inner-holder {
width: 130px;
height: 130px;
border-radius: 130px;
transform: rotate(0deg);
}
#dial-holder .dial .dial-inner {
width: 118px;
height: 118px;
position: relative;
top: 6px;
left: 6px;
border-radius: 118px;
background: linear-gradient(top, #f4f4f4, #ffffff 40%);
}
#dial-holder .dial .handle {
width: 22px;
height: 22px;
margin: 0 auto;
position: relative;
top: 12px;
border-radius: 22px;
box-shadow: inset 0 2px 2px 0px rgba(0, 0, 0, 0.2), 0 2px 1px #ffffff;
background-color: #ededed;
background-image: linear-gradient(top, #ececec, #eeeeee);
background-repeat: repeat-x;
cursor: pointer;
}
</style>
<style>
#codepen-footer, #codepen-footer * {
-webkit-box-sizing: border-box !important;
-moz-box-sizing: border-box !important;
box-sizing: border-box !important;
}
#codepen-footer {
display: block !important;
position: fixed !important;
bottom: 0 !important;
left: 0 !important;
width: 100% !important;
padding: 0 10px !important;
margin: 0 !important;
height: 30px !important;
line-height: 30px !important;
font-size: 12px !important;
color: #eeeeee !important;
background-color: #505050 !important;
text-align: left !important;
background: -webkit-linear-gradient(top, #505050, #383838) !important;
background: -moz-linear-gradient(top, #505050, #383838) !important;
background: -ms-linear-gradient(top, #505050, #383838) !important;
background: -o-linear-gradient(top, #505050, #383838) !important;
border-top: 1px solid black !important;
border-bottom: 1px solid black !important;
border-radius: 0 !important;
box-shadow: inset 0 1px 0 #6e6e6e, 0 2px 2px rgba(0, 0, 0, 0.4) !important;
z-index: 300 !important;
font-family: "Lucida Grande", "Lucida Sans Unicode", Tahoma, sans-serif !important;
letter-spacing: 0 !important;
word-spacing: 0 !important;
}
#codepen-footer a {
color: #a7a7a7 !important;
text-decoration: none !important;
}
#codepen-footer a:hover {
color: white !important;
}
</style>
<script>
// Kill alerts, confirmations and prompts
window.alert = function(){};
window.confirm = function(){};
window.prompt = function(){};
window.open = function(){};
window.print = function(){};
</script>
<script src="http://codepen.io/javascripts/libs/prefixfree.min.js"></script>
</head>
<body>
<div id="dial-holder">
<div class="outer-circle">
<div class="dial">
<div class="dial-inner-holder">
<div class="dial-inner">
<div class="handle" draggable=true></div>
</div>
</div>
</div>
</div>
<div class="key">
<div class="min">MIN<span>/</span></div>
<div class="max">MAX<span>\</span></div>
</div>
</div>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://codepen.io/javascripts/libs/modernizr.js"></script>
<script>
(function() {
var handleX, handleY, $dial, $handle, $display, d, c, r, cx, cy;
var pi = Math.PI;
var max = 130;
var min = -130;
var deg2rad = function(deg) {
return deg * (pi / 180);
}
var rad2deg = function(rad) {
return rad * (180 / pi);
}
var dragStart = function(e) {
$('body').css('cursor', 'pointer');
window.addEventListener('mousemove', mouseMove);
window.addEventListener('mouseup', dragStop);
e.preventDefault();
}
var mouseMove = function(e) {
var x = e.clientX;
var y = e.clientY;
var distance = Math.sqrt( Math.pow((cx - x), 2) + Math.pow((cy - y), 2) );
if (distance <= r) {
rx = x - cx;
ry = y - cy;
var Angle = rad2deg(Math.atan2(ry, rx));
if (Angle < 0) {
Angle = 180 + (Angle + 180);
}
rotate(Angle);
lastAngle = Angle;
}
}
var dragStop = function(e) {
$('body').css('cursor', 'default');
window.removeEventListener('mouseup', dragStop);
window.removeEventListener('mousemove', mouseMove);
};
var rotate = function(angle) {
var newAngle = angle + 90;
if (newAngle > 359) {
newAngle -= 360;
};
if (newAngle > 180) {
newAngle = (360 - newAngle) - ((360 - newAngle) * 2);
};
if (newAngle > max || newAngle < min) {
return;
}
$dial.css('-webkit-transform', 'rotate(' + newAngle + 'deg)');
}
$(function() {
$dialHolder = $('#dial-holder');
$dial = $('.dial-inner-holder');
$handle = $('.handle');
d = $dial.outerWidth();
c = pi * d;
r = d / 2;
cx = $dial.offset().left + r;
cy = $dial.offset().top + r;
$('.handle').on('dragstart', dragStart);
});
})();
</script>
<div id="codepen-footer">
<a style="color: #f73535 !important;" href="https://codepen.wufoo.com/forms/m7x3r3/def/field14=" onclick="window.open(this.href, null, 'height=517, width=680, toolbar=0, location=0, status=1, scrollbars=1, resizable=1'); return false">Report Abuse</a>
&nbsp;
<a href="/cornellsteven/pen/light-metal-volume-dial/4">Edit this Pen</a>
</div>
</body>
</html>
var handleX, handleY, $dial, $handle, $display, d, c, r, cx, cy;
var pi = Math.PI;
var max = 130;
var min = -130;
var deg2rad = function(deg) {
return deg * (pi / 180);
}
var rad2deg = function(rad) {
return rad * (180 / pi);
}
var dragStart = function(e) {
$('body').css('cursor', 'pointer');
window.addEventListener('mousemove', mouseMove);
window.addEventListener('mouseup', dragStop);
e.preventDefault();
}
var mouseMove = function(e) {
var x = e.clientX;
var y = e.clientY;
var distance = Math.sqrt( Math.pow((cx - x), 2) + Math.pow((cy - y), 2) );
if (distance <= r) {
rx = x - cx;
ry = y - cy;
var Angle = rad2deg(Math.atan2(ry, rx));
if (Angle < 0) {
Angle = 180 + (Angle + 180);
}
rotate(Angle);
lastAngle = Angle;
}
}
var dragStop = function(e) {
$('body').css('cursor', 'default');
window.removeEventListener('mouseup', dragStop);
window.removeEventListener('mousemove', mouseMove);
};
var rotate = function(angle) {
var newAngle = angle + 90;
if (newAngle > 359) {
newAngle -= 360;
};
if (newAngle > 180) {
newAngle = (360 - newAngle) - ((360 - newAngle) * 2);
};
if (newAngle > max || newAngle < min) {
return;
}
$dial.css('-webkit-transform', 'rotate(' + newAngle + 'deg)');
}
$(function() {
$dialHolder = $('#dial-holder');
$dial = $('.dial-inner-holder');
$handle = $('.handle');
d = $dial.outerWidth();
c = pi * d;
r = d / 2;
cx = $dial.offset().left + r;
cy = $dial.offset().top + r;
$('.handle').on('dragstart', dragStart);
});
<div id="dial-holder">
<div class="outer-circle">
<div class="dial">
<div class="dial-inner-holder">
<div class="dial-inner">
<div class="handle" draggable=true></div>
</div>
</div>
</div>
</div>
<div class="key">
<div class="min">MIN<span>/</span></div>
<div class="max">MAX<span>\</span></div>
</div>
</div>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
body {
background-color: #f1f1f1;
background-image: linear-gradient(top, #f7f7f7, #e8e8e8);
background-repeat: repeat-x;
position: relative;
overflow: hidden;
}
#dial-holder {
width: 150px;
height: 150px;
position: absolute;
top: 50%;
left: 50%;
margin-top: -75px;
margin-left: -75px;
}
#dial-holder .key {
width: 250px;
height: 50px;
position: absolute;
left: 50%;
margin-left: -125px;
bottom: -50px;
}
#dial-holder .key div {
position: absolute;
top: 0;
display: inline-block;
color: #909090;
text-shadow: 0 2px 0 white;
font-size: 12px;
line-height: 12px;
font-family: "Gotham";
font-weight: 500;
}
#dial-holder .key div span {
display: inline-block;
position: absolute;
top: -16px;
color: #c2c2c2;
font-size: 17px;
font-weight: 500;
}
#dial-holder .key .min {
left: 0;
}
#dial-holder .key .min span {
right: -19px;
transform: rotate(20deg);
}
#dial-holder .key .max {
right: 0;
}
#dial-holder .key .max span {
left: -15px;
transform: rotate(-20deg);
}
#dial-holder .outer-circle {
width: 150px;
height: 150px;
position: relative;
border-radius: 150px;
box-shadow: inset 0 2px 1px 0px rgba(0, 0, 0, 0.2), 0 0 2px 0 #ffffff;
background-color: #f3f3f3;
background-image: linear-gradient(top, #ebebeb, #ffffff);
background-repeat: repeat-x;
}
#dial-holder .dial {
width: 130px;
height: 130px;
position: relative;
top: 10px;
left: 10px;
border-radius: 130px;
box-shadow: 0 0 2px 1px rgba(0, 0, 0, 0.1), 0 5px 3px 1px rgba(0, 0, 0, 0.1), 0 3px 3px 0px rgba(0, 0, 0, 0.2);
background: linear-gradient(bottom, #f4f4f4, #ffffff 40%);
}
#dial-holder .dial .dial-inner-holder {
width: 130px;
height: 130px;
border-radius: 130px;
transform: rotate(0deg);
}
#dial-holder .dial .dial-inner {
width: 118px;
height: 118px;
position: relative;
top: 6px;
left: 6px;
border-radius: 118px;
background: linear-gradient(top, #f4f4f4, #ffffff 40%);
}
#dial-holder .dial .handle {
width: 22px;
height: 22px;
margin: 0 auto;
position: relative;
top: 12px;
border-radius: 22px;
box-shadow: inset 0 2px 2px 0px rgba(0, 0, 0, 0.2), 0 2px 1px #ffffff;
background-color: #ededed;
background-image: linear-gradient(top, #ececec, #eeeeee);
background-repeat: repeat-x;
cursor: pointer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment