设备事件
if (navigator.geolocation) {
// 支持
} else {
// 不支持
}
navigator.geolocation.getCurrentPosition(geoSuccess,geoError);
上面代码指定了处理当前地理位置的两个回调函数。
- 用户同意授权
如果用户同意授权,就会调用
function geoSuccess(event) {
console.log(event.coords.latitude + ', ' + event.coords.longitude);
}
- coords.latitude:纬度
- coords.longitude:经度
- coords.accuracy:精度
- coords.altitude:海拔
- coords.altitudeAccuracy:海拔精度
大多数桌面浏览器不提供上面列表的后四个值。
- 用户拒绝授权
如果用户拒绝授权,就会调用
function geoError(event) {
console.log("Error code " + event.code + ". " + event.message);
}
- 0:未知错误,浏览器没有提示出错的原因,相当于常量
- 设置定位
var option = {
enableHighAccuracy : true,
timeout : Infinity,
maximumAge : 0
};
navigator.geolocation.getCurrentPosition(geoSuccess, geoError, option);
这个参数对象有三个成员:
- enableHighAccuracy:如果设为
var watchID = navigator.geolocation.watchPosition(geoSuccess,geoError, option);
一旦用户位置发生变化,就会调用回调函数
navigator.geolocation.clearWatch(watchID);
的,然后我们就需要在用户启动时给用户一些
警告信息
var battery = navigator.battery || navigator.webkitBattery || navigator.mozBattery;
// 电池属性
console.warn("Battery charging: ", battery.charging); // 当前电池是否在充电 true
console.warn("Battery level: ", battery.level); // 0.58
console.warn("Battery discharging time: ", battery.dischargingTime);
// 添加事件监听器
battery.addEventListener("chargingchange", function(e) {
console.warn("Battery charge change: ", battery.charging);
}, false);
if (window.DeviceOrientationEvent) {
// 支持
} else {
// 不支持
}
一旦设备的方向发生变化,会触发
window.addEventListener("deviceorientation", callback);
回调函数接受一个
function callback(event){
console.log(event.alpha);
console.log(event.beta);
console.log(event.gamma);
}
上面代码中,
- alpha:表示围绕
navigator.vibrate =
navigator.vibrate ||
navigator.webkitVibrate ||
navigator.mozVibrate ||
navigator.msVibrate;
if (navigator.vibrate) {
// 支持
}
navigator.vibrate(1000);
上面的代码使得设备振动
navigator.vibrate([500, 300, 100]);
上面代码表示,设备先振动
navigator.vibrate(0);
navigator.vibrate([]);
如果要让振动一直持续,可以使用
var vibrateInterval;
function startVibrate(duration) {
navigator.vibrate(duration);
}
function stopVibrate() {
if (vibrateInterval) clearInterval(vibrateInterval);
navigator.vibrate(0);
}
function startPeristentVibrate(duration, interval) {
vibrateInterval = setInterval(function() {
startVibrate(duration);
}, interval);
}
window.addEventListener('devicelight', function(event) {
console.log(event.value + 'lux');
});
上面代码表示,
window.addEventListener("devicelight", function(e) {
var lux = e.value;
if (lux < 50) {
document.body.className = "dim";
}
if (lux >= 50 && lux <= 1000) {
document.body.className = "normal";
}
if (lux > 1000) {
document.body.className = "bright";
}
});
@media (light-level: dim) {
/* 暗光环境 */
}
@media (light-level: normal) {
/* 正常光环境 */
}
@media (light-level: washed) {
/* 明亮环境 */
}
Touch & Tap
Touch Events
从onTouchStart
等<Tappable onTap={}>
的方式包裹一层监听组件,从而避免核心库的冗余。
|
这些事件可以被附着到页面的任意元素上,并且同样可以通过element.addEventListener
来添加事件响应函数,譬如:
window.addEventListener(
"load",
function() {
// on page load
document.body.addEventListener(
"touchstart",
function(e) {
alert(e.changedTouches[0].pageX); // alert pageX coordinate of touch point
},
false
);
},
false
);
Tap & 300ms:Tap 事件与300ms 的延迟
绝大部分基于
head
中进行如下设置:
<meta name="viewport" content="width=device-width, user-scalable=no">
或者
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
不过这种方式在
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=3"
a, button, .myelements
{
-ms-touch-action: manipulation; /* IE10*/
touch-action: manipulation; /* IE11+ */
}
click
与touchstart
,touchend
事件是不会延迟touchend
,有如下原因:
-
如果用户在不同的元素上触发了
touchstart
与touchend
事件,那么不应该触发Click 事件。 -
如果某个用户在触发了
touchstart
事件之后,因为有较大移动在touchend
之前触发了touchmove
,这个时候就不能触发Click 事件。 -
对于非手持设备同样需要处理点击事件,还需要注意不能对于同一事件触发两次
-
FastClick from FT Labs attaches appropriate listeners on mobile browsers when they’re required, i.e. solutions 2 to 4 above have not been used. The script compresses to around 10Kb.
-
Tappy! from the Filament Group is a normalized tap event library, similar in concept to PointerEvents, which compresses to 1Kb.
# Gesture Recognize Types
Multiple Touch
Drag&Drop
Scroll
Zoom
Shake
Hover
Hover on Touch

<script src="http://hammerjs.github.io/dist/hammer.min.js"></script>
<script src="your/path/hoverontouch.js"></script>
(2)HTML Structure
<div class="object" data-triggerlink="https://www.example.com">
<div class="cover">
// Put the cover Informaton here
</div>
<div class="info">
//Put the secondary Information here
</div>
</div>