iOS:active或-webkit-tap-highlight无效的原因及解决方案

博客主要讲述元素在按压态/点击态的样式设置,在iOS上点击事件需设置事件才能正确触发,还介绍了适合做“点元素时覆盖遮罩颜色”效果的非标准CSS属性。同时分析了该属性在iOS上不能预期工作的原因,并给出让元素被识别成可点击元素的方法。

通常,元素在按压态/点击态时会使用 :active伪类 来设置特定的样式,这个操作在pc/Android上都没有问题,但 iOS上的点击事件(click)是模拟出来的,在iOS上使用 :active 伪类 需要设置一个touch事件,这样元素的:active伪类才能被正确触发。一般是给body标签上加一个ontouchstart属性,但是但是~注意body的渲染范围,要保证元素上的touch事件能冒到body上才行

除了:active伪类外,还有一条css属性更适合做“点元素时元素上面覆盖一层遮罩颜色”的效果:-webkit-tap-highlight,此属性非标准,只有WebKit/Safari, Blink/Chrome和某些版本的IE、Edge可以用

可是有时候,-webkit-tap-highlight在iOS上并不能像预期一样工作,这就和Safari on iOS上“clickable 元素”的概念分不开了。

在iOS,当用户tap一个clickable的元素时,事件会按mouseovermousemovemousedownmouseupclick的顺序到达,然后用户tap另一个clickable元素的时候才触发这个元素的mouseout事件;而当用于tap一个nonclickable元素的时候,啥事件都不会发生,如图所示:
在这里插入图片描述

因此,给一个被识别成了nonclickable的元素设置-webkit-tap-highlight-color属性是没用的。

不过可以给nonclickable的元素加一些响应用户操作的事件/css属性,让它被识别成clickable元素,例如:

  • cursor: pointer;
  • οnclick=“void(0)”

还可以用一些天生自带clickable的标签换掉nonclickable(主要是 div)的元素:

  • 带有href属性的<a>
  • 带href属性的<area>
  • <button>
  • <img>
  • <input>
  • 绑了form的<label>
  • <textarea>

参考文档

https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariWebContent/AdjustingtheTextSize/AdjustingtheTextSize.html#//apple_ref/doc/uid/TP40006510-SW5

https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html#//apple_ref/doc/uid/TP40006511-SW1

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>圣遗物属性发送 - 逍遥网络</title> <style> :root { --primary-color: #4CAF50; --primary-hover: #45a049; --secondary-color: #f8f9fa; --text-color: #333; --light-text: #6c757d; --border-color: #ced4da; --box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1); --transition: all 0.3s ease; --error-color: #dc3545; --warning-color: #ffc107; --info-color: #17a2b8; --purple-color: #6f42c1; } * { box-sizing: border-box; margin: 0; padding: 0; } body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background: linear-gradient(135deg, rgba(245, 247, 250, 0.9) 0%, rgba(195, 207, 226, 0.9) 100%); color: var(--text-color); line-height: 1.6; display: flex; justify-content: center; align-items: center; min-height: 100vh; padding: 20px; } .container { width: 100%; max-width: 1100px; background-color: rgba(255, 255, 255, 0.95); border-radius: 10px; box-shadow: var(--box-shadow); padding: 20px; } .header { text-align: center; margin-bottom: 20px; } h1 { color: var(--text-color); margin-bottom: 10px; font-weight: 600; text-shadow: 1px 1px 2px rgba(0,0,0,0.1); } .nav-tabs { display: flex; background: var(--secondary-color); border-radius: 8px; padding: 5px; margin-bottom: 20px; flex-wrap: wrap; } .nav-tab { flex: 1; padding: 12px 20px; text-align: center; background: none; border: none; cursor: pointer; border-radius: 6px; transition: var(--transition); font-weight: 500; min-width: 120px; } .nav-tab.active { background: var(--primary-color); color: white; } .tab-content { display: none; } .tab-content.active { display: block; } form { background-color: rgba(255, 255, 255, 0.9); padding: 30px; border-radius: 10px; box-shadow: var(--box-shadow); transition: var(--transition); } form:hover { box-shadow: 0 6px 16px rgba(0, 0, 0, 0.15); } .form-group { margin-bottom: 20px; } label { display: block; margin-bottom: 8px; font-weight: 500; color: var(--text-color); } input[type="text"], input[type="password"], input[type="number"], select { width: 100%; padding: 12px 15px; border: 1px solid var(--border-color); border-radius: 6px; font-size: 16px; transition: var(--transition); background-color: var(--secondary-color); } input[type="text"]:focus, input[type="password"]:focus, input[type="number"]:focus, select:focus { outline: none; border-color: var(--primary-color); box-shadow: 0 0 0 3px rgba(76, 175, 80, 0.2); } .attribute_entry { background-color: rgba(248, 249, 250, 0.8); padding: 15px; border-radius: 6px; margin-bottom: 15px; border: 1px dashed var(--border-color); position: relative; } .attribute_entry label { display: inline-block; margin-right: 10px; margin-bottom: 0; } .attribute_entry select, .attribute_entry input[type="number"] { width: auto; min-width: 200px; margin-right: 10px; } .remove_attribute { background-color: var(--error-color); color: white; border: none; padding: 8px 12px; border-radius: 4px; cursor: pointer; font-size: 14px; transition: var(--transition); } .remove_attribute:hover { background-color: #c82333; } .btn { padding: 12px 24px; border: none; border-radius: 6px; cursor: pointer; font-size: 16px; transition: var(--transition); font-weight: 500; margin: 5px; } .btn-primary { background-color: var(--primary-color); color: white; } .btn-primary:hover { background-color: var(--primary-hover); } .btn-info { background-color: var(--info-color); color: white; } .btn-info:hover { background-color: #138496; } .btn-warning { background-color: var(--warning-color); color: var(--text-color); } .btn-warning:hover { background-color: #e0a800; } .btn-purple { background-color: var(--purple-color); color: white; } .btn-purple:hover { background-color: #5a359a; } input[type="submit"] { width: 100%; padding: 15px; background-color: var(--primary-color); color: white; border: none; border-radius: 6px; font-size: 18px; font-weight: 500; cursor: pointer; transition: var(--transition); margin-top: 20px; } input[type="submit"]:hover { background-color: var(--primary-hover); transform: translateY(-2px); } .inputBox { text-align: center; margin-top: 20px; } .inputBox a { color: var(--primary-color); text-decoration: none; font-weight: 500; transition: var(--transition); } .inputBox a:hover { text-decoration: underline; color: var(--primary-hover); } #divMsg { text-align: center; margin-top: 15px; color: var(--light-text); font-size: 14px; } .note { font-size: 14px; color: var(--light-text); margin-top: 5px; font-style: italic; } .sdk-result { margin-top: 20px; padding: 15px; border-radius: 6px; text-align: center; display: none; } .sdk-result.success { background-color: #d4edda; color: #155724; border: 1px solid #c3e6cb; } .sdk-result.error { background-color: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; } .sdk-code { font-family: 'Courier New', monospace; font-size: 18px; font-weight: bold; background: #2c3e50; color: white; padding: 10px; border-radius: 4px; margin: 10px 0; letter-spacing: 1px; } .sdk-codes-list { max-height: 300px; overflow-y: auto; margin: 15px 0; padding: 10px; background: #f8f9fa; border-radius: 6px; border: 1px solid var(--border-color); } .sdk-code-item { padding: 8px; margin: 5px 0; background: white; border-radius: 4px; border-left: 4px solid var(--primary-color); } .table-container { overflow-x: auto; margin-top: 20px; background: white; border-radius: 8px; box-shadow: var(--box-shadow); } table { width: 100%; border-collapse: collapse; } th, td { padding: 12px; text-align: left; border-bottom: 1px solid var(--border-color); } th { background: var(--secondary-color); font-weight: 600; } .status-used { color: var(--error-color); font-weight: bold; } .status-unused { color: var(--primary-color); font-weight: bold; } .batch-controls { display: flex; gap: 10px; align-items: center; margin-bottom: 20px; } .batch-count { width: 100px; } .form-actions { display: flex; gap: 10px; margin-top: 20px; } .form-actions .btn { flex: 1; } @media (max-width: 768px) { form { padding: 20px; } .nav-tab { min-width: 100px; padding: 10px 15px; font-size: 14px; } .attribute_entry select, .attribute_entry input[type="number"] { width: 100%; margin-bottom: 10px; } .attribute_entry label { display: block; margin-bottom: 5px; } .form-actions { flex-direction: column; } .batch-controls { flex-direction: column; align-items: stretch; } .batch-count { width: 100%; } } </style> </head> <body> <div class="container"> <div class="header"> <h1>圣遗物兑换系统</h1> <div class="inputBox"> </div> </div> <!-- 兑换CDK标签页 --> <div id="sdk" class="tab-content active"> <form id="redeemForm"> <div class="form-group"> <label for="redeem_uid">UID:</label> <input type="text" id="redeem_uid" name="uid" required placeholder="请输入您的UID"> </div> <div class="form-group"> <label for="sdk_code">CDK兑换码:</label> <input type="text" id="sdk_code" name="sdk_code" required placeholder="请输入12位CDK兑换码"> <div class="note">请输入12位大写字母和数字组成的CDK码</div> </div> <input type="hidden" name="action" value="redeem_sdk"> <input type="submit" value="兑换装备" class="btn btn-primary"> </form> <div id="divMsg">BY 逍遥网络 2025</div> <div id="redeemResult" class="sdk-result"></div> </div> </div> <script> // 标签页切换 function switchTab(tabName) { // 隐藏所有标签内容 document.querySelectorAll('.tab-content').forEach(tab => { tab.classList.remove('active'); }); // 移除所有标签的激活状态 document.querySelectorAll('.nav-tab').forEach(tab => { tab.classList.remove('active'); }); // 显示选中的标签内容 document.getElementById(tabName).classList.add('active'); // 激活选中的标签 event.target.classList.add('active'); // 如果是管理标签页,自动加载SDK列表 if (tabName === 'manage') { loadSDKList(); } } // 加载物品数据 function loadItems() { fetch('syxywl.cn-syw.txt') .then(response => response.text()) .then(data => { const lines = data.split('\n'); const selects = document.querySelectorAll('#item_id, #sdk_item_id, #batch_item_id'); selects.forEach(select => { // 清空现有选项(保留第一个提示选项) while (select.options.length > 1) { select.remove(1); } lines.forEach(line => { if (line.trim() !== '') { const [value, text] = line.split(':'); const option = document.createElement('option'); option.value = value; option.textContent = text; select.appendChild(option); } }); }); }) .catch(error => console.error('加载物品数据失败:', error)); } // 加载主属性数据 function loadMainProps() { fetch('sx1-zsx.txt') .then(response => response.text()) .then(data => { const lines = data.split('\n'); const selects = document.querySelectorAll('#mainPropId, #sdk_mainPropId, #batch_mainPropId'); selects.forEach(select => { // 清空现有选项(保留第一个提示选项) while (select.options.length > 1) { select.remove(1); } lines.forEach(line => { if (line.trim() !== '') { const [value, text] = line.split(':'); const option = document.createElement('option'); option.value = value; option.textContent = text; select.appendChild(option); } }); }); }) .catch(error => console.error('加载主属性数据失败:', error)); } // 加载属性数据 function loadAttributes() { fetch('sx2-fsx.txt') .then(response => response.text()) .then(data => { const lines = data.split('\n'); const selects = document.querySelectorAll('.attr-select'); selects.forEach(select => { // 清空现有选项(保留第一个提示选项) while (select.options.length > 1) { select.remove(1); } lines.forEach(line => { if (line.trim() !== '') { const [value, text] = line.split(':'); const option = document.createElement('option'); option.value = value; option.textContent = text; select.appendChild(option); } }); }); }) .catch(error => console.error('加载属性数据失败:', error)); } // 页面加载完成后初始化数据 document.addEventListener('DOMContentLoaded', function() { loadItems(); loadMainProps(); loadAttributes(); // 绑定表单提交事件 document.getElementById('redeemForm').addEventListener('submit', handleRedeemSubmit); }); // 处理兑换SDK表单提交 function handleRedeemSubmit(e) { e.preventDefault(); const formData = new FormData(this); fetch('send_item.php', { method: 'POST', body: formData }) .then(response => response.json()) .then(data => { const resultDiv = document.getElementById('redeemResult'); resultDiv.className = 'sdk-result ' + (data.success ? 'success' : 'error'); resultDiv.textContent = data.message; resultDiv.style.display = 'block'; if (data.success) { this.reset(); setTimeout(() => { resultDiv.style.display = 'none'; }, 5000); } }) .catch(error => { console.error('Error:', error); const resultDiv = document.getElementById('redeemResult'); resultDiv.className = 'sdk-result error'; resultDiv.textContent = '网络错误,请重试'; resultDiv.style.display = 'block'; }); } </script> </body> </html> 优化显示UI 适配手机电脑平板
最新发布
11-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值