基于HTML5陀螺仪实现挪移动画结果-
发布时间:08/01 来源:未知 浏览:
关键词:
比来用ofo小黄车App的时候,发明之前下方扫一扫酿成了一个眼睛动的小黄人,觉得蛮成心思的,这里用HTML5仿一下结果。
ofo眼睛结果
代码实现
Document 《script》 'use 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.querySelector('#eyeLeft'); var eyeRight = document.querySelector('#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)'; }, false); } else { document.querySelector('body').innerHTML = '阅读器不支撑DeviceOrientationEvent'; } 《script》
终究结果
IE8不兼容rgba()怎样处置
以上就是基于HTML5陀螺仪实现挪移动画结果的细致内容,更多请关注 百分百源码网 其它相干文章!