mirror of
https://github.com/snowykami/neo-blog.git
synced 2025-09-26 19:16:24 +00:00
feat: 添加 useDevice 钩子以判断移动端,优化组件动画效果
This commit is contained in:
18
web/src/hooks/use-device.ts
Normal file
18
web/src/hooks/use-device.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
export function useDevice() {
|
||||
const [isMobile, setIsMobile] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
// 简单判断移动端
|
||||
const check = () => {
|
||||
const ua = navigator.userAgent;
|
||||
setIsMobile(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(ua));
|
||||
};
|
||||
check();
|
||||
window.addEventListener("resize", check);
|
||||
return () => window.removeEventListener("resize", check);
|
||||
}, []);
|
||||
|
||||
return { isMobile };
|
||||
}
|
Reference in New Issue
Block a user