css动画效果代码案例?html动画代码
大家好,今天小编来为大家解答以下的问题,关于css动画效果代码案例,html动画代码这个很多人还不知道,现在让我们一起来看看吧!
5、CSS样式之动画效果
阴影:通过合理添加阴影可以使平面网页内容显示出立体的效果
1、 box-shadow:盒子阴影
(1)阴影在x轴方向的偏移,正右负左
(2)阴影在y轴方向的偏移,正下负上
(3)阴影的模糊度,数值越大,阴影越模糊
(4)阴影的范围,数值越大,阴影越大
(5)阴影颜色
(6)阴影位置,默认outset盒子外阴影, inset盒子内阴影
2、 text-shadow:文字阴影
(1)阴影x轴偏移
(2)阴影y轴偏移
(3)阴影模糊度
(4)阴影颜色
文字阴影不能像盒子阴影一样叠加阴影。
可以通过渐变为标签设置一定梯度变化的背景色
渐变色只能给标签的background-image样式赋值。
1、线性渐变:-webkit-linear-gradient
1)线性的角度或方向,默认从上到下渐变
(1) left/ right/ top/ bottom设置渐变的开始方向
(2)角度的设置,单位deg, 0deg在3点钟方向,正角度逆时针旋转,负角度顺时针旋转。
2)渐变的颜色和阶段,如果不设置阶段,各个自动平分
2、径向渐变,以圆心向四周沿着半径方向渐变:-webkit-radial-gradient
(1)设置圆的类型,默认椭圆,可以设置circle
(2)设置颜色及阶段
倒影:通过-webkit-box-reflect来为标签设置倒影
(1)设置倒影方向
above:倒影出现在标签的上方
below:倒影出现在标签的下方
left:倒影出现在标签的左方
right:倒影出现在标签的右方
(2)设置倒影距离
(3)设置蒙版图片,可以设置渐变
倒影目前只在 Chrome和 Safari浏览器生效
过渡动画效果:将标签的样式变化以连续平滑的方式显示,类似于动画。
1)transition-property:设置过渡需要表现表现的样式属性,通常使用 all来设置所有样式变化都用过渡显示。
2)transition-duration:设置过渡的持续时间。
3)transition-delay:设置过渡效果的延迟时间。
4)transition-timing-function:设置过渡的速度曲线。
过渡效果,可以简写,用空格可空开过渡样式的各个值,不分先后,当存在两个时间时,第一个为过渡持续时间,第二个为过渡延迟时间。
可以设置的值:
(1)ease:先快后慢
(2)ease-in:加速
(3)ease-out:减速
(4)ease-in-out:先加速后减速
(5)linear:匀速
1、2d变换:
通过 transform来为标签设置变换
1)平移变换 translate
translateX():设置标签沿着x轴移动的距离
translateY():设置标签沿着y轴移动的距离
translate():设置标签沿着xy轴的移动距离,第一个值表示x轴平移,第二个值表示y轴平移
x轴水平向右为正方向,y轴向下为正方向
平移会保留标签原本位置,相对自身原本位置平移
2)旋转变换 rotate
默认旋转点在标签的正中心,正角度使标签沿着顺时针旋转,负角度使标签沿着逆时针旋转。
0deg方向是12点方向。
旋转点又是标签变换的坐标系原点
3)缩放变换 scale
scaleX()、scaleY()、scale()
缩放变换,放大缩小的是标签坐标系的比例,例如,放大2倍,坐标系中1px就变成了2px;注意,一旦坐标系比例发生变化,会影响其他变换,例如:平移变换100px在2倍坐标系下,就会平移200px。
transform-origin设置标签变换参照点位置:
(1)left/right/top/bottom/center来设置特殊位置
(2)通过具体像素精确设置位置
第一个值表示x轴方向对参照点位置的设置
第二个值表示y轴方向对参照点位置的设置
允许变换参照点设置在标签之外
注意,一旦为标签更改变换参照点,那么变换参照点的基准就变成了标签左上角为原点。
2d的变换总结:
(1)默认变换参照点在标签的正中心,x轴为穿过参照点水平轴,向右为正,y轴是穿过参照点竖直轴,向下为正。
(2)平移、旋转、缩放都会改变标签坐标系的状态。
(3)变换都是参照标签初始位置进行变换。
2、3D变换
设置3d变换:
设置视距:
(1)和2d变换相似,只是在2d变换平面的基础上,多出了一条,垂直于标签平面并默认向外为正的z轴。
(2)3d变换需要为变换标签的父标签设置变换类型为3d,相当于在该标签下生成了一块3d空间。
(3)3d变换下,只有平移和旋转变换,没有缩放。
(4)3d变换中,可以通过改变标签变换参照点位置来改变XYZ轴的位置
1、 animation动画,配合@keyframes来为标签设置关键帧动画
animation属性值:
1)animation-name:动画名称,用于为动画绑定关键帧
@keyframes后面的名称
2)animation-duration:动画播放时间
s为单位,时间为0无动画过程
3)animation-delay:动画延迟时间
s为单位,时间为0无延迟
4)animation-timing-function:动画缓动效果
可以设置的值:
(1)ease
(2)ease-in
(3)ease-out
(4)ease-in-out
(5)linear
5)animation-direction:设置动画方向
alternate,当播放次数大于一,返向播放
6)animation-iteration-count:设置动画播放次数
infinite,无限播放
7)animation-fill-mode:设置动画结束位置
默认 backwards,回到初始位置
forwards,停在结束位置
注意: animation同样存在简写,将样式值以空格隔开,不区分先后,两个时间同时出现,第一个为播放时间,第二个为延迟时间。
一些css3样式只在部分浏览器生效,可以通过添加兼容前缀的形式来对部分低版本浏览器兼容
例如: transition: all 1s linear;
-webkit-transition: all 1s linear;
-moz-transition: all 1s linear;
-o-transition: all 1s linear;
-ms-transition: all 1s linear;
兼容问题:
-webkit- chrome、safari
-moz- firefox
-o- opera
-ms- ie
nimation、transition、transform、gradient等css3样式都应添加前缀进行多类型多版本浏览器兼容。
css如何实现无限轮播图动画(代码示例)
本篇文章给大家带来的内容是如何实现自动无限播放的轮播图动画效果,有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所帮助。
在之前的文章【css如何实现图片的旋转展示效果】中介绍了手动无限轮播图的制作,本篇我们来看看自动无限轮播图动画的制作。下面我们就来看看动画效果是如何实现的。
1、设置动画的舞台
HTML与之前文章里的示例非常相似。我们要有一个动画发生的舞台(#stage),一个将会旋转的div容器和一系列图像:
<div id="stage">
<div id="rotator" style="-webkit-animation-name: rotator;">
<a rel="external nofollow" href="1.jpg"><img src="1.jpg" width="140"></a>
<a rel="external nofollow" href="2.jpg"><img src="2.jpg" width="140"></a>
<a rel="external nofollow" href="3.jpg"><img src="3.jpg" width="140"></a>
<a rel="external nofollow" href="4.jpg"><img src="4.jpg" width="140"></a>
<a rel="external nofollow" href="5.jpg"><img src="5.jpg" width="140"></a>
<a rel="external nofollow" href="6.jpg"><img src="6.jpg" width="140"></a>
<a rel="external nofollow" href="7.jpg"><img src="7.jpg" width="140"></a>
<a rel="external nofollow" href="8.jpg"><img src="8.jpg" width="140"></a>
</div>
</div>内联样式属性引用下面的动画@keyframes。它需要内联而不是CSS,以便我们能够使用JavaScript停止和重新启动动画。
2、在3D空间中布置照片
CSS样式用于定位多张照片,首先围绕y轴旋转它们(垂直向上翻页),然后径向向外平移:
#stage{
margin: 2em auto 1em 50%;
height: 140px;
-webkit-perspective: 1200px;
-webkit-perspective-origin: 0 50%;
}
#rotator a{
position: absolute;
left:-81px;
}
#rotator a img{
padding: 10px;
border: 1px solid#ccc;
background:#fff;
-webkit-backface-visibility: hidden;
}
#rotator a:nth-of-type(1) img{
-webkit-transform: rotateY(-90deg) translateZ(300px);
}
#rotator a:nth-of-type(2) img{
-webkit-transform: rotateY(-60deg) translateZ(300px);
}
#rotator a:nth-of-type(3) img{
-webkit-transform: rotateY(-30deg) translateZ(300px);
}
#rotator a:nth-of-type(4) img{
-webkit-transform: translateZ(300px);
background:#000;
}
#rotator a:nth-of-type(5) img{
-webkit-transform: rotateY(30deg) translateZ(300px);
}
#rotator a:nth-of-type(6) img{
-webkit-transform: rotateY(60deg) translateZ(300px);
}
#rotator a:nth-of-type(n+7){ display: none;}照片设置-81px的值代表向左移动,使前向照片在旋转轴上居中。距离是图像宽度的一半(140px/ 2)加上LHS图像填充(10px)和边框(1px)。
该阶段需要设置立体的动画,舞台从页面的中心开始,因此旋转元素下的锚元素需要向后移动以使动画居中。
我们只开始准备六张照片,左边三张,中间一张,右边两张。最左侧的照片(位置1)从左侧开始,因此仅在第一次旋转后才可见。
当照片旋转时,它会消失(显示:无),并且新照片会附加到左侧,准备从位置1旋转。在7和更高的位置可以有任意数量的照片。只有当它们移动到可见位置时,它们才会出现。
甚至可以在动画进行时使用Ajax加载新照片。
3、添加动画效果
正如我们之前尝试的那样,不是将照片轮旋转整整360度,而是我们实际做的只是旋转30度(足以从一张照片到下一张照片):
@-webkit-keyframes rotator{
from{-webkit-transform: rotateY(0deg);}
to{-webkit-transform: rotateY(30deg);}
}
#rotator{
-webkit-transform-origin: 0 0;
-webkit-transform-style: preserve-3d;
-webkit-animation-timing-function: cubic-bezier(1, 0.2, 0.2, 1);
-webkit-animation-duration: 1s;
-webkit-animation-delay: 1s;
}
#rotator:hover{
-webkit-animation-play-state: paused;
}要使关键帧在其他浏览器中工作,请复制所有样式,替换-webkit- with-moz-和-ms-,如下面的示例代码块所示。
动画完成后,它会触发一个JavaScript事件,您可以在下一节中阅读该事件。事件处理程序沿着照片移动,以便在动画重置时,而不是回到初始状态,照片都围绕圆圈移动了一步。
为了更清楚地了解正在发生的事情,中心照片已在下面的演示中突出显示。在动画发生时,您将看到突出显示的节点旋转,然后重置回起始位置,但包含不同的照片。
4、 JavaScript添加动画控制器
我们在此示例中需要JavaScript来检测动画何时结束,以便协调通过车轮重置移动的照片。没有这个,轮子只会在前两张照片之间交替出现。
document.addEventListener("DOMContentLoaded", function(){
var rotateComplete= function(){
target.style.webkitAnimationName="";
target.insertBefore(arr[arr.length-1], arr[0]);
setTimeout(function(el){
el.style.webkitAnimationName="rotator";
}, 0, target);
};
var target= document.getElementById("rotator");
var arr= target.getElementsByTagName("a");
target.addEventListener("webkitAnimationEnd", rotateComplete, false);
}, false);对于每个WebKit样式和其他引用,存在Firefox(-moz-或Moz),Opera(-o-或O),甚至Internet Explorer(-ms-或ms)的替代品-我们必须忍受的混乱直到标准最终确定。
要在Safari,Chrome,Firefox,Opera和Internet Explorer 10中使用此功能,我们需要包含以下额外的设置:
var rotateComplete= function(){
with(target.style){
webkitAnimationName= MozAnimationName= msAnimationName="";
}
target.insertBefore(arr[arr.length-1], arr[0]);
setTimeout(function(el){
with(el.style){
webkitAnimationName= MozAnimationName= msAnimationName="rotator";
}
}, 0, target);
};
var target= document.getElementById("rotator");
var arr= target.getElementsByTagName("a");
target.addEventListener("webkitAnimationEnd", rotateComplete, false);
target.addEventListener("animationend", rotateComplete, false);
target.addEventListener("MSAnimationEnd", rotateComplete, false);目前尚不清楚为何需要setTimeout。我们不需要它来设置延迟,因为使用CSS完成,但没有setTimeout(甚至0ms)动画无法重新触发。
5、效果展示
这只是横向的轮播,在之后的文章【css实现三面立体旋转无限轮播图动画】里会在本篇文章的基础上改进轮播方式,制作不一样的的轮播图动画。
如何通过css样式来实现网站logo发光动画效果
添加修改CSS代码如下:
.logo-site,.logo-sites{position:relative;float:left;margin:18px 0 0 10px;width:220px;max-height:50px;overflow:hidden;transition-duration:.5s}
.logo-site img,.logo-sites img{width:220px;max-height:50px}@media screen and(max-width:480px){
.logo-site,.logo-sites{width:140px}}@media screen and(min-width:900px){
.logo-site:before{content:"";position:absolute;left:-665px;top:-460px;width:220px;height:15px;background-color:rgba(255,255,255,.5);-webkit-transform:rotate(-45deg);-moz-transform:rotate(-45deg);-ms-transform:rotate(-45deg);-o-transform:rotate(-45deg);transform:rotate(-45deg);-webkit-animation:searchLights 1s ease-in 1s infinite;-o-animation:searchLights 1s ease-in 1s infinite;animation:searchLights 1s ease-in 1s infinite}}@-webkit-keyframes searchLights{0%{left:-100px;top:0}to{left:120px;top:100px}}@-o-keyframes searchLights{0%{left:-100px;top:0}to{left:120px;top:100px}}@-moz-keyframes searchLights{0%{left:-100px;top:0}to{left:120px;top:100px}}@keyframes searchLights{0%{left:-100px;top:0}to{left:120px;top:100px}}
.site-title{font-size:24px;font-size:2.4rem;font-weight:700;padding:0 0 2px 0}
.site-name{display:none}@media screen and(min-width:900px){
.logo-site img,.logo-sites img{transition-duration:.8s}
操作流程
把css代码当中的logo-site改成你网站包围logo的css即可(f12键审核元素,即可找到你的logo的css元素)。找到你logo的css名称然后把上面的css代码修改成你的css名称,放入css代码当中即可(防止出错,记得备份!)
以上就是logo添加css扫光效果的方法,当然你也可以运用到其他的地方。
注意,有些模板CSS代码不相同,不能贸然的使用,可能会出现错误,新手不要尝试。
END,本文到此结束,如果可以帮助到大家,还望关注本站哦!