CSS转换,过渡效果和动画

CSS中,有时为了更吸引眼球,可以采用一些动画或拉伸等让元素更具有趣味性

1.转换transform

  • translate:平移
    translate(x,y) //x,y轴方向平移
<div class="div">
    <div class="div1"></div>
</div>

<style>
        .div1{
            background-color: red;
            height: 100px;
            width: 100px;
            transform:translate(100px,200px);
    	}

    </style>

结果如下
没有加transform之前:
在这里插入图片描述
添加transform之后:
在这里插入图片描述

  • rotate:旋转
    X/Y/Z轴旋转
.div1{
            background-color: red;
            height: 100px;
            width: 100px;
            margin-top: 100px;
            margin-left: 300px;
            transform:rotateX/Y/Z(50deg);

        }

结果如下
x轴
在这里插入图片描述

Y轴
在这里插入图片描述
Z轴
在这里插入图片描述

  • 比例scale
.div1{
            background-color: red;
            height: 100px;
            width: 100px;
            margin-top: 100px;
            margin-left: 300px;
            transform: scale(1.5);

        }

结果如下
添加scale前
在这里插入图片描述

添加scale后
在这里插入图片描述

2.过渡效果transtion

添加动态效果时,为了能更加好看,清楚,常常会添加过渡效果,例
当鼠标放在下面div上时,让其旋转180度

 .div1{
            background-color: red;
            height: 100px;
            width: 100px;
            margin-top: 100px;
            margin-left: 300px;
            transition: all 1s;
        }
        .div1:hover{
            transform: rotate(180deg);
        }

因为无法放动图,其效果无法体现,图片省略

3.动画

想要实现动画,就要使用@keyframes,在里面写要添加的动画


.div1{
            background-color: red;
            height: 100px;
            width: 100px;
            transition: all 1s;
            animation: my 1s;
        }
        @keyframes my {
            from{

            }
            to{
                margin-left: 300px;
                margin-top: 200px;
            }
        }

@keyframes后面是自己定义的动画名称,在需要动画的元素里添加这个动画,以及动画要执行的时间(图省略)
animation里还有以下几个属性:

  • animation-name 要执行的动画名称
  • animation-duration 要执行多长时间,通常和动画名称写在一起,简写为animation
  • animation-delay:延迟多长时间
  • animation-direction:动画在下一个是否为逆向播放状态,默认不逆向,如果写成animation-direction:alternate;
  • animation-fill-mode:forwards 动画结束停在最后的那个状态,
  • animation-iteration-count: 4;播放的次数,如果值为infinite,则无限循环播放
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值