getHashParams
获取hash上的参数
export const getHashParams = () => {
const hash = window.location.hash
const query = hash.includes('?') ? hash.split('?')[1] : ''
if (Object.is(query, '')) {
return null
} else {
let json = {}
let arr = query.split('&')
for (let i = 0; i < arr.length; i++) {
let innerArr = arr[i].split('=')
// 因为汉字不是URL的合法字符,会被浏览器自动转成UTF-8编码(手动方法为encodeURIComponent),所以需要decodeURIComponent还原转义后的URL片段
json[innerArr[0]] = decodeURIComponent(innerArr[1])
}
return json
}
}
const params = getHashParams()
search + debounce
const onChange = e => {
onInputChange(e.target.value)
}
const onInputChange = debounce(value => {
console.log('value: ', value)
// 请求
setKeyword(value)
}, 500)
<Modal
title="Add External URL Links"
visible={visible}
bodyStyle={{ padding: '16px 24px' }}
destroyOnClose
className={css`
.ant-input-group-addon{
display: none;
}
`}
onCancel={handleCancel}
onOk={handleOk}
>
<Input.Search
allowClear
maxLength={50}
style={{ width: '400px' }}
defaultValue={keyword}
suffix={null}
placeholder="Enter the name/AppleID of the MiniProgram"
onChange={onChange}
/>
</Modal>
判断元素是否在可视范围内
export function elementInViewport(element) {
const rect = element.getBoundingClientRect();
return (
(rect.top >= 0 || rect.bottom >= 0) &&
(rect.left >= 0 || rect.right >= 0) &&
rect.top <= (window.innerHeight || document.documentElement.clientHeight)
);
}
复制功能
execCommand
只对可编辑区域有效
// 复制到剪贴板
export function copy(text) {
const input = document.createElement('input');
input.setAttribute('readonly', 'readonly'); // ios避免拉起键盘
input.setAttribute('value', text);
document.body.appendChild(input);
input.focus();
input.setSelectionRange(0, 9999); // 指定选中文本的起始和结束位置
if (document.execCommand("copy")) {
document.execCommand('copy');
console.log('复制成功');
}
document.body.removeChild(input);
}