Search K
Appearance
Appearance
关键词: 移动端优化, 触摸事件, 电池寿命, 网络优化, 移动端测试, 响应式设计, 性能优化, 用户体验
移动端设备具有不同于桌面端的特殊性能特点:
<!-- 移动端优化基础 -->
<div class="mobile-optimization-basics">
<h3>移动端优化基础</h3>
<div class="mobile-considerations">
<div class="consideration-card">
<h4>📱 硬件限制</h4>
<div class="consideration-content">
<ul>
<li>CPU处理能力相对较弱</li>
<li>内存容量有限</li>
<li>存储空间受限</li>
<li>电池续航考虑</li>
</ul>
<div class="optimization-tips">
<h5>优化建议:</h5>
<p>减少JavaScript执行时间,压缩资源文件,使用WebP等现代图片格式</p>
</div>
</div>
</div>
<div class="consideration-card">
<h4>🌐 网络环境</h4>
<div class="consideration-content">
<ul>
<li>网络速度不稳定</li>
<li>数据流量限制</li>
<li>连接延迟较高</li>
<li>网络切换频繁</li>
</ul>
<div class="optimization-tips">
<h5>优化建议:</h5>
<p>减少HTTP请求,使用CDN,实现离线功能,优化资源加载顺序</p>
</div>
</div>
</div>
<div class="consideration-card">
<h4>👆 交互方式</h4>
<div class="consideration-content">
<ul>
<li>触摸操作代替鼠标</li>
<li>屏幕尺寸多样化</li>
<li>输入方式受限</li>
<li>手势操作支持</li>
</ul>
<div class="optimization-tips">
<h5>优化建议:</h5>
<p>增大可点击区域,优化触摸响应,支持手势操作,简化表单输入</p>
</div>
</div>
</div>
</div>
<div class="mobile-viewport">
<h4>移动端视口配置</h4>
<div class="viewport-examples">
<div class="viewport-example">
<h5>标准配置</h5>
<pre><code><meta name="viewport"
content="width=device-width, initial-scale=1.0,
maximum-scale=1.0, user-scalable=no"></code></pre>
</div>
<div class="viewport-example">
<h5>允许缩放</h5>
<pre><code><meta name="viewport"
content="width=device-width, initial-scale=1.0,
maximum-scale=3.0, user-scalable=yes"></code></pre>
</div>
<div class="viewport-example">
<h5>固定宽度</h5>
<pre><code><meta name="viewport"
content="width=375, initial-scale=1.0"></code></pre>
</div>
</div>
</div>
</div>
<style>
.mobile-optimization-basics {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
.mobile-optimization-basics h3 {
text-align: center;
color: #333;
margin-bottom: 30px;
}
.mobile-considerations {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
gap: 25px;
margin-bottom: 40px;
}
.consideration-card {
background: white;
padding: 25px;
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
border-left: 4px solid #e74c3c;
}
.consideration-card h4 {
color: #2c3e50;
margin: 0 0 15px 0;
font-size: 18px;
}
.consideration-content ul {
margin: 0 0 15px 0;
padding-left: 20px;
}
.consideration-content li {
color: #666;
margin-bottom: 8px;
}
.optimization-tips {
background: #f8f9fa;
padding: 15px;
border-radius: 8px;
border-left: 3px solid #3498db;
}
.optimization-tips h5 {
color: #2c3e50;
margin: 0 0 10px 0;
font-size: 14px;
}
.optimization-tips p {
color: #666;
margin: 0;
font-size: 14px;
line-height: 1.5;
}
.mobile-viewport {
background: #f8f9fa;
padding: 25px;
border-radius: 12px;
}
.mobile-viewport h4 {
color: #333;
margin: 0 0 20px 0;
text-align: center;
}
.viewport-examples {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 20px;
}
.viewport-example {
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
.viewport-example h5 {
color: #2c3e50;
margin: 0 0 10px 0;
}
.viewport-example pre {
margin: 0;
overflow-x: auto;
}
.viewport-example code {
background: #2c3e50;
color: #ecf0f1;
padding: 10px;
border-radius: 4px;
display: block;
font-size: 11px;
line-height: 1.4;
}
</style><!-- 触摸事件优化 -->
<div class="touch-optimization">
<h3>触摸事件优化</h3>
<div class="touch-demos">
<div class="touch-demo">
<h4>基础触摸事件</h4>
<div class="touch-area" id="basic-touch">
<p>触摸这里测试基础事件</p>
<div class="touch-info" id="basic-info">等待触摸...</div>
</div>
</div>
<div class="touch-demo">
<h4>手势识别</h4>
<div class="touch-area" id="gesture-touch">
<p>尝试滑动、捏合等手势</p>
<div class="touch-info" id="gesture-info">等待手势...</div>
</div>
</div>
<div class="touch-demo">
<h4>触摸优化</h4>
<div class="touch-area optimized" id="optimized-touch">
<p>优化后的触摸响应</p>
<div class="touch-info" id="optimized-info">等待触摸...</div>
</div>
</div>
</div>
<div class="touch-best-practices">
<h4>触摸事件最佳实践</h4>
<div class="practices-grid">
<div class="practice-item">
<h5>🎯 增大触摸目标</h5>
<p>按钮和链接的最小尺寸应为44px×44px</p>
</div>
<div class="practice-item">
<h5>⚡ 使用passive事件</h5>
<p>提高滚动性能,减少事件处理延迟</p>
</div>
<div class="practice-item">
<h5>🚫 避免300ms延迟</h5>
<p>使用touch-action或FastClick消除点击延迟</p>
</div>
<div class="practice-item">
<h5>👆 支持多点触摸</h5>
<p>处理多指操作,提供更好的用户体验</p>
</div>
</div>
</div>
</div>
<style>
.touch-optimization {
max-width: 1200px;
margin: 20px auto;
padding: 20px;
}
.touch-optimization h3 {
text-align: center;
color: #333;
margin-bottom: 30px;
}
.touch-demos {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 20px;
margin-bottom: 40px;
}
.touch-demo {
background: white;
padding: 20px;
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
.touch-demo h4 {
color: #2c3e50;
margin: 0 0 15px 0;
text-align: center;
}
.touch-area {
background: #e3f2fd;
border: 2px dashed #2196F3;
border-radius: 8px;
padding: 30px;
text-align: center;
cursor: pointer;
transition: all 0.3s ease;
min-height: 100px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
touch-action: manipulation;
}
.touch-area:hover {
background: #bbdefb;
border-color: #1976D2;
}
.touch-area.optimized {
background: #e8f5e8;
border-color: #4CAF50;
}
.touch-area.optimized:hover {
background: #c8e6c9;
border-color: #388E3C;
}
.touch-area p {
color: #333;
margin: 0 0 10px 0;
font-weight: 500;
}
.touch-info {
color: #666;
font-size: 14px;
background: white;
padding: 10px;
border-radius: 4px;
margin-top: 10px;
min-height: 20px;
}
.touch-best-practices {
background: #f8f9fa;
padding: 25px;
border-radius: 12px;
}
.touch-best-practices h4 {
color: #333;
margin: 0 0 20px 0;
text-align: center;
}
.practices-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
}
.practice-item {
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
text-align: center;
}
.practice-item h5 {
color: #2c3e50;
margin: 0 0 10px 0;
}
.practice-item p {
color: #666;
margin: 0;
font-size: 14px;
line-height: 1.5;
}
</style>
<script>
// 触摸事件优化实现
class TouchOptimization {
constructor() {
this.startX = 0;
this.startY = 0;
this.startTime = 0;
this.isMultiTouch = false;
this.init();
}
init() {
this.setupBasicTouch();
this.setupGestureRecognition();
this.setupOptimizedTouch();
}
setupBasicTouch() {
const area = document.getElementById('basic-touch');
const info = document.getElementById('basic-info');
area.addEventListener('touchstart', (e) => {
e.preventDefault();
info.textContent = `触摸开始: ${e.touches.length}个触点`;
});
area.addEventListener('touchmove', (e) => {
e.preventDefault();
info.textContent = `触摸移动: ${e.touches.length}个触点`;
});
area.addEventListener('touchend', (e) => {
e.preventDefault();
info.textContent = `触摸结束: ${e.changedTouches.length}个触点`;
});
}
setupGestureRecognition() {
const area = document.getElementById('gesture-touch');
const info = document.getElementById('gesture-info');
area.addEventListener('touchstart', (e) => {
this.startX = e.touches[0].clientX;
this.startY = e.touches[0].clientY;
this.startTime = Date.now();
this.isMultiTouch = e.touches.length > 1;
if (this.isMultiTouch) {
info.textContent = '多点触摸检测到';
} else {
info.textContent = '单点触摸开始';
}
});
area.addEventListener('touchmove', (e) => {
e.preventDefault();
if (this.isMultiTouch) {
info.textContent = '多点手势进行中';
return;
}
const deltaX = e.touches[0].clientX - this.startX;
const deltaY = e.touches[0].clientY - this.startY;
if (Math.abs(deltaX) > Math.abs(deltaY)) {
info.textContent = deltaX > 0 ? '向右滑动' : '向左滑动';
} else {
info.textContent = deltaY > 0 ? '向下滑动' : '向上滑动';
}
});
area.addEventListener('touchend', (e) => {
const endTime = Date.now();
const duration = endTime - this.startTime;
if (duration < 200) {
info.textContent = '快速点击';
} else {
info.textContent = '长按操作';
}
});
}
setupOptimizedTouch() {
const area = document.getElementById('optimized-touch');
const info = document.getElementById('optimized-info');
// 使用passive事件监听器提高性能
area.addEventListener('touchstart', (e) => {
info.textContent = '优化触摸开始';
area.style.transform = 'scale(0.95)';
}, { passive: true });
area.addEventListener('touchend', (e) => {
info.textContent = '优化触摸结束';
area.style.transform = 'scale(1)';
}, { passive: true });
// 添加CSS touch-action属性
area.style.touchAction = 'manipulation';
// 防止双击缩放
area.addEventListener('click', (e) => {
e.preventDefault();
info.textContent = '点击事件触发';
});
}
}
// 初始化触摸优化
const touchOptimization = new TouchOptimization();
</script><!-- 电池寿命优化 -->
<div class="battery-optimization">
<h3>电池寿命优化</h3>
<div class="battery-info">
<div class="battery-status">
<h4>当前电池状态</h4>
<div class="battery-level">
<div class="battery-indicator" id="battery-level">检测中...</div>
<div class="battery-charging" id="battery-charging">充电状态检测中...</div>
<div class="battery-time" id="battery-time">剩余时间检测中...</div>
</div>
</div>
<div class="optimization-strategies">
<h4>优化策略</h4>
<div class="strategies-list">
<div class="strategy-item">
<h5>🔋 降低CPU使用</h5>
<ul>
<li>减少动画和过渡效果</li>
<li>优化JavaScript执行</li>
<li>使用requestAnimationFrame</li>
</ul>
</div>
<div class="strategy-item">
<h5>📶 网络优化</h5>
<ul>
<li>减少网络请求频率</li>
<li>使用缓存策略</li>
<li>批量处理数据传输</li>
</ul>
</div>
<div class="strategy-item">
<h5>🌙 后台管理</h5>
<ul>
<li>暂停非必要任务</li>
<li>使用Page Visibility API</li>
<li>实现节电模式</li>
</ul>
</div>
</div>
</div>
</div>
<div class="power-saving-demo">
<h4>节电模式演示</h4>
<div class="demo-controls">
<button id="enable-power-saving">启用节电模式</button>
<button id="disable-power-saving">禁用节电模式</button>
<div class="power-status" id="power-status">正常模式</div>
</div>
<div class="animated-element" id="animated-element">
<div class="animation-box">动画测试</div>
</div>
</div>
</div>
<style>
.battery-optimization {
max-width: 1200px;
margin: 20px auto;
padding: 20px;
}
.battery-optimization h3 {
text-align: center;
color: #333;
margin-bottom: 30px;
}
.battery-info {
display: grid;
grid-template-columns: 1fr 2fr;
gap: 30px;
margin-bottom: 30px;
}
.battery-status {
background: white;
padding: 25px;
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
.battery-status h4 {
color: #2c3e50;
margin: 0 0 20px 0;
text-align: center;
}
.battery-level {
text-align: center;
}
.battery-indicator {
font-size: 24px;
font-weight: bold;
color: #27ae60;
margin-bottom: 15px;
}
.battery-charging,
.battery-time {
color: #666;
margin-bottom: 10px;
}
.optimization-strategies {
background: white;
padding: 25px;
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
.optimization-strategies h4 {
color: #2c3e50;
margin: 0 0 20px 0;
text-align: center;
}
.strategies-list {
display: grid;
gap: 20px;
}
.strategy-item {
background: #f8f9fa;
padding: 20px;
border-radius: 8px;
border-left: 4px solid #3498db;
}
.strategy-item h5 {
color: #2c3e50;
margin: 0 0 10px 0;
}
.strategy-item ul {
margin: 0;
padding-left: 20px;
}
.strategy-item li {
color: #666;
margin-bottom: 5px;
font-size: 14px;
}
.power-saving-demo {
background: #f8f9fa;
padding: 25px;
border-radius: 12px;
}
.power-saving-demo h4 {
color: #333;
margin: 0 0 20px 0;
text-align: center;
}
.demo-controls {
display: flex;
justify-content: center;
gap: 15px;
margin-bottom: 30px;
flex-wrap: wrap;
}
.demo-controls button {
padding: 10px 20px;
border: none;
border-radius: 6px;
cursor: pointer;
font-size: 14px;
transition: all 0.3s ease;
}
#enable-power-saving {
background: #f39c12;
color: white;
}
#disable-power-saving {
background: #27ae60;
color: white;
}
.demo-controls button:hover {
opacity: 0.8;
}
.power-status {
background: white;
padding: 10px 20px;
border-radius: 6px;
border: 2px solid #ddd;
text-align: center;
min-width: 120px;
}
.animated-element {
text-align: center;
padding: 20px;
}
.animation-box {
display: inline-block;
padding: 20px 40px;
background: linear-gradient(45deg, #3498db, #2ecc71);
color: white;
border-radius: 10px;
animation: bounce 2s infinite;
}
@keyframes bounce {
0%, 100% { transform: translateY(0); }
50% { transform: translateY(-20px); }
}
.power-saving .animation-box {
animation: none;
background: #95a5a6;
}
@media (max-width: 768px) {
.battery-info {
grid-template-columns: 1fr;
}
.demo-controls {
flex-direction: column;
align-items: center;
}
}
</style>
<script>
// 电池寿命优化实现
class BatteryOptimization {
constructor() {
this.isPowerSavingMode = false;
this.intervals = [];
this.init();
}
init() {
this.checkBatteryStatus();
this.setupPowerSavingDemo();
this.setupVisibilityAPI();
}
async checkBatteryStatus() {
try {
if ('getBattery' in navigator) {
const battery = await navigator.getBattery();
this.updateBatteryUI(battery);
// 监听电池状态变化
battery.addEventListener('levelchange', () => this.updateBatteryUI(battery));
battery.addEventListener('chargingchange', () => this.updateBatteryUI(battery));
} else {
this.updateBatteryUI(null);
}
} catch (error) {
console.log('Battery API not supported');
this.updateBatteryUI(null);
}
}
updateBatteryUI(battery) {
const levelEl = document.getElementById('battery-level');
const chargingEl = document.getElementById('battery-charging');
const timeEl = document.getElementById('battery-time');
if (battery) {
levelEl.textContent = `${Math.round(battery.level * 100)}%`;
chargingEl.textContent = battery.charging ? '正在充电' : '未充电';
if (battery.charging) {
const chargingTime = battery.chargingTime;
timeEl.textContent = chargingTime === Infinity ?
'充电时间未知' : `充满还需 ${Math.round(chargingTime / 3600)}小时`;
} else {
const dischargingTime = battery.dischargingTime;
timeEl.textContent = dischargingTime === Infinity ?
'使用时间未知' : `还可使用 ${Math.round(dischargingTime / 3600)}小时`;
}
// 低电量时自动启用节电模式
if (battery.level < 0.2 && !battery.charging) {
this.enablePowerSaving();
}
} else {
levelEl.textContent = '不支持';
chargingEl.textContent = '不支持';
timeEl.textContent = '不支持';
}
}
setupPowerSavingDemo() {
const enableBtn = document.getElementById('enable-power-saving');
const disableBtn = document.getElementById('disable-power-saving');
enableBtn.addEventListener('click', () => this.enablePowerSaving());
disableBtn.addEventListener('click', () => this.disablePowerSaving());
}
enablePowerSaving() {
this.isPowerSavingMode = true;
// 停止所有动画
const animatedElement = document.getElementById('animated-element');
animatedElement.classList.add('power-saving');
// 减少更新频率
this.intervals.forEach(interval => clearInterval(interval));
this.intervals = [];
// 更新状态显示
const statusEl = document.getElementById('power-status');
statusEl.textContent = '节电模式';
statusEl.style.background = '#f39c12';
statusEl.style.color = 'white';
console.log('节电模式已启用');
}
disablePowerSaving() {
this.isPowerSavingMode = false;
// 恢复动画
const animatedElement = document.getElementById('animated-element');
animatedElement.classList.remove('power-saving');
// 更新状态显示
const statusEl = document.getElementById('power-status');
statusEl.textContent = '正常模式';
statusEl.style.background = 'white';
statusEl.style.color = '#333';
console.log('节电模式已禁用');
}
setupVisibilityAPI() {
// 页面可见性API
document.addEventListener('visibilitychange', () => {
if (document.hidden) {
// 页面不可见时启用节电模式
if (!this.isPowerSavingMode) {
this.enablePowerSaving();
}
} else {
// 页面可见时恢复正常模式
if (this.isPowerSavingMode) {
this.disablePowerSaving();
}
}
});
}
}
// 初始化电池优化
const batteryOptimization = new BatteryOptimization();
</script><!-- 移动端网络优化 -->
<div class="mobile-network-optimization">
<h3>移动端网络优化</h3>
<div class="network-info">
<div class="connection-info">
<h4>网络连接信息</h4>
<div class="connection-details">
<div class="detail-item">
<span>连接类型:</span>
<span id="connection-type">检测中...</span>
</div>
<div class="detail-item">
<span>网络速度:</span>
<span id="connection-speed">检测中...</span>
</div>
<div class="detail-item">
<span>网络延迟:</span>
<span id="connection-rtt">检测中...</span>
</div>
<div class="detail-item">
<span>省流量模式:</span>
<span id="save-data">检测中...</span>
</div>
</div>
</div>
<div class="optimization-techniques">
<h4>网络优化技术</h4>
<div class="techniques-grid">
<div class="technique-item">
<h5>📦 资源压缩</h5>
<p>启用Gzip/Brotli压缩,减少传输数据量</p>
</div>
<div class="technique-item">
<h5>🔄 请求合并</h5>
<p>减少HTTP请求数量,使用雪碧图和文件合并</p>
</div>
<div class="technique-item">
<h5>⚡ 缓存策略</h5>
<p>合理使用浏览器缓存和CDN缓存</p>
</div>
<div class="technique-item">
<h5>🎯 按需加载</h5>
<p>实现懒加载,只在需要时加载资源</p>
</div>
</div>
</div>
</div>
<div class="adaptive-loading">
<h4>自适应加载演示</h4>
<div class="loading-demo">
<div class="demo-controls">
<button onclick="loadHighQuality()">加载高质量内容</button>
<button onclick="loadLowQuality()">加载低质量内容</button>
<button onclick="loadAdaptive()">自适应加载</button>
</div>
<div class="content-area" id="adaptive-content">
<p>点击按钮测试不同的加载策略</p>
</div>
</div>
</div>
</div>
<style>
.mobile-network-optimization {
max-width: 1200px;
margin: 20px auto;
padding: 20px;
}
.mobile-network-optimization h3 {
text-align: center;
color: #333;
margin-bottom: 30px;
}
.network-info {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 30px;
margin-bottom: 30px;
}
.connection-info {
background: white;
padding: 25px;
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
.connection-info h4 {
color: #2c3e50;
margin: 0 0 20px 0;
text-align: center;
}
.connection-details {
display: grid;
gap: 15px;
}
.detail-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px;
background: #f8f9fa;
border-radius: 6px;
}
.detail-item span:first-child {
font-weight: 500;
color: #2c3e50;
}
.detail-item span:last-child {
color: #3498db;
font-weight: bold;
}
.optimization-techniques {
background: white;
padding: 25px;
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}
.optimization-techniques h4 {
color: #2c3e50;
margin: 0 0 20px 0;
text-align: center;
}
.techniques-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 15px;
}
.technique-item {
background: #f8f9fa;
padding: 15px;
border-radius: 8px;
border-left: 4px solid #e74c3c;
}
.technique-item h5 {
color: #2c3e50;
margin: 0 0 8px 0;
font-size: 14px;
}
.technique-item p {
color: #666;
margin: 0;
font-size: 12px;
line-height: 1.4;
}
.adaptive-loading {
background: #f8f9fa;
padding: 25px;
border-radius: 12px;
}
.adaptive-loading h4 {
color: #333;
margin: 0 0 20px 0;
text-align: center;
}
.demo-controls {
display: flex;
justify-content: center;
gap: 15px;
margin-bottom: 20px;
flex-wrap: wrap;
}
.demo-controls button {
padding: 10px 20px;
border: none;
border-radius: 6px;
background: #3498db;
color: white;
cursor: pointer;
font-size: 14px;
transition: all 0.3s ease;
}
.demo-controls button:hover {
background: #2980b9;
}
.content-area {
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
min-height: 150px;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
}
@media (max-width: 768px) {
.network-info {
grid-template-columns: 1fr;
}
.techniques-grid {
grid-template-columns: 1fr;
}
}
</style>
<script>
// 移动端网络优化
class MobileNetworkOptimization {
constructor() {
this.connectionInfo = null;
this.init();
}
init() {
this.detectNetworkInfo();
this.monitorNetworkChanges();
}
detectNetworkInfo() {
const typeEl = document.getElementById('connection-type');
const speedEl = document.getElementById('connection-speed');
const rttEl = document.getElementById('connection-rtt');
const saveDataEl = document.getElementById('save-data');
if ('connection' in navigator) {
this.connectionInfo = navigator.connection;
typeEl.textContent = this.connectionInfo.effectiveType || '未知';
speedEl.textContent = this.connectionInfo.downlink ?
`${this.connectionInfo.downlink} Mbps` : '未知';
rttEl.textContent = this.connectionInfo.rtt ?
`${this.connectionInfo.rtt} ms` : '未知';
saveDataEl.textContent = this.connectionInfo.saveData ? '启用' : '未启用';
} else {
typeEl.textContent = '不支持';
speedEl.textContent = '不支持';
rttEl.textContent = '不支持';
saveDataEl.textContent = '不支持';
}
}
monitorNetworkChanges() {
if (this.connectionInfo) {
this.connectionInfo.addEventListener('change', () => {
this.detectNetworkInfo();
this.adaptToNetworkConditions();
});
}
}
adaptToNetworkConditions() {
if (!this.connectionInfo) return;
const isSlowConnection = this.connectionInfo.effectiveType === 'slow-2g' ||
this.connectionInfo.effectiveType === '2g';
const isSaveDataEnabled = this.connectionInfo.saveData;
if (isSlowConnection || isSaveDataEnabled) {
console.log('检测到慢速网络,启用优化模式');
this.enableNetworkOptimization();
} else {
console.log('网络状况良好,使用正常模式');
this.disableNetworkOptimization();
}
}
enableNetworkOptimization() {
// 启用网络优化模式
document.body.classList.add('network-optimized');
// 禁用非必要的动画
const style = document.createElement('style');
style.textContent = `
.network-optimized * {
animation-duration: 0s !important;
transition-duration: 0s !important;
}
`;
document.head.appendChild(style);
}
disableNetworkOptimization() {
// 禁用网络优化模式
document.body.classList.remove('network-optimized');
// 恢复动画
const styles = document.querySelectorAll('style');
styles.forEach(style => {
if (style.textContent.includes('network-optimized')) {
style.remove();
}
});
}
getOptimalImageQuality() {
if (!this.connectionInfo) return 'high';
const effectiveType = this.connectionInfo.effectiveType;
const saveData = this.connectionInfo.saveData;
if (saveData || effectiveType === 'slow-2g' || effectiveType === '2g') {
return 'low';
} else if (effectiveType === '3g') {
return 'medium';
} else {
return 'high';
}
}
}
// 全局函数
function loadHighQuality() {
const contentArea = document.getElementById('adaptive-content');
contentArea.innerHTML = `
<div>
<h4>高质量内容</h4>
<p>加载了高分辨率图片和完整功能</p>
<div style="width: 200px; height: 150px; background: linear-gradient(45deg, #3498db, #2ecc71); border-radius: 8px; margin: 10px auto;"></div>
<p>文件大小: ~2MB</p>
</div>
`;
}
function loadLowQuality() {
const contentArea = document.getElementById('adaptive-content');
contentArea.innerHTML = `
<div>
<h4>低质量内容</h4>
<p>加载了压缩图片和基础功能</p>
<div style="width: 200px; height: 150px; background: #95a5a6; border-radius: 8px; margin: 10px auto;"></div>
<p>文件大小: ~200KB</p>
</div>
`;
}
function loadAdaptive() {
const networkOptimization = new MobileNetworkOptimization();
const quality = networkOptimization.getOptimalImageQuality();
const contentArea = document.getElementById('adaptive-content');
switch(quality) {
case 'low':
loadLowQuality();
break;
case 'medium':
contentArea.innerHTML = `
<div>
<h4>中等质量内容</h4>
<p>根据网络状况优化的内容</p>
<div style="width: 200px; height: 150px; background: linear-gradient(45deg, #f39c12, #e74c3c); border-radius: 8px; margin: 10px auto;"></div>
<p>文件大小: ~800KB</p>
</div>
`;
break;
case 'high':
default:
loadHighQuality();
break;
}
}
// 初始化网络优化
const mobileNetworkOptimization = new MobileNetworkOptimization();
</script><!-- 移动端测试 -->
<div class="mobile-testing">
<h3>移动端测试</h3>
<div class="testing-categories">
<div class="testing-category">
<h4>📱 设备测试</h4>
<div class="category-content">
<div class="test-methods">
<h5>真机测试</h5>
<ul>
<li>iOS设备(iPhone、iPad)</li>
<li>Android设备(不同厂商)</li>
<li>不同屏幕尺寸和分辨率</li>
<li>不同操作系统版本</li>
</ul>
</div>
<div class="test-methods">
<h5>模拟器测试</h5>
<ul>
<li>Chrome DevTools设备模拟</li>
<li>iOS模拟器(Xcode)</li>
<li>Android模拟器(Android Studio)</li>
<li>在线测试工具</li>
</ul>
</div>
</div>
</div>
<div class="testing-category">
<h4>🌐 浏览器测试</h4>
<div class="category-content">
<div class="browser-support">
<h5>主流移动浏览器</h5>
<div class="browser-grid">
<div class="browser-item">
<strong>Safari Mobile</strong>
<p>iOS默认浏览器</p>
</div>
<div class="browser-item">
<strong>Chrome Mobile</strong>
<p>Android主流浏览器</p>
</div>
<div class="browser-item">
<strong>Samsung Internet</strong>
<p>三星设备浏览器</p>
</div>
<div class="browser-item">
<strong>UC Browser</strong>
<p>中国市场流行浏览器</p>
</div>
</div>
</div>
</div>
</div>
<div class="testing-category">
<h4>⚡ 性能测试</h4>
<div class="category-content">
<div class="performance-metrics">
<h5>关键指标</h5>
<ul>
<li>首屏加载时间</li>
<li>页面完全加载时间</li>
<li>交互响应时间</li>
<li>电池消耗情况</li>
<li>内存使用情况</li>
</ul>
</div>
<div class="testing-tools">
<h5>测试工具</h5>
<ul>
<li>Lighthouse</li>
<li>WebPageTest</li>
<li>Chrome DevTools</li>
<li>Firebase Performance</li>
</ul>
</div>
</div>
</div>
</div>
<div class="testing-checklist">
<h4>移动端测试清单</h4>
<div class="checklist-grid">
<div class="checklist-section">
<h5>🎯 功能测试</h5>
<div class="checklist-items">
<label><input type="checkbox"> 触摸操作正常</label>
<label><input type="checkbox"> 手势识别正确</label>
<label><input type="checkbox"> 表单输入流畅</label>
<label><input type="checkbox"> 导航功能完整</label>
<label><input type="checkbox"> 多媒体播放正常</label>
</div>
</div>
<div class="checklist-section">
<h5>📐 UI/UX测试</h5>
<div class="checklist-items">
<label><input type="checkbox"> 响应式布局适配</label>
<label><input type="checkbox"> 字体大小合适</label>
<label><input type="checkbox"> 按钮尺寸足够</label>
<label><input type="checkbox"> 滚动体验流畅</label>
<label><input type="checkbox"> 加载状态清晰</label>
</div>
</div>
<div class="checklist-section">
<h5>🚀 性能测试</h5>
<div class="checklist-items">
<label><input type="checkbox"> 首屏时间<3秒</label>
<label><input type="checkbox"> LCP<2.5秒</label>
<label><input type="checkbox"> FID<100ms</label>
<label><input type="checkbox"> CLS<0.1</label>
<label><input type="checkbox"> 内存使用合理</label>
</div>
</div>
<div class="checklist-section">
<h5>🔧 兼容性测试</h5>
<div class="checklist-items">
<label><input type="checkbox"> iOS Safari兼容</label>
<label><input type="checkbox"> Chrome Mobile兼容</label>
<label><input type="checkbox"> 不同分辨率适配</label>
<label><input type="checkbox"> 横竖屏切换正常</label>
<label><input type="checkbox"> 网络异常处理</label>
</div>
</div>
</div>
</div>
</div>
<style>
.mobile-testing {
max-width: 1200px;
margin: 20px auto;
padding: 20px;
}
.mobile-testing h3 {
text-align: center;
color: #333;
margin-bottom: 30px;
}
.testing-categories {
display: grid;
gap: 25px;
margin-bottom: 40px;
}
.testing-category {
background: white;
padding: 25px;
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
border-left: 4px solid #9b59b6;
}
.testing-category h4 {
color: #2c3e50;
margin: 0 0 20px 0;
font-size: 18px;
}
.category-content {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
}
.test-methods h5,
.browser-support h5,
.performance-metrics h5,
.testing-tools h5 {
color: #2c3e50;
margin: 0 0 15px 0;
font-size: 16px;
}
.test-methods ul,
.performance-metrics ul,
.testing-tools ul {
margin: 0;
padding-left: 20px;
}
.test-methods li,
.performance-metrics li,
.testing-tools li {
color: #666;
margin-bottom: 8px;
font-size: 14px;
}
.browser-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 15px;
}
.browser-item {
background: #f8f9fa;
padding: 15px;
border-radius: 8px;
text-align: center;
}
.browser-item strong {
color: #2c3e50;
display: block;
margin-bottom: 5px;
}
.browser-item p {
color: #666;
margin: 0;
font-size: 12px;
}
.testing-checklist {
background: #f8f9fa;
padding: 25px;
border-radius: 12px;
}
.testing-checklist h4 {
color: #333;
margin: 0 0 20px 0;
text-align: center;
}
.checklist-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 20px;
}
.checklist-section {
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
.checklist-section h5 {
color: #2c3e50;
margin: 0 0 15px 0;
font-size: 16px;
}
.checklist-items {
display: grid;
gap: 10px;
}
.checklist-items label {
display: flex;
align-items: center;
gap: 10px;
color: #333;
cursor: pointer;
font-size: 14px;
}
.checklist-items input[type="checkbox"] {
width: 16px;
height: 16px;
}
.checklist-items label:hover {
color: #3498db;
}
@media (max-width: 768px) {
.browser-grid {
grid-template-columns: 1fr;
}
.checklist-grid {
grid-template-columns: 1fr;
}
}
</style>A: 可以使用touch-action: manipulation CSS属性,或者使用FastClick库,或者现代浏览器中通过设置viewport meta标签来消除延迟。
A: 使用响应式图片、现代图片格式(WebP、AVIF)、懒加载、根据网络条件自适应质量,以及合理的图片压缩。
A: 可以使用Network Information API的saveData属性来检测,但需要注意浏览器兼容性。
A: 优先优化首屏渲染、减少主线程阻塞、优化关键资源加载、然后是电池和网络优化。
A: 使用Service Worker实现资源缓存、提供离线页面、缓存关键数据、显示网络状态提示。
第十一章总结:我们已经完成了HTML5性能优化的全部内容,从页面加载优化到移动端专门优化,涵盖了性能监控、代码优化等关键技术。这些知识将帮助您创建高性能的Web应用。