-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbar.dom.js
198 lines (176 loc) · 6.55 KB
/
rbar.dom.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
rbar.dom.js // ralf-bartsch.net // 2018-07-09
license and info: https://github.com/r84r/js-lib-rbar
Vereinfachung von DOM-Zugriffen
Achtung: Es wird keine Überprüfung übergebener Parameter druchgeführt
-> z.B. führen nicht vorhandene Elemente zu einem Fehler
*/
var rbar = (function(ns) {
ns.dom = (function(ns) {
/**********************************************************************
Effekte
**********************************************************************/
/*
Element verstecken oder zeigen
*/
ns.hide = function(el) {
el.style.display = 'none';
}
ns.show = function(el) {
el.style.display = '';
}
/**********************************************************************
CSS
**********************************************************************/
/*
Klasse prüfen, hinzufügen oder entfernen
-> nur bis IE9 nötig, ab IE10 wird classList unterstützt
*/
ns.hasClass = function(el, className) {
if (el.classList)
el.classList.contains(className)
else
new RegExp('(^| )' + className + '( |$)', 'gi').test(el.className)
}
ns.addClass = function(el, className) {
if (el.classList)
el.classList.add(className)
else
el.className += ' ' + className
}
ns.removeClass = function(el, className) {
if (el === undefined) return;
if (el.classList)
el.classList.remove(className)
else
el.className = el.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'), ' ');
}
/*
Klasse umschalten
*/
ns.toggleClass = function(el, className) {
if (el.classList)
el.classList.toggle(className)
else {
var classes = el.className.split(' ');
var existingIndex = classes.indexOf(className);
if (existingIndex >= 0)
classes.splice(existingIndex, 1)
else
classes.push(className)
el.className = classes.join(' ');
}
}
/*
CSS auf alle Elemente eines Selektors applizieren.
Bsp: selectors='div.home, a.start', styles={float:'left',color:'blue'}
*/
ns.setStyles = function(selectors, styles) {
var el;
if ((typeof selectors) == 'string')
el = document.querySelectorAll(selectors)
else
el = selectors;
for(i=0; i<el.length; i++)
for (var s in styles)
el[i].style[s] = styles[s];
}
/**********************************************************************
Elemente
**********************************************************************/
/*
Position auf Elternelement bezogen
*/
ns.position = function(el) {
return {left: el.offsetLeft,
top: el.offsetTop};
}
/*
Position auf komplette Seite bezogen
*/
ns.offset = function(el) {
var rect = el.getBoundingClientRect();
return {top: rect.top + document.body.scrollTop,
left: rect.left + document.body.scrollLeft};
}
/*
Position auf angezeigten Ausschnitt bezogen
*/
ns.viewportPosition = function(el) {
return el.getBoundingClientRect();
}
/*
Höhe des Anzeigefensters (ohne Scrollleiste)
*/
ns.viewportHeight = function() {
return document.documentElement.clientHeight;
}
/*
Weite des Anzeigefensters (ohne Scrollleiste)
*/
ns.viewportWidth = function() {
return document.documentElement.clientWidth;
}
/*
Seitenverhältnis des Anzeigefesters (ohne Scrollleiste)
*/
ns.viewportAspectRatio = function() {
return (document.documentElement.clientWidth / document.documentElement.clientHeight);
}
ns.outerHeight = function(el, withMargin) {
if (arguments.length === 2 && withMargin === true) {
var height = el.offsetHeight;
var style = getComputedStyle(el);
height += parseInt(style.marginTop) + parseInt(style.marginBottom);
} else {
var height = el.offsetHeight;
}
return height;
}
ns.outerWidth = function(el, withMargin) {
if (arguments.length === 2 && withMargin === true) {
var width = el.offsetWidth;
var style = getComputedStyle(el);
width += parseInt(style.marginLeft) + parseInt(style.marginRight);
} else {
var width = el.offsetWidth;
}
return width;
}
/*
komplettes Element ersetzen (umschließenden Tag mit Inhalt)
*/
ns.replaceWith = function(el, string) {
el.outerHTML = string;
}
/*
Inhalt eines Elements ersetzen (ohne umschließenden Tag)
*/
ns.html = function(el, string) {
el.innerHTML = string;
}
/*
Attribut-Wert eines HTML-Tags erhalten. Gibt zwei Werte zurück
-> empty: 'true' wenn nicht vorhanden oder leer
-> value: String des Attributes ('' falls empty==true)
*/
ns.getAttr = function(el, attr) {
if (el.hasAttribute(attr)) {
var r = el.getAttribute(attr);
return {empty: (r === '' ? true : false), value: r};
}
return {empty: true, value: ''};
}
/*
Sucht das nächste Elternelement
*/
ns.closest = function(el, selector) {
if (!el || !el.parentElement) return null
else if (el.matches(selector)) return el
else if (el.parentElement.matches(selector)) return el.parentElement
else return ns.closest(el.parentElement, selector)
}
return ns;
})({});
return ns;
})(rbar || {})