SHOW:
|
|
- or go back to the newest paste.
1 | function E(id) {return document.getElementById(id)} | |
2 | - | |
2 | + | var swStep = 10, swInt = 10; |
3 | function Menu(name, swipe) { | |
4 | // --- Class to make a swipy menu. The menu elements must be in the following format: | |
5 | - | // <div class="wrap" id="wrap_M"><div class="menu" id="menu_M">[content]</div></div> |
5 | + | // <div class="wrap"><div class="menu" id="menu_M">[content]</div></div> |
6 | // where M is the menu name | |
7 | // swipe should be either "left" to swipe to the left or "down" to swipe downwards | |
8 | // CSS needed: | |
9 | // div.wrap {position: absolute; overflow: hidden; visibility: hidden;} | |
10 | // div.wrap div.menu {position: relative; visibility: visible } | |
11 | ||
12 | this.name = name; | |
13 | this.el = E('menu_' + name); | |
14 | - | this.wrap = E('wrap_' + name); |
14 | + | |
15 | - | this.closeButton = this.el.getElementsByClassName('close')[0]; |
15 | + | |
16 | - | this.init = function() { |
16 | + | |
17 | this.swipe = swipe | |
18 | - | this.closeButton.onclick = function() {t.close()} |
18 | + | |
19 | - | this.getSize(); |
19 | + | |
20 | if (this.ti != undefined) { | |
21 | clearInterval(this.ti); | |
22 | } | |
23 | this.ti = setInterval(function() { | |
24 | if (opening) t.stage += swStep; | |
25 | - | this.getSize = function() { |
25 | + | |
26 | - | // --- Briefly display the menu in order to measure its dimensions |
26 | + | |
27 | - | // (this needs to be rerun at the start and whenever the user resizes) |
27 | + | |
28 | - | var disp = this.el.style.display; |
28 | + | |
29 | - | if (disp != 'block') this.el.style.display = 'block'; |
29 | + | |
30 | - | this.width = this.el.offsetWidth; |
30 | + | |
31 | - | this.height = this.el.offsetHeight; |
31 | + | |
32 | - | if (disp != 'block') this.el.style.display = disp; |
32 | + | |
33 | } | |
34 | - | this.init(); |
34 | + | |
35 | if (this.swipe == 'left') { | |
36 | this.el.style.left = 100 - this.stage + '%'; | |
37 | } | |
38 | else if (this.swipe == 'down') { | |
39 | this.el.style.bottom = (100 - this.stage) * this.el.offsetHeight / 100 + 'px'; | |
40 | // (because % positioning doesn't work vertically) | |
41 | - | if (opening) t.stage += swStep; |
41 | + | |
42 | setOpacity(this.el, this.maxOp * this.stage / 100); | |
43 | } | |
44 | this.el.style.display = 'block'; | |
45 | this.draw(); | |
46 | this.close = function () { | |
47 | this.opened = false; | |
48 | this.animate('close'); | |
49 | document.body.onclick = null; | |
50 | this.el.onclick = null; | |
51 | } | |
52 | this.open = function() { | |
53 | var t = this; | |
54 | this.opened = true; | |
55 | this.animate('open'); | |
56 | document.body.onclick = function() {t.close()} | |
57 | this.el.onclick = function(ev) {stopProp(ev)} | |
58 | } | |
59 | } | |
60 | // e.g. | |
61 | var myMenu = new Menu('options', 'left'); | |
62 | E('openMenuButton').onclick = myMenu.open(); |