知识点预备:

【1】CSS3中特别重要的transform中的rotate(),现在transform可以将元素进行2D和3D变形。

2D transform常用的transform-function的功能:

translate():用来移动元素,可以根据X轴和Y轴坐标重新定位元素位置。在此基础上有两个扩展函数:translateX()和translateY()。

  scale():用来缩小或放大元素,可以使用元素尺寸发生变化。在此基础上有两个扩展函数:scaleX()和scaleY()。rotate():用来旋转元素。

  skew():用来让元素倾斜。在此基础上有两个扩展函数:skewX()和skewY()。

  matrix():定义矩阵变形,基于X轴和Y轴坐标重新定位元素位置。

3D transform常用的transform-function的功能:

  translate3d():移元素元素,用来指定一个3D变形移动位移量translate():指定3D位移在Z轴的位移量。

  scale3d():用来缩放一个元素。scaleZ():指定Z轴的缩放向量。

  rotate3d():指定元素具有一个三维旋转的角度。

  rotateX()、rotateY()和rotateZ():让元素具有一个旋转角度。

  perspective():指定一个透视投影矩阵。

  matrix3d():定义矩阵变形。

  这里我们只需要2D中的rotate()属性用来旋转元素,

  注意:旋转的角度以顺时针方向为正(按顺势正方向角度增大)

  其他属性的用法可以参考这里W3cplus

【2】transfrom-origin

  指定变形的原点,默认是在元素的中心,可以接受一到三个参数,分别表示变形原点X轴、Y轴、Z轴的位置(可以实现许多有趣的旋转)

【3】transition

  过渡transition是一个复合属性,包括transition-property、transition-duration、transition-timing-function、transition-delay这四个子属性。通过这四个子属性的配合来完成一个完整的过渡效果

  transition-property: 过渡属性(默认值为all)

  transition-duration: 过渡持续时间(默认值为0s)

  transiton-timing-function: 过渡函数(默认值为ease函数)

  transition-delay: 过渡延迟时间(默认值为0s)
  想要详细了解该属性可以参考深入理解CSS过渡transition

【4】CSS3 :nth-of-type(n) 选择器

  选择器匹配属于父元素的特定类型的第 N 个子元素的每个元素

  好晕,还是来看个例子:

1 #box div:nth-of-type(2)

这句代码的意思是选择在#box下的第2个div

注意:如果在第一个div里嵌套了一个div,那么会选择嵌套在第一个div里的那个div(-_-!)。

好了,准备工作做完了,开始实现扇形展开效果

第一步

基本样式布局,直接看代码

结构:

 1     <div id="box">
2 <div></div>
3 <div></div>
4 <div></div>
5 <div></div>
6 <div></div>
7 <div></div>
8 <div></div>
9 <div></div>
10 <div></div>
11 <div></div>
12 </div>

样式:

 1         *{
2 margin: 0;
3 padding: 0;
4 }
5 body{
6 background: #666;
7 }
8 #box{
9 width: 1200px;
10 height: 700px;
11 background: url(img/bg.png) no-repeat;
12 position: relative;
13 margin: 100px auto;
14 }
15 #box div{
16 width: 400px;
17 height: 100px;
18 border-radius: 9px;
19 position: absolute;
20 left: 555px;
21 top: 444px;
22 /*
23
24 */
25 background: url(img/logo.png) no-repeat 280px 56px;
26 transform-origin: 35px 50px;
27 transition: 1s;
28 }
29 #box div:nth-of-type(1){
30 background-color: #417191;
31 }
32 #box div:nth-of-type(2){
33 background-color: #ce8298;
34 }
35 #box div:nth-of-type(3){
36 background-color: #f78992;
37 }
38 #box div:nth-of-type(4){
39 background-color: #fbc0a5;
40 }
41 #box div:nth-of-type(5){
42 background-color: #f8988f;
43 }
44 #box div:nth-of-type(6){
45 background-color: #f16273;
46 }
47 #box div:nth-of-type(7){
48 background-color: #9c9fcd;
49 }
50 #box div:nth-of-type(8){
51 background-color: #bae2d3;
52 }
53 #box div:nth-of-type(9){
54 background-color: #43bab8;
55 }
56 #box div:nth-of-type(10){
57 background-color: #678d95;

第二步

写扇形的展开函数go()和闭合函数back()

go()函数

1         //使扇形打开
2 function go(){
3 for(var i = 0;i<divs.length;i++){
4 //"rotate("+345-(i*15)+"deg)"
5 //不行,因为这样会先算"rotate("+345
6 divs[i].style.transform = "rotate("+(342-(i*15))+"deg)";
7 }
8 }

我们要让每个div都按一定的角度分开不至于重叠,用(i*15)可以实现这个小目标。342是定义第一个div的位置。

back()函数

1         //是扇形合并
2 function back(){
3 for(var i=0;i<divs.length;i++){
4 divs[i].style.transform = "rotate(0deg)";
5 }
6 }
7 }

第三步

当点击最后一个div时,扇形收起或者打开

        divs[9].onclick = function(){
if(jilu){
back();
}
else{
go();
}
jilu = !jilu; }

jilu这个变量的初始值是true,用jilu这个变量是为了当点击最后一个div时,通过判断jilu的值,扇形可以收起或者打开

第四步

当点击每个div时(除了最后一个div),被点击的div旋转到270度,并且在他左右边的div距离他的位置加大。

 1         //为每个div添加点击事件
2 for(var i=0;i<divs.length - 1;i++){
3 divs[i].index = i;
4 divs[i].onclick = function(){
5 for(var i = 0;i < divs.length;i++){
6 //当第i个div被点击,其他div的角度
7 if(this.index > i){
8 divs[i].style.transform = "rotate("+(342-(i*15) + this.index*15-72+10)+"deg)";
9 }
10 if( this.index < i){
11 divs[i].style.transform = "rotate("+(342-(i*15) + this.index*15-72-10)+"deg)";
12 }
13 }
14 //被点击div的角度
15 divs[this.index].style.transform = "rotate("+(342-(this.index*15) + this.index*15-72)+"deg)";
16
17 }
18 }

我们可以先看被点击div的角度,此时(342-(this.index*15) + this.index*15-72)等于270度,那我们将其他的div的角度都加10度,就可以了。

这个效果还是挺简单的,角度的计算那里有点绕,自己用浏览器看看每个div的角度,就会理解为什么这么写了。

源码:

  1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <title>Document</title>
6 <style>
7 *{
8 margin: 0;
9 padding: 0;
10 }
11 body{
12 background: #666;
13 }
14 #box{
15 width: 1200px;
16 height: 700px;
17 background: url(img/bg.png) no-repeat;
18 position: relative;
19 margin: 100px auto;
20 }
21 #box div{
22 width: 400px;
23 height: 100px;
24 border-radius: 9px;
25 position: absolute;
26 left: 555px;
27 top: 444px;
28 /*
29
30 */
31 background: url(img/logo.png) no-repeat 280px 56px;
32 transform-origin: 35px 50px;
33 transition: 1s;
34 }
35 #box div:nth-of-type(1){
36 padding: 0px; line-height: 1.5 !important;">;
37 }
38 #box div:nth-of-type(2){
39 background-color: #ce8298;
40 }
41 #box div:nth-of-type(3){
42 background-color: #f78992;
43 }
44 #box div:nth-of-type(4){
45 background-color: #fbc0a5;
46 }
47 #box div:nth-of-type(5){
48 background-color: #f8988f;
49 }
50 #box div:nth-of-type(6){
51 background-color: #f16273;
52 }
53 #box div:nth-of-type(7){
54 background-color: #9c9fcd;
55 }
56 #box div:nth-of-type(8){
57 background-color: #bae2d3;
58 }
59 #box div:nth-of-type(9){
60 background-color: #43bab8;
61 }
62 #box div:nth-of-type(10){
63 background-color: #678d95;
64
65 </style>
66 </head>
67 <body>
68 <div id="box">
69 <div></div>
70 <div></div>
71 <div></div>
72 <div></div>
73 <div></div>
74 <div></div>
75 <div></div>
76 <div></div>
77 <div></div>
78 <div></div>
79 </div>
80 <script>
81 window.onload = function(){
82 var box = document.getElementById('box');
83 var divs = box.getElementsByTagName('div');
84 var jilu = true;
85
86 setTimeout(function(){
87 go();
88 },1000);
89
90 //8 222 270 +48
91 //7 237 270 +33
92 //6 252 270 +18
93 // 15
94
95 //为每个div添加点击事件
96 for(var i=0;i<divs.length - 1;i++){
97 divs[i].index = i;
98 divs[i].onclick = function(){
99 for(var i = 0;i < divs.length;i++){
100 //当第i个div被点击,其他div的角度
101 if(this.index > i){
102 divs[i].style.transform = "rotate("+(342-(i*15) + this.index*15-72+10)+"deg)";
103 }
104 if( this.index < i){
105 divs[i].style.transform = "rotate("+(342-(i*15) + this.index*15-72-10)+"deg)";
106 }
107 }
108 //被点击div的角度
109 divs[this.index].style.transform = "rotate("+(342-(this.index*15) + this.index*15-72)+"deg)";
110
111 }
112 }
113
114 //点击最后一个图片,合并扇形
115 divs[9].onclick = function(){
116 if(jilu){
117 back();
118 }
119 else{
120 go();
121 }
122 jilu = !jilu;
123
124 }
125
126 //使扇形打开
127 function go(){
128 for(var i = 0;i<divs.length;i++){
129 //"rotate("+345-(i*15)+"deg)"
130 //不行,因为这样会先算"rotate("+345
131 divs[i].style.transform = "rotate("+(342-(i*15))+"deg)";
132 }
133 }
134 //是扇形合并
135 function back(){
136 for(var i=0;i<divs.length;i++){
137 divs[i].style.transform = "rotate(0deg)";
138 }
139 }
140 }
141
142
143 </script>
144 </body>
145 </html>

CSS扇形展开效果的更多相关文章

  1. 实用js+css多级树形展开效果导航菜单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. CSS - toggle collapse 类似bootstrap的展开效果

    问题:toggle collapse 类似bootstrap的展开效果(展开一个关闭另一个) Demo:http://jsfiddle.net/JSDavi/L47vscw4/ 方案:使用transi ...

  3. 利用target的特性,可以实现纯css的tab效果切换

    基础知识: :target起作用的是href连接到的位置 如 <a href="#tab1">tab1</a> <div id="tab1& ...

  4. 一款由jQuery实现的手风琴式相册图片展开效果

    之前我们有分享过很多jQuery手风琴样式的菜单,比如CSS3手风琴下拉菜单.今天要分享的jQuery手风琴效果很特别,它是手风琴样式的相册图片展开效果.我们只需点击图片缩略图即可展开当前的图片,并将 ...

  5. css三级菜单效果

    一个简单实用的css三级菜单效果 <!doctype html> <html> <head> <meta charset="utf-8"& ...

  6. 推荐20款基于 jQuery & CSS 的文本效果插件

    jQuery 和 CSS 可以说是设计和开发行业的一次革命.这一切如此简单,快捷的一站式服务.jQuery 允许你在你的网页中添加一些真正令人惊叹的东西而不用付出很大的努力,要感谢那些优秀的 jQue ...

  7. CSS 边框 阴影 效果

    CSS 边框 阴影 效果 以下将css实现阴影效果,以便须要朋友们,直接上代码 #shadow1{ width: 200px; height: 100px; color: white; backgro ...

  8. Css实现透明效果,兼容IE8

    Css实现透明效果,兼容IE8 >>>>>>>>>>>>>>>>>>>>> ...

  9. HTML与CSS简单页面效果实例

    本篇博客实现一个HTML与CSS简单页面效果实例 index.html <!DOCTYPE html> <html> <head> <meta charset ...

随机推荐

  1. eclipse tomcat maven热部署

    1.  tomcat插件 如果是Kepler的话,已经自带了tomcat插件,如果没有,到http://www.eclipsetotale.com/tomcatPlugin.html下载安装或在线安装 ...

  2. E. Santa Claus and Tangerines 二分答案 + 记忆化搜索

    http://codeforces.com/contest/752/problem/E 首先有一个东西就是,如果我要检测5,那么14我们认为它能产生2个5. 14 = 7 + 7.但是按照平均分的话, ...

  3. C#版SQLHelper.cs类

    using System; using System.Data; using System.Xml; using System.Data.SqlClient; using System.Collect ...

  4. js判断用户是否禁用了cookie

    function CookieEnable() { var result = false; if (navigator.cookiesEnabled) return true; document.co ...

  5. quick-cocos2d-x之testlua之mainMenu.lua

    require "helper" require "testResource" require "ActionsTest.ActionsTest&qu ...

  6. PHP 输出缓冲控制(Output Control) 学习

    php 缓冲简介 其实我对php ob 系列印象还是很模糊,具体怎么玩的,还不是很了解,平时curd,确实对这些内容没有深入.作为phper 甚是惭愧.网上搜了一通,互相copy,代码运行不能出现作者 ...

  7. win10下vs2015配置Opencv3.1.0过程详解

    下载安装Opencv3.1.0 下载Opencv3.1.0,进入官网,点击opencv for windows即可下载.  点击运行下载好的文件.实际上,opencv的安装程序就是解压缩文件,个人因为 ...

  8. Android入门

    在学Android,摘自<第一行代码——Android> 布局管理 通过xml文件进行布局管理. android:id="@+id/button_1" 为当前的元素定义 ...

  9. 【动态规划】bzoj1669 [Usaco2006 Oct]Hungry Cows饥饿的奶牛

    #include<cstdio> #include<algorithm> using namespace std; int n,a[5001],b[5001],en; int ...

  10. [SmartFoxServer概述]Zones和Rooms结构

    Zones和Rooms结构: 相对于SFS 1.X而言,在Zones和Rooms的配置上,SFS2X有了显著的改善.尤其是我们建立了房组这样一个简单的概念,它允许在一个逻辑组中管理Rooms,从而独立 ...