如何用CSS3实现元素旋转效果

发布时间:2020-07-01 16:06:59 作者:Leah
来源:亿速云 阅读:657

如何用CSS3实现元素旋转效果?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

问题:

1、实现以下效果,且使用纯DIV+CSS

如何用CSS3实现元素旋转效果

附加说明:

1、带蝴蝶的粉色圆圈是一张图片

2、中间的“4色花”是由4个半圆通过旋转生成的,每个半圆的直径为180px

现在来具体操作

1、准备素材:当前案例的素材为带蝴蝶的粉色圆圈

如何用CSS3实现元素旋转效果

2、创建好index.html,写好架构,架构如何分析呢

思路分析:

1、目标外层是一个div,div的背景图片为一个带蝴蝶的粉色圆圈

2、div内包含一朵4色花,所以这朵花由5个部分组成,4个花瓣+1个白色小孔心

好,先按照分析,写好思路,暂时不管css的实现

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8">
<title>CSS旋转案例</title>
</head> 
<body>
<div class="wrapper">
    <div class="bottole">
        <div class="leaf leaf2"></div>
        <div class="leaf leaf3"></div>
        <div class="leaf leaf4"></div>
        <div class="leaf"></div>
        <div class="smallcircle"></div>
    </div>
</div>
</body>
</html>

3、写样式 ,创建css文件夹,里面新建index.css

思路分析:

.container * 公共样式

1、定义公共样式

.wrapper *{
    margin:0;
    padding:0;
}

.bottole 外层div设置

1、因为要保证背景图片完全显示出来,所以一定要大于背景图片

2、背景图片不能重复

  .bottole{
    width: 640px;
    height: 420px;
    background-image: url(../images/pao.png);
    background-repeat: no-repeat;
    background-position-x: 40px;
    background-position-y: -35px;
  }

.leaf 半圆 叶子

1、因为半圆直径为180,所以高度肯定是90px,然后半圆我们可以通过border-radius实现,且默认颜色我们就定义为橙色,为了让4个半圆重叠在一起,就必须使用position:absolute,然后需要设置margin-left,margin-top让它的位置呈现目标效果(这里我们可以通过不断调试得出)

  .wrapper .leaf {
    width: 180px;
    height: 90px;
    border-radius: 180px 180px 0 0;
    background-color: orange;
    position: absolute;
    margin-left: 100px;
    margin-top: 150px;
  }

.leaf2 叶子单独设置

1、该叶子的颜色为绿色,且需要旋转90度

.wrapper .leaf2 {
    background: green;
    -webkit-transform: rotate(90deg);
    transform: rotate(90deg);
  }

.leaf3 叶子单独设置

1、该叶子的颜色为蓝色,且需要旋转180度

  .wrapper .leaf3 {
    background: blue;
    -webkit-transform: rotate(180deg);
    transform: rotate(180deg);
  }

.leaf4 叶子单独设置

1、该叶子的颜色为红色,需要旋转的角度为270度

  .wrapper .leaf4 {
    background: red;
    -webkit-transform: rotate(270deg);
    transform: rotate(270deg);
  }

小圆孔 花心设置

1、小圆孔大小为20,我们可以让一个div大小都为20,然后border-radius也为20就可以制作成一个圆

2、背景色为白色

3、为了让孔位于花朵中心,我们需要设置margin-left,margin-top

  .smallcircle{
    width: 20px;
    height: 20px;
    background-color: white;
    border-radius: 20px;
    position: absolute;
    margin-left: 180px;
    margin-top: 190px;
  }

好,到目前为止,我们把想到的样式全部写好了,具体不对,我们再来修改

目前为止,css所有内容如下:

.wrapper *{
    margin:0;
    padding:0;
}
.bottole{
    width: 640px;
    height: 420px;
    border-radius: 400px;
    background-image: url(../images/pao.png);
    background-repeat: no-repeat;
    background-position-x: 40px;
    background-position-y: -35px;
  }
  .wrapper .leaf {
    width: 180px;
    height: 90px;
    border-radius: 180px 180px 0 0;
    background-color: orange;
    position: absolute;
    margin-left: 100px;
    margin-top: 150px;
  }
.wrapper .leaf2 {
    background: green;
    -webkit-transform: rotate(90deg);
    transform: rotate(90deg);
  }
  .wrapper .leaf3 {
    background: blue;
    -webkit-transform: rotate(180deg);
    transform: rotate(180deg);
  }
  .wrapper .leaf4 {
    background: red;
    -webkit-transform: rotate(270deg);
    transform: rotate(270deg);
  }
  .smallcircle{
    width: 20px;
    height: 20px;
    background-color: white;
    border-radius: 20px;
    position: absolute;
    margin-left: 180px;
    margin-top: 190px;
  }

将css引入index.html中

<!DOCTYPE html>
<html>
<head> 
<meta charset="utf-8">
<title>CSS旋转案例</title>
<link href="css/index.css" rel="stylesheet" type="text/css">
</head> 
<body>
<div class="wrapper">
    <div class="bottole">
        <div class="leaf leaf2"></div>
        <div class="leaf leaf3"></div>
        <div class="leaf leaf4"></div>
        <div class="leaf"></div>
        <div class="smallcircle"></div>
    </div>
</div>
</body>
</html>

运行效果如下:

如何用CSS3实现元素旋转效果

关于如何用CSS3实现元素旋转效果问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. css3如何让图片实现不停旋转的效果
  2. 怎么用CSS3实现图标旋转效果

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

css3

上一篇:关于IDEA的run maven启动方法

下一篇:Apache Hadoop 入门教程第二章

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》