1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
function calcRateByWidth( baseWidth: number, baseHeight: number, baseProportion: number ) { const heightRatio = parseFloat( (window.innerWidth / baseProportion / baseHeight).toFixed(5) ); const widthRatio = parseFloat((window.innerWidth / baseWidth).toFixed(5)); return { widthRatio, heightRatio }; }
function calcRateByHeight( baseWidth: number, baseHeight: number, baseProportion: number ) { const widthRatio = parseFloat( ((window.innerHeight * baseProportion) / baseWidth).toFixed(5) ); const heightRatio = parseFloat( (window.innerHeight / baseHeight).toFixed(5) ); return { widthRatio, heightRatio }; }
function calcRateByStretch(baseWidth: number, baseHeight: number) { const widthRatio = parseFloat((window.innerWidth / baseWidth).toFixed(5)); const heightRatio = parseFloat( (window.innerHeight / baseHeight).toFixed(5) ); return { widthRatio, heightRatio }; }
const handleScreenAuto = () => { const designDraftWidth = 1920; const designDraftHeight = 1080; const baseProportion = parseFloat( (designDraftWidth / designDraftHeight).toFixed(5) ); const currentRate = parseFloat( (window.innerWidth / window.innerHeight).toFixed(5) ); let scale = { widthRatio: 1, heightRatio: 1, };
scale = currentRate > baseProportion ? calcRateByHeight(designDraftWidth, designDraftHeight, baseProportion) : calcRateByWidth(designDraftWidth, designDraftHeight, baseProportion); ( document.querySelector("#screen") as any ).style.transform = `scale(${scale.widthRatio}, ${scale.heightRatio}) translate(-50%)`; }; useEffect(() => { handleScreenAuto(); window.onresize = () => handleScreenAuto(); return () => { window.onresize = null; }; }, []);
|