文章摘自269个工具函数库:269个工具函数库上

浏览器操作相关browser工具函数

90.返回当前url

  1. export const currentURL = () => window.location.href;

91.获取url参数(第一种)

  1. /**
  2. * @param {*} name
  3. * @param {*} origin
  4. */
  5. export function getUrlParam(name, origin = null) {
  6. let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  7. let r = null;
  8. if (origin == null) {
  9. r = window.location.search.substr(1).match(reg);
  10. } else {
  11. r = origin.substr(1).match(reg);
  12. }
  13. if (r != null) return decodeURIComponent(r[2]);
  14. return null;
  15. }

92.获取url参数(第二种)

  1. /**
  2. * @param {*} name
  3. * @param {*} origin
  4. */
  5. export function getUrlParams(name, origin = null) {
  6. let url = location.href;
  7. let temp1 = url.split('?');
  8. let pram = temp1[1];
  9. let keyValue = pram.split('&');
  10. let obj = {};
  11. for (let i = 0; i < keyValue.length; i++) {
  12. let item = keyValue[i].split('=');
  13. let key = item[0];
  14. let value = item[1];
  15. obj[key] = value;
  16. }
  17. return obj[name];
  18. }

93.修改url中的参数

  1. /**
  2. * @param { string } paramName
  3. * @param { string } replaceWith
  4. */
  5. export function replaceParamVal(paramName,replaceWith) {
  6. var oUrl = location.href.toString();
  7. var re=eval('/('+ paramName+'=)([^&]*)/gi');
  8. location.href = oUrl.replace(re,paramName+'='+replaceWith);
  9. return location.href;
  10. }

94.删除url中指定的参数

  1. /**
  2. * @param { string } name
  3. */
  4. export function funcUrlDel(name){
  5. var loca =location;
  6. var baseUrl = loca.origin + loca.pathname + "?";
  7. var query = loca.search.substr(1);
  8. if (query.indexOf(name)>-1) {
  9. var obj = {};
  10. var arr = query.split("&");
  11. for (var i = 0; i < arr.length; i++) {
  12. arr[i] = arr[i].split("=");
  13. obj[arr[i][0]] = arr[i][1];
  14. }
  15. delete obj[name];
  16. var url = baseUrl + JSON.stringify(obj).replace(/[\"\{\}]/g,"").replace(/\:/g,"=").replace(/\,/g,"&");
  17. return url
  18. }
  19. }

95.获取窗口可视范围的高度

  1. export function getClientHeight() {
  2. let clientHeight = 0;
  3. if (document.body.clientHeight && document.documentElement.clientHeight) {
  4. clientHeight = (document.body.clientHeight < document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;
  5. }
  6. else {
  7. clientHeight = (document.body.clientHeight > document.documentElement.clientHeight) ? document.body.clientHeight : document.documentElement.clientHeight;
  8. }
  9. return clientHeight;
  10. }

96.获取窗口可视范围宽度

  1. export function getPageViewWidth() {
  2. let d = document,
  3. a = d.compatMode == "BackCompat" ? d.body : d.documentElement;
  4. return a.clientWidth;
  5. }

97.获取窗口宽度

  1. export function getPageWidth() {
  2. let g = document,
  3. a = g.body,
  4. f = g.documentElement,
  5. d = g.compatMode == "BackCompat" ? a : g.documentElement;
  6. return Math.max(f.scrollWidth, a.scrollWidth, d.clientWidth);
  7. }

98.获取窗口尺寸

  1. export function getViewportOffset() {
  2. if (window.innerWidth) {
  3. return {
  4. w: window.innerWidth,
  5. h: window.innerHeight
  6. }
  7. } else {
  8. // ie8及其以下
  9. if (document.compatMode === "BackCompat") {
  10. // 怪异模式
  11. return {
  12. w: document.body.clientWidth,
  13. h: document.body.clientHeight
  14. }
  15. } else {
  16. // 标准模式
  17. return {
  18. w: document.documentElement.clientWidth,
  19. h: document.documentElement.clientHeight
  20. }
  21. }
  22. }
  23. }

99.获取滚动条距顶部高度

  1. export function getPageScrollTop() {
  2. let a = document;
  3. return a.documentElement.scrollTop || a.body.scrollTop;
  4. }

100.获取滚动条距左边的高度

  1. export function getPageScrollLeft() {
  2. let a = document;
  3. return a.documentElement.scrollLeft || a.body.scrollLeft;
  4. }

101.开启全屏

  1. /**
  2. * @param {*} element
  3. */
  4. export function launchFullscreen(element) {
  5. if (element.requestFullscreen) {
  6. element.requestFullscreen()
  7. } else if (element.mozRequestFullScreen) {
  8. element.mozRequestFullScreen()
  9. } else if (element.msRequestFullscreen) {
  10. element.msRequestFullscreen()
  11. } else if (element.webkitRequestFullscreen) {
  12. element.webkitRequestFullScreen()
  13. }
  14. }

102.关闭全屏

  1. export function exitFullscreen() {
  2. if (document.exitFullscreen) {
  3. document.exitFullscreen()
  4. } else if (document.msExitFullscreen) {
  5. document.msExitFullscreen()
  6. } else if (document.mozCancelFullScreen) {
  7. document.mozCancelFullScreen()
  8. } else if (document.webkitExitFullscreen) {
  9. document.webkitExitFullscreen()
  10. }
  11. }

103.返回当前滚动条位置

  1. export const getScrollPosition = (el = window) => ({
  2. x: el.pageXOffset !== undefined ? el.pageXOffset : el.scrollLeft,
  3. y: el.pageYOffset !== undefined ? el.pageYOffset : el.scrollTop
  4. });

104.滚动到指定元素区域

  1. export const smoothScroll = element =>{
  2. document.querySelector(element).scrollIntoView({
  3. behavior: 'smooth'
  4. });
  5. };

105.平滑滚动到页面顶部

  1. export const scrollToTop = () => {
  2. const c = document.documentElement.scrollTop || document.body.scrollTop;
  3. if (c > 0) {
  4. window.requestAnimationFrame(scrollToTop);
  5. window.scrollTo(0, c - c / 8);
  6. }
  7. };

106.http跳转https

  1. export const httpsRedirect = () => {
  2. if (location.protocol !== 'https:') location.replace('https://' + location.href.split('//')[1]);
  3. };

107.检查页面底部是否可见

  1. export const bottomVisible = () =>{
  2. return document.documentElement.clientHeight + window.scrollY >=
  3. (document.documentElement.scrollHeight || document.documentElement.clientHeight);
  4. };

108.打开一个窗口

  1. /**
  2. * @param { string } url
  3. * @param { string } windowName
  4. * @param { number } width
  5. * @param { number } height
  6. */
  7. export function openWindow(url, windowName, width, height) {
  8. var x = parseInt(screen.width / 2.0) - width / 2.0;
  9. var y = parseInt(screen.height / 2.0) - height / 2.0;
  10. var isMSIE = navigator.appName == "Microsoft Internet Explorer";
  11. if (isMSIE) {
  12. var p = "resizable=1,location=no,scrollbars=no,width=";
  13. p = p + width;
  14. p = p + ",height=";
  15. p = p + height;
  16. p = p + ",left=";
  17. p = p + x;
  18. p = p + ",top=";
  19. p = p + y;
  20. window.open(url, windowName, p);
  21. } else {
  22. var win = window.open(
  23. url,
  24. "ZyiisPopup",
  25. "top=" +
  26. y +
  27. ",left=" +
  28. x +
  29. ",scrollbars=" +
  30. scrollbars +
  31. ",dialog=yes,modal=yes,width=" +
  32. width +
  33. ",height=" +
  34. height +
  35. ",resizable=no"
  36. );
  37. eval("try { win.resizeTo(width, height); } catch(e) { }");
  38. win.focus();
  39. }
  40. }

109.自适应页面(rem)

  1. /**
  2. * @param { number } width
  3. */
  4. export function AutoResponse(width = 750) {
  5. const target = document.documentElement;
  6. target.clientWidth >= 600
  7. ? (target.style.fontSize = "80px")
  8. : (target.style.fontSize = target.clientWidth / width * 100 + "px");
  9. }