@bestime/utils_browser 1.0.3 → 1.0.4
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.
|
@@ -138,7 +138,11 @@ declare function removeCookie(key: string): void;
|
|
|
138
138
|
* @param to - 替换为
|
|
139
139
|
* @returns
|
|
140
140
|
*/
|
|
141
|
-
declare function
|
|
141
|
+
declare function replaceClass(
|
|
142
|
+
el: HTMLElement,
|
|
143
|
+
from: string | string[],
|
|
144
|
+
to: string | string[]
|
|
145
|
+
): void;
|
|
142
146
|
|
|
143
147
|
/**
|
|
144
148
|
* 给dom元素添加className
|
|
@@ -151,11 +155,19 @@ declare function addClass(el: HTMLElement, name: string | string[]): void;
|
|
|
151
155
|
/**
|
|
152
156
|
* 给dom元素移除className
|
|
153
157
|
* @param el - DOM
|
|
154
|
-
* @param name -
|
|
158
|
+
* @param name - 需要移除className
|
|
155
159
|
* @returns
|
|
156
160
|
*/
|
|
157
161
|
declare function removeClass(el: HTMLElement, name: string | string[]): void;
|
|
158
162
|
|
|
163
|
+
/**
|
|
164
|
+
* 给dom元素添加className
|
|
165
|
+
* @param el - DOM
|
|
166
|
+
* @param name - 将要被切换的className
|
|
167
|
+
* @returns
|
|
168
|
+
*/
|
|
169
|
+
declare function toggleClass(el: HTMLElement, name: string | string[]): void;
|
|
170
|
+
|
|
159
171
|
export {
|
|
160
172
|
addClass,
|
|
161
173
|
downloadFileByArrayBuffer,
|
|
@@ -172,6 +184,7 @@ export {
|
|
|
172
184
|
removeCookie,
|
|
173
185
|
removeElement,
|
|
174
186
|
removeStorage,
|
|
187
|
+
replaceClass,
|
|
175
188
|
setCookie,
|
|
176
189
|
setStorage,
|
|
177
190
|
toggleClass
|
|
@@ -3,6 +3,6 @@
|
|
|
3
3
|
* @QQ 1174295440
|
|
4
4
|
* @author Bestime
|
|
5
5
|
* @see https://github.com/bestime/tool
|
|
6
|
-
* @update 2023-11-08 22:
|
|
6
|
+
* @update 2023-11-08 22:23:42
|
|
7
7
|
*/
|
|
8
|
-
import{defaultValue,isString,variableHasValue,isArray,forEach}from"@bestime/utils_base";function getWindowSize(){return{width:document.documentElement.clientWidth||document.body.clientWidth||window.innerWidth||0,height:document.documentElement.clientHeight||document.body.clientHeight||window.innerHeight||0}}var timer,htivId=0,idName="",isStart=!1,records={};function stop(){isStart=!1,clearInterval(timer),timer=undefined}function startRun(){clearInterval(timer),isStart=!0,timer=setInterval(function(){for(var e in records){var t=records[e];t.current=+new Date,t.current-t.start>=t.interval&&(t.start=t.current,records[e].handler())}},17)}function add(e,t){return idName="HI-"+ ++htivId,records[idName]={start:+new Date,current:0,handler:e,interval:t},isStart||startRun(),idName}function remove(e){delete records[e],0===Object.keys(records).length&&stop()}var hpInterval={add:add,remove:remove};function observeDomResize(n,o,r,e){var i=[0,0,!1],l=[0,0,!1],a=[0,0,!1],
|
|
8
|
+
import{defaultValue,isString,variableHasValue,isArray,forEach}from"@bestime/utils_base";function getWindowSize(){return{width:document.documentElement.clientWidth||document.body.clientWidth||window.innerWidth||0,height:document.documentElement.clientHeight||document.body.clientHeight||window.innerHeight||0}}var timer,htivId=0,idName="",isStart=!1,records={};function stop(){isStart=!1,clearInterval(timer),timer=undefined}function startRun(){clearInterval(timer),isStart=!0,timer=setInterval(function(){for(var e in records){var t=records[e];t.current=+new Date,t.current-t.start>=t.interval&&(t.start=t.current,records[e].handler())}},17)}function add(e,t){return idName="HI-"+ ++htivId,records[idName]={start:+new Date,current:0,handler:e,interval:t},isStart||startRun(),idName}function remove(e){delete records[e],0===Object.keys(records).length&&stop()}var hpInterval={add:add,remove:remove};function observeDomResize(n,o,r,e){var i=[0,0,!1],l=[0,0,!1],a=[0,0,!1],c=[0,0,!1],t=hpInterval.add(d,e=e||500);function d(){var e,t;document.body.contains(n)?(e=!1,null!=r&&r.includes("position")&&(t=n.getBoundingClientRect(),a[0]=t.left,a[2]=a[0]!==a[1],c[0]=t.top,c[2]=c[0]!==c[1]),i[0]=n.offsetWidth,i[2]=i[0]!==i[1],l[0]=n.offsetHeight,l[2]=l[0]!==l[1],i[2]&&(i[1]=i[0],null!=r&&r.includes("width")&&(e=!0)),l[2]&&(l[1]=l[0],null!=r&&r.includes("height")&&(e=!0)),(c[2]||a[2])&&(c[1]=c[0],a[1]=a[0],null!=r&&r.includes("position")&&(e=!0)),r&&0!==r.length||(i[2]||l[2]||c[2]||a[2])&&(e=!0),e&&o(n)):s()}function s(){hpInterval.remove(t),i=undefined,l=undefined}return d(),s}function downloadFileByUrl(e,t){var n=document.createElement("a");n.style.display="none",n.download=t,n.setAttribute("href",e),n.setAttribute("target","_blank"),n.setAttribute("download",t),document.body.appendChild(n),n.click(),n.remove()}var $undefinedValue=void 0,$decodeURIComponent=decodeURIComponent,$browserGlobal=window,$headElement=document.getElementsByTagName("head")[0];function downloadFileByArrayBuffer(e,t){var n=$browserGlobal.URL,e=n.createObjectURL(new Blob([e]));downloadFileByUrl(e,t),n.revokeObjectURL(e),undefined}function removeElement(e){e.parentNode&&e.parentNode.removeChild(e)}function prevent(e,t,n){e=e||$browserGlobal.event,n=!1!==n,(t=!1!==t)&&$browserGlobal.event?$browserGlobal.event.cancelBubble=!0:e.stopPropagation(),n&&$browserGlobal.event?$browserGlobal.event.returnValue=!1:e.preventDefault()}function getStorage(e){e=localStorage.getItem(e);return defaultValue(e,"")}function removeStorage(e){localStorage.removeItem(e)}function hpSetStringValue(e){return e=isString(e)?e:JSON.stringify(e)}function setStorage(e,t){localStorage.setItem(e,hpSetStringValue(t))}function getRelativePos(e){var t=e.getBoundingClientRect();return{x:t.left,y:t.top,height:e.offsetHeight,width:e.offsetWidth,clientWidth:e.clientWidth,clientHeight:e.clientHeight}}function getJsFileBaseUrl(e){e=e||0;for(var t="/[^/]*",n=document.scripts,o=0;o<e;o++)t+=t;return n[n.length-1].src.replace(new RegExp(t+"$"),"")}function hpCreateFileLoaderElement(e,t,n,o){if($headElement){var r;if("js"===e?(r=document.createElement("script")).src=t:((r=document.createElement("link")).setAttribute("rel","stylesheet"),r.href=t),o)for(var i in o)r.setAttribute(i,o[i]);r.onload=r.onerror=n,$headElement.appendChild(r)}}var cache={};function loadMultiple(n,o){for(var r=[],i=0,e=0;e<n.length;e++)!function(t){loadSingle(n[t],function(e){r[t]=e,++i===n.length&&o.apply($undefinedValue,r)})}(e)}function loadSingle(e,t){var n=e.dependencies&&e.dependencies.length,o=e["with"]&&e["with"].length,r=(cache[e.url]||(cache[e.url]={count:0,dependencies:!n,"with":!o,complete:!1,create:!1,url:e.url}),cache[e.url]);function i(){r.complete&&r.dependencies&&r["with"]&&(e.module?t($browserGlobal[e.module]):t())}r.count++,!r.dependencies&&n?(r.dependencies=!0,loadMultiple(e.dependencies,function(){loadSingle(e,t)})):!r["with"]&&o?(r["with"]=!0,loadMultiple(e["with"].concat(e),i)):r.create?variableHasValue(function(){return r.complete},i,300):(r.create=!0,hpCreateFileLoaderElement(e.type,e.url,function(){r.complete=!0,i()},e.attribute))}function libraryFile(e,t){console.log("加载器",e,cache),(e instanceof Array?loadMultiple:loadSingle)(e,t)}function setObjectToString(e){return e=isString(e)?e:JSON.stringify(e)}function setCookie(e,t,n){t=setObjectToString(t);e=e+"="+encodeURI(t)+";path=/;";"number"==typeof n&&((t=new Date).setTime(t.getTime()+n),e+="expires="+t.toUTCString()),document.cookie=e}function getCookie(e,t){t=t||document.cookie;var o="";return t.replace(new RegExp("(^|;\\s)"+e+"=(.*?)($|(;\\s))"),function(e,t,n){o=$decodeURIComponent(n)}),o}function removeCookie(e){setCookie(e,"",-1)}function removeClass(t,e){isArray(e)?forEach(e,function(e){t.classList.remove(e)}):t.classList.remove(e)}function addClass(t,e){isArray(e)?forEach(e,function(e){t.classList.add(e)}):t.classList.add(e)}function replaceClass(e,t,n){removeClass(e,t),addClass(e,n)}function toggleClass(t,e){isArray(e)?forEach(e,function(e){t.classList.toggle(e)}):t.classList.toggle(e)}export{addClass,downloadFileByArrayBuffer,downloadFileByUrl,getCookie,getJsFileBaseUrl,getRelativePos,getStorage,getWindowSize,libraryFile,observeDomResize,prevent,removeClass,removeCookie,removeElement,removeStorage,replaceClass,setCookie,setStorage,toggleClass};
|
|
@@ -138,7 +138,11 @@ declare function removeCookie(key: string): void;
|
|
|
138
138
|
* @param to - 替换为
|
|
139
139
|
* @returns
|
|
140
140
|
*/
|
|
141
|
-
declare function
|
|
141
|
+
declare function replaceClass(
|
|
142
|
+
el: HTMLElement,
|
|
143
|
+
from: string | string[],
|
|
144
|
+
to: string | string[]
|
|
145
|
+
): void;
|
|
142
146
|
|
|
143
147
|
/**
|
|
144
148
|
* 给dom元素添加className
|
|
@@ -151,11 +155,19 @@ declare function addClass(el: HTMLElement, name: string | string[]): void;
|
|
|
151
155
|
/**
|
|
152
156
|
* 给dom元素移除className
|
|
153
157
|
* @param el - DOM
|
|
154
|
-
* @param name -
|
|
158
|
+
* @param name - 需要移除className
|
|
155
159
|
* @returns
|
|
156
160
|
*/
|
|
157
161
|
declare function removeClass(el: HTMLElement, name: string | string[]): void;
|
|
158
162
|
|
|
163
|
+
/**
|
|
164
|
+
* 给dom元素添加className
|
|
165
|
+
* @param el - DOM
|
|
166
|
+
* @param name - 将要被切换的className
|
|
167
|
+
* @returns
|
|
168
|
+
*/
|
|
169
|
+
declare function toggleClass(el: HTMLElement, name: string | string[]): void;
|
|
170
|
+
|
|
159
171
|
declare global {
|
|
160
172
|
/**
|
|
161
173
|
* 该声明文件用于全局声明(不用npm安装时拷贝到项目中直接使用)
|
|
@@ -177,6 +189,7 @@ declare global {
|
|
|
177
189
|
removeCookie,
|
|
178
190
|
removeElement,
|
|
179
191
|
removeStorage,
|
|
192
|
+
replaceClass,
|
|
180
193
|
setCookie,
|
|
181
194
|
setStorage,
|
|
182
195
|
toggleClass
|
|
@@ -3,6 +3,6 @@
|
|
|
3
3
|
* @QQ 1174295440
|
|
4
4
|
* @author Bestime
|
|
5
5
|
* @see https://github.com/bestime/tool
|
|
6
|
-
* @update 2023-11-08 22:
|
|
6
|
+
* @update 2023-11-08 22:23:42
|
|
7
7
|
*/
|
|
8
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@bestime/utils_base")):"function"==typeof define&&define.amd?define(["exports","@bestime/utils_base"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).jUtilsBrowser={},e.jUtilsBase)}(this,function(e,
|
|
8
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@bestime/utils_base")):"function"==typeof define&&define.amd?define(["exports","@bestime/utils_base"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).jUtilsBrowser={},e.jUtilsBase)}(this,function(e,s){"use strict";var n,i,o=0,r=!1,c={};var d={add:function(e,t){return n="HI-"+ ++o,c[n]={start:+new Date,current:0,handler:e,interval:t},r||(clearInterval(i),r=!0,i=setInterval(function(){for(var e in c){var t=c[e];t.current=+new Date,t.current-t.start>=t.interval&&(t.start=t.current,c[e].handler())}},17)),n},remove:function(e){delete c[e],0===Object.keys(c).length&&(r=!1,clearInterval(i),i=undefined)}};function l(e,t){var n=document.createElement("a");n.style.display="none",n.download=t,n.setAttribute("href",e),n.setAttribute("target","_blank"),n.setAttribute("download",t),document.body.appendChild(n),n.click(),n.remove()}var a=void 0,u=decodeURIComponent,f=window,g=document.getElementsByTagName("head")[0];var h={};function m(n,i){for(var o=[],r=0,e=0;e<n.length;e++)!function(t){p(n[t],function(e){o[t]=e,++r===n.length&&i.apply(a,o)})}(e)}function p(e,t){var n=e.dependencies&&e.dependencies.length,i=e["with"]&&e["with"].length,o=(h[e.url]||(h[e.url]={count:0,dependencies:!n,"with":!i,complete:!1,create:!1,url:e.url}),h[e.url]);function r(){o.complete&&o.dependencies&&o["with"]&&(e.module?t(f[e.module]):t())}if(o.count++,!o.dependencies&&n)o.dependencies=!0,m(e.dependencies,function(){p(e,t)});else if(!o["with"]&&i)o["with"]=!0,m(e["with"].concat(e),r);else if(o.create)s.variableHasValue(function(){return o.complete},r,300);else{o.create=!0;var c,n=e.type,i=e.url,l=function(){o.complete=!0,r()},a=e.attribute;if(g){if("js"===n?(c=document.createElement("script")).src=i:((c=document.createElement("link")).setAttribute("rel","stylesheet"),c.href=i),a)for(var u in a)c.setAttribute(u,a[u]);c.onload=c.onerror=l,g.appendChild(c)}}}function t(e,t,n){i=t,t=i=s.isString(i)?i:JSON.stringify(i);var i=e+"="+encodeURI(t)+";path=/;";"number"==typeof n&&((e=new Date).setTime(e.getTime()+n),i+="expires="+e.toUTCString()),document.cookie=i}function v(t,e){s.isArray(e)?s.forEach(e,function(e){t.classList.remove(e)}):t.classList.remove(e)}function b(t,e){s.isArray(e)?s.forEach(e,function(e){t.classList.add(e)}):t.classList.add(e)}e.addClass=b,e.downloadFileByArrayBuffer=function(e,t){var n=f.URL;l(e=n.createObjectURL(new Blob([e])),t),n.revokeObjectURL(e),undefined},e.downloadFileByUrl=l,e.getCookie=function(e,t){t=t||document.cookie;var i="";return t.replace(new RegExp("(^|;\\s)"+e+"=(.*?)($|(;\\s))"),function(e,t,n){i=u(n)}),i},e.getJsFileBaseUrl=function(e){e=e||0;for(var t="/[^/]*",n=document.scripts,i=0;i<e;i++)t+=t;return n[n.length-1].src.replace(new RegExp(t+"$"),"")},e.getRelativePos=function(e){var t=e.getBoundingClientRect();return{x:t.left,y:t.top,height:e.offsetHeight,width:e.offsetWidth,clientWidth:e.clientWidth,clientHeight:e.clientHeight}},e.getStorage=function(e){return e=localStorage.getItem(e),s.defaultValue(e,"")},e.getWindowSize=function(){return{width:document.documentElement.clientWidth||document.body.clientWidth||window.innerWidth||0,height:document.documentElement.clientHeight||document.body.clientHeight||window.innerHeight||0}},e.libraryFile=function(e,t){console.log("加载器",e,h),(e instanceof Array?m:p)(e,t)},e.observeDomResize=function(n,i,o,e){var r=[0,0,!1],c=[0,0,!1],l=[0,0,!1],a=[0,0,!1],t=d.add(u,e=e||500);function u(){var e,t;document.body.contains(n)?(e=!1,null!=o&&o.includes("position")&&(t=n.getBoundingClientRect(),l[0]=t.left,l[2]=l[0]!==l[1],a[0]=t.top,a[2]=a[0]!==a[1]),r[0]=n.offsetWidth,r[2]=r[0]!==r[1],c[0]=n.offsetHeight,c[2]=c[0]!==c[1],r[2]&&(r[1]=r[0],null!=o&&o.includes("width")&&(e=!0)),c[2]&&(c[1]=c[0],null!=o&&o.includes("height")&&(e=!0)),(a[2]||l[2])&&(a[1]=a[0],l[1]=l[0],null!=o&&o.includes("position")&&(e=!0)),o&&0!==o.length||(r[2]||c[2]||a[2]||l[2])&&(e=!0),e&&i(n)):s()}function s(){d.remove(t),r=undefined,c=undefined}return u(),s},e.prevent=function(e,t,n){e=e||f.event,n=!1!==n,(t=!1!==t)&&f.event?f.event.cancelBubble=!0:e.stopPropagation(),n&&f.event?f.event.returnValue=!1:e.preventDefault()},e.removeClass=v,e.removeCookie=function(e){t(e,"",-1)},e.removeElement=function(e){e.parentNode&&e.parentNode.removeChild(e)},e.removeStorage=function(e){localStorage.removeItem(e)},e.replaceClass=function(e,t,n){v(e,t),b(e,n)},e.setCookie=t,e.setStorage=function(e,t){localStorage.setItem(e,(e=t,e=s.isString(e)?e:JSON.stringify(e)))},e.toggleClass=function(t,e){s.isArray(e)?s.forEach(e,function(e){t.classList.toggle(e)}):t.classList.toggle(e)}});
|