android 落叶效果,HTML5效果:实现树叶飘落

该博客介绍了如何使用HTML5、CSS3和JavaScript创建一个逼真的落叶飘落动画效果。通过设置不同的动画属性,如淡入淡出、下落轨迹和旋转,使得树叶在网页上呈现出自然的飘落动态。此技术主要涉及前端开发中的CSS动画和JavaScript交互。
摘要由CSDN通过智能技术生成

实现如图所示的东西效果(落叶下落):

0818b9ca8b590ca3270a3433284dd417.png

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 > div {

position: absolute;

width: 100px;

height: 100px;

-webkit-animation-iteration-count: infinite;

-webkit-animation-direction: normal;

-webkit-animation-timing-function: linear;

}

#leafContainer > div > 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 leafDiv = document.createElement('div');

leafDiv.style.top = "-100px";

leafDiv.style.left = pixelValue(randomInteger(0, 500));

leafDiv.style.webkitAnimationName = 'fade, drop';

var fadeAndDropDuration = durationValue(randomFloat(5, 11));

leafDiv.style.webkitAnimationDuration = fadeAndDropDuration + ', ' + fadeAndDropDuration;

var leafDelay = durationValue(randomFloat(0, 5));

leafDiv.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;

leafDiv.appendChild(image);

return leafDiv;

}

window.addEventListener('load', init, false);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值