css动画集锦

html-css022

css动画集锦,第1张

1.渐变动画背景

body {

background: linear-gradient(-45deg, #ee7752, #e73c7e, #23a6d5, #23d5ab)

background-size: 400% 400%

animation: gradient 15s ease infinite

height: 100vh

}

@keyframes gradient {

0% {

background-position: 0% 50%

}

50% {

background-position: 100% 50%

}

100% {

background-position: 0% 50%

}

}

2.浮动动画效果

div {

box-shadow: 0 5px 15px 0px rgba(0,0,0,0.6)

transform: translatey(0px)

animation: float 6s ease-in-out infinite

}

@keyframes float {

0% {

box-shadow: 0 5px 15px 0px rgba(0,0,0,0.6)

transform: translatey(0px)

}

50% {

box-shadow: 0 25px 15px 0px rgba(0,0,0,0.2)

transform: translatey(-20px)

}

100% {

box-shadow: 0 5px 15px 0px rgba(0,0,0,0.6)

transform: translatey(0px)

}

}

一、实现CSS3无限循环动画代码示例。

代码如下:

CSS:

@-webkit-keyframes gogogo {

0%{

-webkit-transform: rotate(0deg)

border:5px solid red

}

50%{

-webkit-transform: rotate(180deg)

background:black

border:5px solid yellow

}

100%{

-webkit-transform: rotate(360deg)

background:white

border:5px solid red

}

}

.loading{

border:5px solid black

border-radius:40px

width: 28px

height: 188px

-webkit-animation:gogogo 2s infinite linear

margin:100px

}

扩展资料

实现动画无限循环所需要的CSS属性说明:

1、infinite

在animation后面加上infinite就可以无限循环,另外还可以做反向循环使用animation-direction

2、animation-name

规定需要绑定到选择器的 keyframe 名称。

3、animation-duration

规定完成动画所花费的时间,以秒或毫秒计。

4、animation-timing-function

规定动画的速度曲线。

5、animation-delay

规定在动画开始之前的延迟。

6、animation-iteration-count

规定动画应该播放的次数。

7、animation-direction

规定是否应该轮流反向播放动画。

完美的封装了popUpBox这个类

这个类一共两个公开方法

show    显示

hidden    隐藏

其他功能请自行扩展

不敢保证简单,但每一行都写了注释

<!DOCTYPE html>

<html>

<head>

<title>CSS 点击文字或按钮打开div浮动层 </title>

<meta charset="UTF-8" />

</head>

<script>

function popUpBox(){ //创建盒子类

this.init = function(){ //初始化函数

var onloadFunc = window.onload, //将window.onload事件执行的函数保存到onloadFunc

subInit = function(o){ //需要等结构加载完成执行的函数

document.body.appendChild(o.mask) //将遮罩添加到body

document.body.appendChild(o.box) //将盒子添加到body

},

createElement = function(){ //创建元素函数,共同的样式在这里已经设置

var e = document.createElement('div') //创建一个div

e.style.position = 'fixed' //设置定位方式为跟随浏览器移动

e.style.left = '0px' //元素x轴位置

e.style.top = '0px' //元素y轴位置

e.style.zIndex = '2000' //元素z轴位置

e.style.width = window.innerWidth + 'px' //元素宽度

e.style.height = window.innerHeight + 'px' //元素高度

e.style.margin = '0px' //元素的外边距

e.style.padding = '0px' //元素的内边距

e.style.backgroundColor = 'rgba(0, 0, 0, 0.3)' //元素的背景颜色

return e //返回这个元素

}

this.mask = createElement() //创建遮罩

this.mask.onclick = (function(o){ //创建匿名函数 参数一 传入需要操作的对象

return function(){ //匿名函数返回为遮罩绑定鼠标点击事件执行函数

o.hidden() //为o这个对象执行隐藏函数

}

})(this) //立即执行匿名函数 并传入当前对象

this.box = createElement() //盒子

this.box.style.left = window.innerWidth / 2 + 'px' //设置盒子水平居中

this.box.style.top = window.innerHeight / 2 + 'px' //设置盒子垂直居中

this.box.style.width = '400px' //设置盒子宽度

this.box.style.height = '300px' //设置盒子高度

this.box.style.border = '1px solid #3399ff' //设置边框

this.box.style.borderRadius = '5px' //设置圆的边框

this.box.style.zIndex = '2001' //设置盒子的z轴

this.box.style.backgroundColor = '#FFF' //设置盒子的背景颜色

this.box.style.transform = 'translate(-50%, -50%)' //执行2d动画,把这个盒子位置向反方向移动50%

this.box.innerHTML = '在这里设置盒子的内容,关闭盒子调用实例的hidden()函数即可,如这个实例想这样 loginBox.hidden()//就可以隐藏 '

this.hidden() //隐藏遮罩和盒子

window.onload = (function(o){ //创建匿名函数 参数一 要操作的对象

return function(){ //返回onload事件执行的函数

if(typeof onloadFunc == 'function'){ //判断onloadFunc中有没有函数,防止覆盖其他功能的执行事件

onloadFunc() //执行已经绑定的函数

}

subInit(o) //执行初始化需要执行的函数

}

})(this) //立即执行匿名函数 并传入当前对象

}

this.show = function(){ //显示

this.mask.style.display = 'block'

this.box.style.display = 'block'

}

this.hidden = function(){ //隐藏

this.mask.style.display = 'none'

this.box.style.display = 'none'

}

this.init() //调用初始化函数

}

var loginBox = new popUpBox() //创建一个实例,可以创建N个,互相不干扰

</script>

<body>

<a href="#" onclick="loginBox.show()">弹出盒子</a>

</body>

</html>