最近用ofo小黄车app的时候,发现以前下方扫一扫变成了一个眼睛动的小黄人,觉得蛮有意思的,这里用html5仿一下效果。
ofo眼睛效果
效果分析
从效果中不难看出,是使用陀螺仪事件实现的。
这里先来看一下html5中陀螺仪事件的一些概念。
陀螺仪事件为deviceorientation
,这里主要获取事件中的alpha
,beta
,gamma
。
aplha
行动装置水平放置时,绕 z 轴旋转的角度,数值为 0 度到 360 度。
beta
行动装置水平放置时,绕 x 轴旋转的角度,数值为 -180 度到 180 度。
gamma
行动装置水平放置时,绕 z 轴旋转的角度,数值为 -90 度到 90 度。
这里,只需要用到beta和gamma。
将apk解压,得到眼睛素材:
代码实现
<!doctype html><html lang="en"><head> <meta chart="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, ur-scalable=no"> <title>document</title>元宵节祝福语一句话; <style> #box{ position2021高考分数线浙江: relative; width: 300px; margin: 0 auto; } #face{ background-image: url(images/face.png); background-size: cover; width: 300px; height: 300px; position: absolute; } #eyeleft{ background-image: url(images/eye.png); background-size: cover; width: 40px; height: 40px; position: absolute; top: 90px; left: 100px; } #eyeright{ background-image: url(images/eye.png); background-size: cover; width: 40px; height: 40px; position: absolute; top: 90px; left: 190px; } #glass{ background-image: url(images/glass.png); background-size: cover; width: 300px; height: 300px; position: absolute; } </style></head><body> <div id="box"> <div id="face"></div> <div id="eyeleft"></div> <div id="eyeright"></div> <d爸爸回来啦iv id="glass"></div> <div id="log"></div> </div><script>'u strict';/** author: 王乐平* date:2017.7.17*/var eyeleftposition = { start: [70, 78], end: [100, 110]};var eyerightposition = { start: [150, 78], end: [190, 110]};var eyeleftcenterposition = { x: (eyeleftposition.end[0] - eyeleftposition.start[0]) / 2 + eyeleftposition.start[0], y: (eyeleftposition.end[1] - eyeleftposition.start[1]) / 2 + eyeleftposition.start[1]};var eyerightcenterposition = { x: (eyerightposition.end[0] - eyerightposition.start[0]) / 2 + eyerightposition.start[0], y: (eyerightposition.end[1] - eyerightposition.start[1]) / 2 + eyerightposition.start[1]};var r = 20;var eyeleft = document.querylector('#eyeleft');var eyeright = document.querylector('#eyeright');if (window.deviceorientationevent) { window.addeventlistener('deviceorientation', function (event) { let {alpha, beta, gamma} = event; eyeleft.style.left = eyeleftcenterposition.x + gamma / 90 * r + 'px'; eyeright.style.left = eyerightcenterposition.x + gamma / 90 * r + 'px'; eyeleft.style.top = eyeright.style.top = eyeleftcenterposition.y + beta / 180 * r + 'px'; eyeright.style.transform = eyeleft.style.防暑降温用品有哪些transform = eyeright.style.webkittransform = eyeleft.style.webkittransform = 'rotate(' + beta + 'deg)'; }, fal);} el { document.querylector('body').innerhtml = '浏览器不支持deviceorientationevent';}</script></body></html&g轴对称图形教案t;
最终效果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。
本文发布于:2023-04-06 11:57:29,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/83f9f268fc2caf0f92f0316e872c38c1.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:基于HTML5陀螺仪实现ofo首页眼睛移动效果的示例.doc
本文 PDF 下载地址:基于HTML5陀螺仪实现ofo首页眼睛移动效果的示例.pdf
留言与评论(共有 0 条评论) |