-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
64 lines (45 loc) · 1.3 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
function q(selector){
var o = this != window ? this : document;
return o.querySelector(selector);
};
Element.prototype.q = q;
(function(){
var baseInput = q('#base-size'),
ptrInput = q('#ptr'),
prtOutput = q('.px-to-rem .output'),
rtpInput = q('#rtp'),
rtpOutput =q('.rem-to-px .output');
function getValue(input){
return parseFloat(input.value.replace(/,/g, '.')) || 0;
};
function base(){
return getValue(baseInput) || 16;
};
//Converter functions
function ptr(px){
var x = base() || 16,
rem = (1/x * px) + 'rem';
return rem;
};
function rtp(rem){
var x = base() || 16,
px = rem / (1/x) + 'px';
return px;
};
function handleInputs(){
var px = getValue(ptrInput),
rem = getValue(rtpInput);
prtOutput.value = ptr(px);
rtpOutput.value = rtp(rem);
};
(function init(){
var inputs = [baseInput, ptrInput, rtpInput],
events = ['keydown', 'keyup', 'change'];
// Attach events
inputs.map(function(input){
events.map(function(event){
input.addEventListener(event, handleInputs);
});
});
}());
}());