分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

HTML5 重力感应效果,实现摇一摇效果

发布时间:2023-09-06 01:38责任编辑:郭大石关键词:HTML
<!DOCTYPE html><html lang="en"><head> ???<meta charset="UTF-8"> ???<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> ???<meta name="format-detection" content="telephone=no" /> ???<title> HTML5 重力感应效果,实现摇一摇效果 </title> ???<style> ???????html,body,div,ul,li{ ???????????margin: 0; ???????????padding: 0; ???????} ???????ul,li{ ???????????list-style: none; ???????} ???????body{ ???????????width:100%; ???????} ???????#box{ ???????????width:90%; ???????????height:80px; ???????????background-color:orange; ???????????margin:0 auto; ???????????color:#fff; ???????????background-position: center -50px; ???????} ???</style></head><body> ???????<div id="box"></div><script> ???????//摇晃的力度 ???????var SHAKE_THRESHOLD = 2000; ?????????var last_update = 0; ?????????//初始化重力感应位置 ???????var x = y = z = last_x = last_y = last_z = 0; ?????????function deviceMotionHandler(eventData) { ?????????????var acceleration = eventData.accelerationIncludingGravity; ?????????????var curTime = new Date().getTime(); ?????????????if ( (curTime - last_update) > 100 ) { ?????????????????var diffTime = curTime - last_update; ?????????????????last_update = curTime; ?????????????????x = acceleration.x; ?????????????????y = acceleration.y; ?????????????????z = acceleration.z; ?????????????????var speed = Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 10000; ??????????????????last_x = x; ?????????????????last_y = y; ?????????????????last_z = z; ????????????????//获取 X Y Z 的数值 ???????????????document.getElementById(‘box‘).innerHTML = ‘X:‘+last_x+‘<br> Y:‘+last_y+‘<br> Z:‘+last_z; ????????????????//如果速度大于摇晃的力度那么执行alert ???????????????if (speed > SHAKE_THRESHOLD) { ?????????????????????alert(‘摇一摇‘); ????????????????????} ????????????} ?????????} ????????//判断是否支持重力感应 ???????if (window.DeviceMotionEvent) { ?????????????window.addEventListener(‘devicemotion‘, deviceMotionHandler, false); ?????????} else { ?????????????alert(‘not support mobile event‘); ?????????} ?</script></body></html>

  

HTML5 重力感应效果,实现摇一摇效果

原文地址:https://www.cnblogs.com/zhoubingyan/p/8335032.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved