// 高亮关键词
export const replaceToBright = (str: string = ''): string => {
if (typeof str !== 'string') {
return str;
}
return str.replace(/\[(.+?)\]/ig, '$1');
}
// 换行
export const replaceToBreak = (str: string = '', pattern = /\^/g): string => {
if (typeof str !== 'string') {
return str;
}
return str.replace(pattern, '
');
}
// 空格
export const replaceToSpace = (str: string = ''): string => {
if (typeof str !== 'string') {
return str;
}
return str.replace(/\&/g, ' ');
}
// 滚动条在Y轴上的滚动距离
export const getScrollTop = () => {
let scrollTop = 0; let bodyScrollTop = 0; let documentScrollTop = 0;
if (document.body) {
bodyScrollTop = document.body.scrollTop;
}
if (document.documentElement) {
documentScrollTop = document.documentElement.scrollTop;
}
scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
return scrollTop;
};
// 文档的总高度
export const getScrollHeight = () => {
let scrollHeight = 0; let bodyScrollHeight = 0; let documentScrollHeight = 0;
if (document.body) {
bodyScrollHeight = document.body.scrollHeight;
}
if (document.documentElement) {
documentScrollHeight = document.documentElement.scrollHeight;
}
scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
return scrollHeight;
};
// 浏览器视口的高度
export const getWindowHeight = () => {
let windowHeight = 0;
if (document.compatMode === 'CSS1Compat') {
windowHeight = document.documentElement.clientHeight;
} else {
windowHeight = document.body.clientHeight;
}
return windowHeight;
};
// url 转 object
export const queryStrToParse = (name: string, queryString: string) => {
if (queryString.indexOf('?') > -1) {
const [, paramsStr] = queryString.split('?');
const params = paramsStr.split('&');
const queryObject = {};
for (let index = 0, lens = params.length; index < lens; index++) {
const element = params[index];
const [key, value] = element.split('=');
queryObject[key] = value;
}
return queryObject[name];
};
}