基础适配方案
CSS媒体查询
/* 竖屏 */
@media screen and (orientation: portrait) {
.openclaw-container {
flex-direction: column;
/* 竖屏样式 */
}
}
/* 横屏 */
@media screen and (orientation: landscape) {
.openclaw-container {
flex-direction: row;
/* 横屏样式 */
}
}
JavaScript监听方向变化
// 检测屏幕方向
function detectOrientation() {
const isLandscape = window.innerWidth > window.innerHeight;
return isLandscape ? 'landscape' : 'portrait';
}
// 监听方向变化
window.addEventListener('resize', () => {
const orientation = detectOrientation();
document.body.setAttribute('data-orientation', orientation);
});
// 初始化
document.body.setAttribute('data-orientation', detectOrientation());
// 或者使用原生API
screen.orientation.addEventListener('change', () => {
console.log(`当前方向: ${screen.orientation.type}`);
});
布局适配策略
响应式布局
.openclaw-game {
/* 基础样式 */
width: 100vw;
height: 100vh;
}
/* 竖屏:垂直布局 */
@media (orientation: portrait) {
.game-area {
height: 70vh;
}
.controls {
height: 30vh;
flex-direction: row;
}
}
/* 横屏:水平布局 */
@media (orientation: landscape) {
.game-area {
width: 70vw;
}
.controls {
width: 30vw;
flex-direction: column;
}
}
网格系统适配
.openclaw-grid {
display: grid;
gap: 10px;
}
/* 竖屏:单列 */
@media (orientation: portrait) {
.openclaw-grid {
grid-template-columns: 1fr;
grid-template-rows: auto 1fr auto;
}
}
/* 横屏:多列 */
@media (orientation: landscape) {
.openclaw-grid {
grid-template-columns: 250px 1fr;
grid-template-rows: 1fr auto;
}
}
游戏界面适配方案
抓娃娃机游戏界面
.openclaw-game-container {
position: relative;
overflow: hidden;
}
/* 竖屏:上下结构 */
@media (orientation: portrait) {
.claw-machine-view {
height: 60%;
}
.control-panel {
height: 40%;
padding: 20px;
}
.joystick {
width: 150px;
height: 150px;
margin: 0 auto;
}
}
/* 横屏:左右结构 */
@media (orientation: landscape) {
.claw-machine-view {
width: 65%;
float: left;
}
.control-panel {
width: 35%;
float: right;
padding: 30px;
}
.joystick {
width: 120px;
height: 120px;
}
}
Canvas游戏区域适配
class OpenClawGame {
constructor(canvas) {
this.canvas = canvas;
this.ctx = canvas.getContext('2d');
this.init();
}
init() {
this.resizeCanvas();
window.addEventListener('resize', this.resizeCanvas.bind(this));
window.addEventListener('orientationchange', this.resizeCanvas.bind(this));
}
resizeCanvas() {
const container = this.canvas.parentElement;
const isPortrait = window.innerHeight > window.innerWidth;
if (isPortrait) {
// 竖屏:按宽度适配
this.canvas.width = container.clientWidth;
this.canvas.height = this.canvas.width * 0.8;
} else {
// 横屏:按高度适配
this.canvas.height = container.clientHeight;
this.canvas.width = this.canvas.height * 1.2;
}
this.render();
}
}
控制面板适配
虚拟摇杆布局
.control-wrapper {
display: flex;
justify-content: center;
align-items: center;
}
/* 竖屏:摇杆在底部居中 */
@media (orientation: portrait) {
.control-wrapper {
flex-direction: row;
height: 200px;
}
.virtual-joystick {
transform: scale(1.2);
}
}
/* 横屏:摇杆在右侧 */
@media (orientation: landscape) {
.control-wrapper {
flex-direction: column;
width: 200px;
}
.action-buttons {
margin-top: 20px;
}
}
按钮大小适配
.game-button {
transition: all 0.3s ease;
}
/* 竖屏:按钮较大便于触摸 */
@media (orientation: portrait) {
.game-button {
min-width: 80px;
min-height: 80px;
font-size: 16px;
margin: 10px;
}
}
/* 横屏:按钮较小节省空间 */
@media (orientation: landscape) {
.game-button {
min-width: 60px;
min-height: 60px;
font-size: 14px;
margin: 5px;
}
}
特殊处理方案
强制横屏模式
// 强制横屏显示(适用于游戏场景)
function lockLandscape() {
if (screen.orientation && screen.orientation.lock) {
screen.orientation.lock('landscape').catch(() => {
console.log('横屏锁定失败');
});
}
}
// 强制竖屏
function lockPortrait() {
if (screen.orientation && screen.orientation.lock) {
screen.orientation.lock('portrait').catch(() => {
console.log('竖屏锁定失败');
});
}
}
// 解锁方向
function unlockOrientation() {
if (screen.orientation && screen.orientation.unlock) {
screen.orientation.unlock();
}
}
横屏提示页面
<!-- 竖屏时显示提示 -->
<div class="orientation-warning">
<div class="warning-content">
<div class="rotate-icon">↻</div>
<p>请将设备横屏以获得更好体验</p>
</div>
</div>
.orientation-warning {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.9);
z-index: 9999;
justify-content: center;
align-items: center;
}
@media (orientation: portrait) and (max-width: 768px) {
.orientation-warning {
display: flex;
}
.rotate-icon {
font-size: 60px;
animation: rotate 2s infinite linear;
margin-bottom: 20px;
}
}
@keyframes rotate {
from { transform: rotate(0deg); }
to { transform: rotate(360deg); }
}
完整示例
// openclaw横竖屏管理器
class OpenClawOrientationManager {
constructor() {
this.currentOrientation = this.getOrientation();
this.callbacks = [];
this.init();
}
init() {
// 监听方向变化
window.addEventListener('resize', this.handleResize.bind(this));
if (window.screen.orientation) {
window.screen.orientation.addEventListener('change', this.handleOrientationChange.bind(this));
}
}
getOrientation() {
return window.innerWidth > window.innerHeight ? 'landscape' : 'portrait';
}
handleResize() {
const newOrientation = this.getOrientation();
if (newOrientation !== this.currentOrientation) {
this.currentOrientation = newOrientation;
this.notifyCallbacks();
}
}
handleOrientationChange() {
this.currentOrientation = this.getOrientation();
this.notifyCallbacks();
}
onOrientationChange(callback) {
this.callbacks.push(callback);
}
notifyCallbacks() {
this.callbacks.forEach(callback => {
callback(this.currentOrientation);
});
}
// 检查是否支持横屏
isLandscapeSupported() {
return window.matchMedia('(orientation: landscape)').matches;
}
// 获取安全区域(处理刘海屏)
getSafeArea() {
const style = getComputedStyle(document.documentElement);
return {
top: parseInt(style.getPropertyValue('--safe-area-top')) || 0,
bottom: parseInt(style.getPropertyValue('--safe-area-bottom')) || 0,
left: parseInt(style.getPropertyValue('--safe-area-left')) || 0,
right: parseInt(style.getPropertyValue('--safe-area-right')) || 0,
};
}
}
// 使用示例
const orientationManager = new OpenClawOrientationManager();
orientationManager.onOrientationChange((orientation) => {
document.body.classList.remove('landscape', 'portrait');
document.body.classList.add(orientation);
// 根据不同方向调整游戏参数
if (orientation === 'landscape') {
adjustForLandscape();
} else {
adjustForPortrait();
}
});
最佳实践建议
- 优先考虑竖屏:大多数用户习惯竖屏操作
- 横屏优化:为游戏核心场景提供横屏支持
- 测试覆盖:在各种设备和分辨率下测试
- 性能考虑:避免方向切换时大量DOM操作
- 用户控制:提供方向锁定选项
- 过渡动画:方向切换时添加平滑过渡
根据你的具体需求选择合适的适配方案,如果需要更具体的实现,请提供更多关于openclaw应用场景的细节。

版权声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。