getHashParams

获取hash上的参数

  1. export const getHashParams = () => {
  2. const hash = window.location.hash
  3. const query = hash.includes('?') ? hash.split('?')[1] : ''
  4. if (Object.is(query, '')) {
  5. return null
  6. } else {
  7. let json = {}
  8. let arr = query.split('&')
  9. for (let i = 0; i < arr.length; i++) {
  10. let innerArr = arr[i].split('=')
  11. // 因为汉字不是URL的合法字符,会被浏览器自动转成UTF-8编码(手动方法为encodeURIComponent),所以需要decodeURIComponent还原转义后的URL片段
  12. json[innerArr[0]] = decodeURIComponent(innerArr[1])
  13. }
  14. return json
  15. }
  16. }
  17. const params = getHashParams()

search + debounce

  1. const onChange = e => {
  2. onInputChange(e.target.value)
  3. }
  4. const onInputChange = debounce(value => {
  5. console.log('value: ', value)
  6. // 请求
  7. setKeyword(value)
  8. }, 500)
  9. <Modal
  10. title="Add External URL Links"
  11. visible={visible}
  12. bodyStyle={{ padding: '16px 24px' }}
  13. destroyOnClose
  14. className={css`
  15. .ant-input-group-addon{
  16. display: none;
  17. }
  18. `}
  19. onCancel={handleCancel}
  20. onOk={handleOk}
  21. >
  22. <Input.Search
  23. allowClear
  24. maxLength={50}
  25. style={{ width: '400px' }}
  26. defaultValue={keyword}
  27. suffix={null}
  28. placeholder="Enter the name/AppleID of the MiniProgram"
  29. onChange={onChange}
  30. />
  31. </Modal>

判断元素是否在可视范围内

  1. export function elementInViewport(element) {
  2. const rect = element.getBoundingClientRect();
  3. return (
  4. (rect.top >= 0 || rect.bottom >= 0) &&
  5. (rect.left >= 0 || rect.right >= 0) &&
  6. rect.top <= (window.innerHeight || document.documentElement.clientHeight)
  7. );
  8. }

复制功能

execCommand 只对可编辑区域有效

  1. // 复制到剪贴板
  2. export function copy(text) {
  3. const input = document.createElement('input');
  4. input.setAttribute('readonly', 'readonly'); // ios避免拉起键盘
  5. input.setAttribute('value', text);
  6. document.body.appendChild(input);
  7. input.focus();
  8. input.setSelectionRange(0, 9999); // 指定选中文本的起始和结束位置
  9. if (document.execCommand("copy")) {
  10. document.execCommand('copy');
  11. console.log('复制成功');
  12. }
  13. document.body.removeChild(input);
  14. }