面试题答案
一键面试利用 Navigator 组件优化过渡动画思路
- 检测设备特性:
- 使用
window.matchMedia
来检测屏幕尺寸,例如:
const isSmallScreen = window.matchMedia('(max - width: 600px)').matches;
- 对于分辨率和刷新率,可以通过
window.devicePixelRatio
获取设备像素比(与分辨率相关),以及通过window.screen.refreshRate
获取刷新率(部分浏览器支持)。
- 使用
- 根据设备特性调整过渡动画:
- 屏幕尺寸:
- 小屏幕设备通常性能相对较弱,可简化过渡动画,如使用更短的动画时长、减少复杂的动画效果。例如,在 React 中,对于
react - navigation
的Navigator
组件:
import { createStackNavigator } from '@react - navigation/stack'; const Stack = createStackNavigator(); const smallScreenOptions = { transitionSpec: { open: { duration: 200, easing: 'ease - in - out' }, close: { duration: 200, easing: 'ease - in - out' } } }; const largeScreenOptions = { transitionSpec: { open: { duration: 300, easing: 'ease - out - quad' }, close: { duration: 300, easing: 'ease - out - quad' } } }; function App() { const isSmall = window.matchMedia('(max - width: 600px)').matches; const screenOptions = isSmall? smallScreenOptions : largeScreenOptions; return ( <NavigationContainer> <Stack.Navigator screenOptions={screenOptions}> <Stack.Screen name="Home" component={HomeScreen} /> <Stack.Screen name="Details" component={DetailsScreen} /> </Stack.Navigator> </NavigationContainer> ); }
- 小屏幕设备通常性能相对较弱,可简化过渡动画,如使用更短的动画时长、减少复杂的动画效果。例如,在 React 中,对于
- 分辨率:
- 高分辨率设备可以支持更细腻的动画效果。例如,在 CSS 过渡动画中,可以增加动画的帧数或者使用更复杂的贝塞尔曲线。在 React Native 的
Navigator
相关库(如react - navigation
)中,也可以根据window.devicePixelRatio
调整动画的精度。
const highResOptions = { transitionSpec: { open: { duration: 300, easing: 'cubic - bezier(0.1, 0.57, 0.1, 1)' }, close: { duration: 300, easing: 'cubic - bezier(0.1, 0.57, 0.1, 1)' } } }; const lowResOptions = { transitionSpec: { open: { duration: 200, easing: 'ease - in - out' }, close: { duration: 200, easing: 'ease - in - out' } } }; function App() { const isHighRes = window.devicePixelRatio >= 3; const screenOptions = isHighRes? highResOptions : lowResOptions; return ( <NavigationContainer> <Stack.Navigator screenOptions={screenOptions}> <Stack.Screen name="Home" component={HomeScreen} /> <Stack.Screen name="Details" component={DetailsScreen} /> </Stack.Navigator> </NavigationContainer> ); }
- 高分辨率设备可以支持更细腻的动画效果。例如,在 CSS 过渡动画中,可以增加动画的帧数或者使用更复杂的贝塞尔曲线。在 React Native 的
- 刷新率:
- 高刷新率设备(如 120Hz 或 144Hz)可以设置更短的动画帧间隔来实现更流畅的动画。例如,在 JavaScript 动画中,根据
window.screen.refreshRate
调整requestAnimationFrame
的调用频率。在Navigator
组件过渡动画中,可以相应调整动画的速度。
const highRefreshOptions = { transitionSpec: { open: { duration: Math.floor(300 / (window.screen.refreshRate / 60)), easing: 'ease - out - quad' }, close: { duration: Math.floor(300 / (window.screen.refreshRate / 60)), easing: 'ease - out - quad' } } }; const lowRefreshOptions = { transitionSpec: { open: { duration: 300, easing: 'ease - out - quad' }, close: { duration: 300, easing: 'ease - out - quad' } } }; function App() { const isHighRefresh = window.screen.refreshRate >= 120; const screenOptions = isHighRefresh? highRefreshOptions : lowRefreshOptions; return ( <NavigationContainer> <Stack.Navigator screenOptions={screenOptions}> <Stack.Screen name="Home" component={HomeScreen} /> <Stack.Screen name="Details" component={DetailsScreen} /> </Stack.Navigator> </NavigationContainer> ); }
- 高刷新率设备(如 120Hz 或 144Hz)可以设置更短的动画帧间隔来实现更流畅的动画。例如,在 JavaScript 动画中,根据
- 屏幕尺寸:
通过上述方式,根据不同设备的屏幕尺寸、分辨率和刷新率,动态调整 Navigator
组件的过渡动画属性,从而优化页面切换的过渡动画效果,使其适应各种响应式布局需求并保持流畅。