HTML5和Webkit实现树叶飘落动画的案例-创新互联
小编给大家分享一下HTML5和Webkit实现树叶飘落动画的案例,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
目前创新互联已为上千多家的企业提供了网站建设、域名、雅安服务器托管、网站托管维护、企业网站设计、岚皋网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。实现如图所示的东西效果(落叶下落):
html代码:
HTML5树叶飘落动画
这是基于webkit的落叶动画
css代码: body{ background-color: #4E4226; } #container { position: relative; height: 700px; width: 500px; margin: 10px auto; overflow: hidden; border: 4px solid #5C090A; background: #4E4226 url('images/backgroundLeaves.jpg') no-repeat top left; } #leafContainer { position: absolute; width: 100%; height: 100%; } #message{ position: absolute; top: 160px; width: 100%; height: 300px; background:transparent url('images/textBackground.png') repeat-x center; color: #5C090A; font-size: 220%; font-family: 'Georgia'; text-align: center; padding: 20px 10px; -webkit-box-sizing: border-box; -webkit-background-size: 100% 100%; z-index: 1; } em { font-weight: bold; font-style: normal; } #leafContainer > p { position: absolute; width: 100px; height: 100px; -webkit-animation-iteration-count: infinite; -webkit-animation-direction: normal; -webkit-animation-timing-function: linear; } #leafContainer > p > img { position: absolute; width: 100px; height: 100px; -webkit-animation-iteration-count: infinite; -webkit-animation-direction: alternate; -webkit-animation-timing-function: ease-in-out; -webkit-transform-origin: 50% -100%; } @-webkit-keyframes fade{ 0% { opacity: 1; } 95% { opacity: 1; } 100% { opacity: 0; } } @-webkit-keyframes drop{ 0% { -webkit-transform: translate(0px, -50px); } 100% { -webkit-transform: translate(0px, 650px); } } @-webkit-keyframes clockwiseSpin{ 0% { -webkit-transform: rotate(-50deg); } 100% { -webkit-transform: rotate(50deg); } } @-webkit-keyframes counterclockwiseSpinAndFlip { 0% { -webkit-transform: scale(-1, 1) rotate(50deg); } 100% { -webkit-transform: scale(-1, 1) rotate(-50deg); } } js代码: const NUMBER_OF_LEAVES = 30; function init(){ var container = document.getElementById('leafContainer'); for (var i = 0; i < NUMBER_OF_LEAVES; i++) { container.appendChild(createALeaf()); } } function randomInteger(low, high){ return low + Math.floor(Math.random() * (high - low)); } function randomFloat(low, high){ return low + Math.random() * (high - low); } function pixelValue(value){ return value + 'px'; } function durationValue(value){ return value + 's'; } function createALeaf(){ var leafp = document.createElement('p'); leafp.style.top = "-100px"; leafp.style.left = pixelValue(randomInteger(0, 500)); leafp.style.webkitAnimationName = 'fade, drop'; var fadeAndDropDuration = durationValue(randomFloat(5, 11)); leafp.style.webkitAnimationDuration = fadeAndDropDuration + ', ' + fadeAndDropDuration; var leafDelay = durationValue(randomFloat(0, 5)); leafp.style.webkitAnimationDelay = leafDelay + ', ' + leafDelay; var image = document.createElement('img'); image.src = 'images/realLeaf' + randomInteger(1, 5) + '.png'; var spinAnimationName = (Math.random() < 0.5) ? 'clockwiseSpin' : 'counterclockwiseSpinAndFlip'; image.style.webkitAnimationName = spinAnimationName; var spinDuration = durationValue(randomFloat(4, 8)); image.style.webkitAnimationDuration = spinDuration; leafp.appendChild(image); return leafp; } window.addEventListener('load', init, false);
PS:下面看下html5 canvas处理连续帧图片,下面的代码基于IE8以上
Canvas Demo
以上是HTML5和Webkit实现树叶飘落动画的案例的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联网站制作公司行业资讯频道!
标题名称:HTML5和Webkit实现树叶飘落动画的案例-创新互联
标题URL:http://azwzsj.com/article/csopcd.html