ph-utils 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/dom.d.ts +9 -0
- package/lib/dom.js +43 -18
- package/package.json +1 -1
package/lib/dom.d.ts
CHANGED
|
@@ -90,3 +90,12 @@ export declare function queryHideNodeSize(hideNode: string | HTMLElement, parent
|
|
|
90
90
|
width: number;
|
|
91
91
|
height: number;
|
|
92
92
|
};
|
|
93
|
+
/**
|
|
94
|
+
* 判断元素是否在父元素的可视区域内。
|
|
95
|
+
*
|
|
96
|
+
* @param el 要检查的元素
|
|
97
|
+
* @param parent 元素的父元素,默认为document.body
|
|
98
|
+
* @param direction 检查的方向,默认为"horizontal"
|
|
99
|
+
* @returns 如果元素在父元素的可视区域内,则返回true;否则返回false。
|
|
100
|
+
*/
|
|
101
|
+
export declare function isVisible(el: HTMLElement, parent?: HTMLElement, direction?: string): boolean;
|
package/lib/dom.js
CHANGED
|
@@ -3,13 +3,13 @@
|
|
|
3
3
|
* 现今不推荐在使用这种方式,现在开发前端的时候,推荐使用一些成熟的框架例如:React、Preact、Vue、Angular、Svelte、Ember、Knockout等
|
|
4
4
|
* 在使用这些框架的时候额外的一些不可避免的 dom 操作时才使用这个工具;如果确实需要使用原生开发推荐使用 jquery 或者想要更精简的话可以使用 https://github.com/finom/bala 封装
|
|
5
5
|
*/
|
|
6
|
-
const vendorPrefix = [
|
|
6
|
+
const vendorPrefix = ["", "-webkit", "-moz-"];
|
|
7
7
|
/**
|
|
8
8
|
* 根据选择器获取节点
|
|
9
9
|
* @param {string} selector 选择器
|
|
10
10
|
*/
|
|
11
11
|
export function elem(selector, dom) {
|
|
12
|
-
if (typeof selector ===
|
|
12
|
+
if (typeof selector === "string") {
|
|
13
13
|
return (dom || document).querySelectorAll(selector);
|
|
14
14
|
}
|
|
15
15
|
else {
|
|
@@ -48,7 +48,7 @@ export function hasClass(elem, clazz) {
|
|
|
48
48
|
*/
|
|
49
49
|
export function transition(element, value) {
|
|
50
50
|
vendorPrefix.forEach((prefix) => {
|
|
51
|
-
let t = prefix ===
|
|
51
|
+
let t = prefix === "" ? "transition" : prefix + "Transition";
|
|
52
52
|
element.style[t] = value;
|
|
53
53
|
});
|
|
54
54
|
}
|
|
@@ -59,7 +59,7 @@ export function transition(element, value) {
|
|
|
59
59
|
*/
|
|
60
60
|
export function transform(element, value) {
|
|
61
61
|
vendorPrefix.forEach((prefix) => {
|
|
62
|
-
let t = prefix ===
|
|
62
|
+
let t = prefix === "" ? "transform" : prefix + "Transform";
|
|
63
63
|
element.style[t] = value;
|
|
64
64
|
});
|
|
65
65
|
}
|
|
@@ -72,24 +72,24 @@ export function transform(element, value) {
|
|
|
72
72
|
*/
|
|
73
73
|
export function on(element, listener, fn, once = false) {
|
|
74
74
|
let eventExtra = { eventStop: false };
|
|
75
|
-
if (typeof once ===
|
|
75
|
+
if (typeof once === "boolean") {
|
|
76
76
|
eventExtra.once = once;
|
|
77
77
|
}
|
|
78
78
|
else {
|
|
79
79
|
eventExtra = once;
|
|
80
80
|
}
|
|
81
81
|
if (eventExtra.eventFlag != null) {
|
|
82
|
-
element.setAttribute(eventExtra.eventFlag,
|
|
82
|
+
element.setAttribute(eventExtra.eventFlag, "__stop__");
|
|
83
83
|
element.addEventListener(listener, (e) => {
|
|
84
84
|
let target = e.target;
|
|
85
|
-
let flag =
|
|
85
|
+
let flag = "";
|
|
86
86
|
do {
|
|
87
|
-
flag = target.getAttribute(eventExtra.eventFlag) ||
|
|
88
|
-
if (flag ===
|
|
87
|
+
flag = target.getAttribute(eventExtra.eventFlag) || "";
|
|
88
|
+
if (flag === "") {
|
|
89
89
|
target = target.parentNode;
|
|
90
90
|
}
|
|
91
|
-
} while (flag ===
|
|
92
|
-
if (flag !==
|
|
91
|
+
} while (flag === "");
|
|
92
|
+
if (flag !== "__stop__" || eventExtra.eventStop) {
|
|
93
93
|
fn(e, target, flag);
|
|
94
94
|
}
|
|
95
95
|
}, eventExtra);
|
|
@@ -150,10 +150,10 @@ export function iterate(elems, fn) {
|
|
|
150
150
|
*/
|
|
151
151
|
export function attr(elem, key, value) {
|
|
152
152
|
if (value != null) {
|
|
153
|
-
elem.setAttribute(
|
|
153
|
+
elem.setAttribute("data-" + key, value);
|
|
154
154
|
}
|
|
155
155
|
else {
|
|
156
|
-
return elem.getAttribute(
|
|
156
|
+
return elem.getAttribute("data-" + key);
|
|
157
157
|
}
|
|
158
158
|
}
|
|
159
159
|
/**
|
|
@@ -172,11 +172,11 @@ export function parent(el) {
|
|
|
172
172
|
*/
|
|
173
173
|
export function queryHideNodeSize(hideNode, parent = document.body) {
|
|
174
174
|
// 计算折叠菜单的高度
|
|
175
|
-
let $tmp = document.createElement(
|
|
176
|
-
$tmp.style.cssText =
|
|
177
|
-
let $tmpInner = document.createElement(
|
|
178
|
-
$tmpInner.style.cssText =
|
|
179
|
-
if (typeof hideNode ===
|
|
175
|
+
let $tmp = document.createElement("div");
|
|
176
|
+
$tmp.style.cssText = "position:fixed;left:-1000px;top:-1000px;opacity:0;";
|
|
177
|
+
let $tmpInner = document.createElement("div");
|
|
178
|
+
$tmpInner.style.cssText = "position:relative;";
|
|
179
|
+
if (typeof hideNode === "string") {
|
|
180
180
|
$tmpInner.innerHTML = hideNode;
|
|
181
181
|
}
|
|
182
182
|
else {
|
|
@@ -188,3 +188,28 @@ export function queryHideNodeSize(hideNode, parent = document.body) {
|
|
|
188
188
|
parent.removeChild($tmp);
|
|
189
189
|
return { width: rect.width, height: rect.height };
|
|
190
190
|
}
|
|
191
|
+
/**
|
|
192
|
+
* 判断元素是否在父元素的可视区域内。
|
|
193
|
+
*
|
|
194
|
+
* @param el 要检查的元素
|
|
195
|
+
* @param parent 元素的父元素,默认为document.body
|
|
196
|
+
* @param direction 检查的方向,默认为"horizontal"
|
|
197
|
+
* @returns 如果元素在父元素的可视区域内,则返回true;否则返回false。
|
|
198
|
+
*/
|
|
199
|
+
export function isVisible(el, parent = document.body, direction = "horizontal") {
|
|
200
|
+
// 获取父元素的边界信息
|
|
201
|
+
const containerRect = parent.getBoundingClientRect();
|
|
202
|
+
// 获取元素的边界信息
|
|
203
|
+
const elementRect = el.getBoundingClientRect();
|
|
204
|
+
// 根据检查方向,确定元素的起始和结束位置
|
|
205
|
+
// 元素的上、下边界
|
|
206
|
+
let elStart = direction === "horizontal" ? elementRect.left : elementRect.top;
|
|
207
|
+
let elEnd = direction === "horizontal" ? elementRect.right : elementRect.bottom;
|
|
208
|
+
// 根据检查方向,确定父元素的起始和结束位置
|
|
209
|
+
// 容器的可视区域的上、下边界
|
|
210
|
+
let containerStart = direction === "horizontal" ? containerRect.left : containerRect.top;
|
|
211
|
+
let containerEnd = direction === "horizontal" ? containerRect.right : containerRect.bottom;
|
|
212
|
+
// 判断元素是否在父元素的可视区域内
|
|
213
|
+
// 判断元素是否完全在容器的可视区域内
|
|
214
|
+
return elStart >= containerStart && elEnd <= containerEnd;
|
|
215
|
+
}
|