index.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import React, { useRef, useCallback, useEffect } from 'react';
  2. import ReactDOM from 'react-dom/client';
  3. import './index.css';
  4. import { HashRouter as Router, Routes, Route } from 'react-router-dom';
  5. import Home from './page/home/iindex';
  6. import QuestionList from './page/question/index';
  7. import Rank from './page/rank/index';
  8. const root = ReactDOM.createRoot(
  9. document.getElementById('root') as HTMLElement
  10. );
  11. let tempW = document.documentElement.clientWidth
  12. let tempH = document.documentElement.clientHeight
  13. let tempMax = tempW >= tempH ? tempW : tempH
  14. let tempMin = tempW >= tempH ? tempH : tempW
  15. const pageBi = Math.round(Number((tempMax / tempMin).toFixed(2)))
  16. const App = () => {
  17. const rootRef = useRef<any>(null)
  18. const planSize = {
  19. width: 1920,
  20. height: Math.round(Number((1920 / pageBi).toFixed(0)))
  21. }
  22. const pageFullChangeFu = useCallback(() => {
  23. let width = document.documentElement.clientWidth
  24. let height = document.documentElement.clientHeight
  25. if (width >= height) {
  26. if (tempMax - width > 100) return
  27. const sizeW = width / planSize.width
  28. let sizeH = height / planSize.height
  29. let moveX = (planSize.width - width) / 2
  30. let moveY = (planSize.height - height) / 2
  31. if (width >= planSize.width) moveX = 0
  32. rootRef.current.style.left = '0'
  33. rootRef.current.style.transform = `translate(${-moveX}px,${-moveY}px) scale(${sizeW},${sizeH}) rotate(0deg)`
  34. rootRef.current.style.transformOrigin = 'center'
  35. } else {
  36. if (tempMax - height > 100) return
  37. // 竖屏
  38. const temp = width
  39. width = height
  40. height = temp
  41. const sizeW = width / planSize.width
  42. let sizeH = height / planSize.height
  43. rootRef.current.style.left = '100%'
  44. rootRef.current.style.transform = `rotate(90deg) scale(${sizeW},${sizeH})`
  45. rootRef.current.style.transformOrigin = 'left top'
  46. }
  47. // 横竖屏变化的时候 刷新页面
  48. // if (window.isHH !== isHHTemp) {
  49. // window.location.reload()
  50. // }
  51. }, [planSize.height, planSize.width])
  52. useEffect(() => {
  53. rootRef.current = document.querySelector('#root')
  54. rootRef.current.style.width = planSize.width + 'px'
  55. rootRef.current.style.height = planSize.height + 'px'
  56. pageFullChangeFu()
  57. window.addEventListener('resize', pageFullChangeFu, false)
  58. }, [pageFullChangeFu, planSize.height, planSize.width])
  59. return (
  60. <Router>
  61. <Routes>
  62. <Route path="/" element={<Home />} />
  63. <Route path="/question" element={<QuestionList />} />
  64. <Route path="/rank" element={<Rank />} />
  65. </Routes>
  66. </Router>
  67. );
  68. };
  69. root.render(
  70. <React.StrictMode>
  71. <App />
  72. </React.StrictMode>
  73. );