el去除某个class

  1. export const removeClass = (el, className) => {
  2. if (!hasClass(el, className)) {
  3. return
  4. }
  5. let reg = new RegExp('(^|\\s)' + className + '(\\s|$)', 'g')
  6. el.className = el.className.replace(reg, ' ')
  7. }

el添加某个class

  1. export const addClass = (el, className) => {
  2. if (hasClass(el, className)) {
  3. return
  4. }
  5. let newClass = el.className.split(' ')
  6. newClass.push(className)
  7. el.className = newClass.join(' ')
  8. }

el是否包含某个class

  1. export const hasClass = (el, className) => {
  2. let reg = new RegExp('(^|\\s)' + className + '(\\s|$)')
  3. return reg.test(el.className)
  4. }

el是否在视口范围内

  1. export const elementIsVisibleInViewport = (el, partiallyVisible = false) => {
  2. const { top, left, bottom, right } = el.getBoundingClientRect();
  3. const { innerHeight, innerWidth } = window;
  4. return partiallyVisible
  5. ? ((top > 0 && top < innerHeight) || (bottom > 0 && bottom < innerHeight)) &&
  6. ((left > 0 && left < innerWidth) || (right > 0 && right < innerWidth))
  7. : top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth;
  8. }

滚动到顶部

  1. const scrollToScrollbarTop = () => {
  2. let dom = document.querySelector('.scrollbar__wrap')
  3. if (dom && dom.scrollTop > 0) {
  4. dom.scrollTo({
  5. top: 0,
  6. behavior: 'smooth'
  7. })
  8. }
  9. }
  1. import { onBeforeUnmount, onMounted, ref } from 'vue'
  2. import { isClient } from '@vueuse/core'
  3. import { throttleAndDebounce } from '../utils'
  4. const threshold = 960
  5. const cubic = (value: number): number => value ** 3
  6. const easeInOutCubic = (value: number): number =>
  7. value < 0.5 ? cubic(value * 2) / 2 : 1 - cubic((1 - value) * 2) / 2
  8. export const useBackTop = (offset = 200) => {
  9. const shouldShow = ref(false)
  10. const throttleResize = throttleAndDebounce(onResize, 300)
  11. const throttleScroll = throttleAndDebounce(onScroll, 160)
  12. onMounted(() => {
  13. if (!isClient) return
  14. onResize()
  15. onScroll()
  16. window.addEventListener('resize', throttleResize)
  17. })
  18. onBeforeUnmount(() => {
  19. if (!isClient) return
  20. window.removeEventListener('resize', throttleResize)
  21. window.removeEventListener('scroll', throttleScroll)
  22. })
  23. const scrollToTop = () => {
  24. const beginTime = Date.now()
  25. const beginValue = document.documentElement.scrollTop
  26. const rAF = window.requestAnimationFrame
  27. const frameFunc = () => {
  28. const progress = (Date.now() - beginTime) / 500
  29. if (progress < 1) {
  30. document.documentElement.scrollTop =
  31. beginValue * (1 - easeInOutCubic(progress))
  32. rAF(frameFunc)
  33. } else {
  34. document.documentElement.scrollTop = 0
  35. }
  36. }
  37. rAF(frameFunc)
  38. }
  39. function onResize() {
  40. if (!isClient) return
  41. const { clientWidth } = document.body
  42. if (clientWidth < threshold) {
  43. window.addEventListener('scroll', throttleScroll)
  44. } else {
  45. window.removeEventListener('scroll', throttleScroll)
  46. }
  47. }
  48. function onScroll() {
  49. if (!isClient) return
  50. shouldShow.value = document.documentElement.scrollTop > offset
  51. }
  52. return {
  53. shouldShow,
  54. scrollToTop,
  55. }
  56. }

获取滚动的坐标

  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. });

是否为PC端

  1. export const isPC = () => {
  2. var userAgentInfo = navigator.userAgent;
  3. var Agents = ["Android", "iPhone",
  4. "SymbianOS", "Windows Phone",
  5. "iPad", "iPod"];
  6. var flag = true;
  7. for (var v = 0; v < Agents.length; v++) {
  8. if (userAgentInfo.indexOf(Agents[v]) > 0) {
  9. flag = false;
  10. break;
  11. }
  12. }
  13. return flag;
  14. }

是否是移动端

  1. export const isDeviceMobile = () => {
  2. return /android|webos|iphone|ipod|balckberry/i.test(ua)
  3. }