CSS3实战 - 图形(圆形、半圆、四边形、菱形、梯形、饼图)

自适应的椭圆

运动的椭圆
<div class="div1"> 鼠标划上来看看</div>
<style>
 .div1 {
   background:#fb3;
   width:200px;
   height:200px;
   line-height:200px;
   text-align: center;
   border-radius: 50%;
   transition: all 0.25s ease-in; 
 }
 .div1:hover {
   background: #ff1d50;
   width:400px;
   height:200px;
   border-radius: 50%;
   -webkit-transition: all 0.2s ease-in;
   transition: all 0.2s ease-in;
 }
 /* ->  首先你得知道,border-radius不仅仅支持整数,还支持百分比    */
</style>

半椭圆

半椭圆

本来一个div就可以,不过我用了两个四十五度的,自己玩。

<div class="div2">div2</div>
<div class="div3">div3</div>
<style>
  .div2 {
    background: #fb3;
    width: 100px;
    height: 100px;
    border-radius: 100%  0 0 0;
    transition: all 0.25s ease-in;
  }
  .div3{
    background: #fb3;
    width: 100px;
    height: 100px;
    border-radius: 0  0 0 100%;
    transition: all 0.25s ease-in;
  }
 /* ->  首先你得知道,border-radius不仅仅支持整数,还支持百分比    */
</style>

平行四边形

平行四边形
<div class="div4">首先</div>
<style>
  .div4{
    position: relative;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
    color: #fff;
  }
  .div4::before {
    content: '';   /* 用伪元素来生成一个矩形 */
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: -1;   /* 为了避免覆盖主元素 */
    background: #58a;
    transform: skew(15deg);   /* 设置倾斜角度 */
  }
</style>

菱形

美腿
<div class="div5">
    <img src="../w658.jpg"/>
</div>
<style>
 .div5{
    width: 100px;
    height: 100px;
    transform: rotate(45deg);
    overflow: hidden;
 }
 .div5 >img {
    max-width: 100%;
    transform: rotate(-45deg)scale(1.42);
 }
</style>

切角矩形

切角矩形
<div class="div6"></div>
<style>
 .div6{
  width: 200px;
  height: 100px;
  background: #58a;
  background:
   linear-gradient(-45deg,transparent 15px, #58a 0)   right,
   linear-gradient(45deg,transparent 15px, #655 0)   left;
  background-size: 50% 100%;
  background-repeat: no-repeat;
 }
</style>

-> 凹角矩形

凹角矩形
<div class="div7"></div>
<style>
 .div7{
  width:240px;
  height:50px;
  background:#58a;
  background:
   radial-gradient(circle at top left, transparent 8px, #58a 0) top left,
   radial-gradient(circle at top right, transparent 8px, #58a 0) top right,
   radial-gradient(circle at bottom right, transparent 8px, #58a 0) bottom right,
   radial-gradient(circle at bottom left, transparent 8px, #58a 0) bottom left;
  background-size: 50% 50%;
  background-repeat: no-repeat;
 }
</style>

-> 切角矩形(SVG)

<div class="div8"></div>
<style>
 .div8{
  width:240px;
  height:50px;
  background:#58a;
  border:15px solid #58a;/*  当切角存在时,则显示切角,不支持时,则显示边框  */
  border-image:1 url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="3" height="3" fill="%2358a">\
   <polygon points="0,1 1,0 2,0 3,1 3,2 2,3 1,3 0,2"/></svg>');
  background-clip:padding-box; }
</style>
切角矩形

梯形1

梯形
<div class="div9"></div>
<style>
 .div9{
    width: 240px;
    height: 40px;
    position: relative;
    color: white;
 }
 .div9:before{
    content: ''; /* 用伪元素来生成一个矩形*/
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: -1;
    background: #58a;
    transform: perspective(.5em) rotateX(5deg);
 }
</style>

梯形2

梯形
<div class="div10"></div>
<div class="div10"></div>
<div class="div10"></div>
<div class="div10"></div>
<style>
 .div10{
    float: left;
    width: 60px;
    height: 40px;
    position: relative;
    padding: .3em 1em 0;
 }
 .div10:before{
    content: '';
    position: absolute;
    width: 110px;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: -1;
    background: #ccc;
    background-image:linear-gradient(hsla(0, 0%, 100%, .6), hsla(0, 0%, 100%, 0));
    border: 1px solid rgba(0,0,0,.4);
    border-bottom: none;
    border-radius: .5em .5em 0 0;
    box-shadow: 0 .15em white inset;
    transform: perspective(.5em) rotateX(5deg);
    transform-origin: bottom;
 }
</style>

梯形3

梯形
<div class="div11"></div>
<div class="div11"></div>
<div class="div11"></div>
<div class="div11"></div>
<style> .div11{
  float:left;
  width:60px;
  height:40px;
  position:relative;
  padding:.3em 1em 0;
 }
 .div11:before{
  content: '';
  position: absolute;
  width:110px;
  top:0;right:0;
  bottom:0;left:0;
  z-index:-1;
  background:#ccc;
  background-image:linear-gradient(hsla(0,0%,100%,.6), hsla(0,0%,100%,0));
  border:1px solid rgba(0,0,0,.4);
  border-bottom:none;
  border-radius:.5em .5em 0 0;
  box-shadow:0 .15em white inset;
  transform:perspective(.5em) rotateX(4deg);
  transform-origin:bottom left;
 }
</style>

div + css 饼图

饼图
<div class="div12">
 <div></div>
 <div><div></div></div>
 <div></div> <div></div>
</div>
<style>
 .div12{
  float:left;
  width:100px;
  height:100px;
  border-radius: 50%;
  background: #655;
  overflow:hidden;  position:relative;
 }
 .div12:after{
  content:'';
  display:table;
  clear:both;
  position:absolute;
  top:0;bottom:0;left:0;right:0;
 }
 .div12 div{
  position:absolute;
  float:left;
  width:100px;
  height:100px;
  border-radius: 50%;
 }
 .div12>div:first-child{
  background-image:
   linear-gradient(to right,transparent 50%, rgba(198, 179, 129, 0.94) 0); }
 .div12>div:first-child:before{
  content: '';
  display:block;
  margin-left:50%;
  height:100%;
  border-radius:0 100% 100% 0 /50%;
  background-color:#655;
  transform-origin:left;
  transform: rotate(50deg);
 }
 .div12>div:nth-child(2){
  background-image:
   linear-gradient(140deg,transparent 50%, rgba(35, 198, 41, 0.94) 0);
 }
 .div12>div:nth-child(2):before{
  content: '';
  display:block;
  margin-left:50%;
  height:100%;
  border-radius:0 100% 100% 0 /50%;
  background-color:#655;
  transform-origin:left;
  transform: rotate(145deg);
 }
</style>

svg 饼图

饼图
<svg width="100" height="100">
 <circle r="25" cx="50" cy="50" />
</svg>
<style>
 @keyframes fillup {  to { stroke-dasharray: 100 100; } }
 circle {
  fill:yellowgreen;
  stroke:#655;
  stroke-width: 50;
  stroke-dasharray:38 100; /* 可得到比率为38%的扇区*/
 }
 svg{
  width:100px;height:100px;
  transform:rotate(-90deg);
  background:yellowgreen;
  border-radius:50%;
 }
</style>

js绘制饼图

js绘制饼图
<div class="pie1">20%</div>
<div class="pie2">60%</div>
<script>
 function $$(selector,context) {
  context =context || document;
  var elements =context.querySelectorAll(selector);
  return Array.prototype.slice.call(elements);
 }
 $$('.pie1').forEach(function(pie) {
  var p = parseFloat(pie.textContent);
  var NS = "http://www.w3.org/2000/svg";
  var svg = document.createElementNS(NS, "svg");
  var circle =document.createElementNS(NS, "circle");
  var title = document.createElementNS(NS, "title");

  circle.setAttribute("r", 25);  circle.setAttribute("cx",16);
  circle.setAttribute("cy",16);  circle.setAttribute("stroke-dasharray",p + 100);
  console.log(p + 100);
  svg.setAttribute("viewBox", "0 0 32 32");
  title.textContent = pie.textContent;
  pie.textContent = '';
  svg.appendChild(title);
  svg.appendChild(circle);
  pie.appendChild(svg); });
</script>
<style>
 @keyframes fillup {  to { stroke-dasharray: 100 100; } }
 .pie1 circle {
  fill:yellowgreen;
  stroke:#655;
  stroke-width: 50;
  stroke-dasharray:70 160; /* 可得到比率为38%的扇区*/
 }
 .pie1 svg{
  width:100px;height:100px;
  transform:rotate(-90deg);
  background:yellowgreen;
  border-radius:50%;
 }
</style>

备注:这里的大多数例子,都是从LEA VEROU的《css揭秘》这本书上借鉴来的,感兴趣的童鞋不如去买这本书,简直是CSS神书,就和Js的蝴蝶一样,前端必备。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,458评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,454评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,171评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,062评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,440评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,661评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,906评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,609评论 0 200
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,379评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,600评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,085评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,409评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,072评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,088评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,860评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,704评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,608评论 2 270

推荐阅读更多精彩内容

  • 在制作页面时,常用CSS画各种形状带代替img,这样可以免去一次HTTP请求。而且有些基本形状用CSS实现比切图更...
    张歆琳阅读 2,403评论 1 19
  • 1、一元一次方程根的情况 △=b2-4ac 当△>0时,一元二次方程有2个不相等的实数根; 当△=0时,一元二次方...
    abbatuu阅读 3,460评论 1 21
  • 文/向光的小虫 “子欲养而亲不待,树欲静而风不止”这句蕴含孝义的句子在当下被很多儿女子孙却用来做了挡箭牌,我自己...
    向光的小虫阅读 274评论 2 0
  • 有月华的夜晚 总使人不能入睡 因为欣喜 有月影的夜晚 微风拂动着 吹乱了长发 不知道该怎样喜欢这样的夜晚 不怕这孤...
    悬崖上的一株草阅读 126评论 0 5
  • 你会时常后悔曾经做过的决定吗? 曾经你做过的每一个决定才成就了今天的你,你曾经积累起来的每一个选择把你带到今天的地...
    陆湘儿阅读 532评论 0 2