CSS3实现各种图形的示例代码


Posted in HTML / CSS onOctober 19, 2016

1、自适应的椭圆

复制代码
代码如下:

<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></p> <p>

CSS3实现各种图形的示例代码

运动的椭圆

2、半椭圆

复制代码
代码如下:

<div class="div2"></div>
<div class="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就可以,不过我用了两个四十五度的,自己玩。

CSS3实现各种图形的示例代码

半椭圆

3、平行四边形

复制代码
代码如下:

<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>

CSS3实现各种图形的示例代码

平行四边形

4、平行四边形

复制代码
代码如下:

<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>

CSS3实现各种图形的示例代码

平行四边形

5、切角矩形

复制代码
代码如下:

<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>

CSS3实现各种图形的示例代码

切角矩形

6、凹角矩形

复制代码
代码如下:

<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>

CSS3实现各种图形的示例代码

凹角矩形

7、切角矩形(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="<a href="http://www.w3.org/2000/svg">http://www.w3.org/2000/svg</a>" 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>

CSS3实现各种图形的示例代码

切角矩形

8、梯形

复制代码
代码如下:

<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>

CSS3实现各种图形的示例代码

梯形

9、梯形

复制代码
代码如下:

<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>

CSS3实现各种图形的示例代码

梯形

10、梯形

复制代码
代码如下:

<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>

CSS3实现各种图形的示例代码

梯形

11、div饼图

复制代码
代码如下:

<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>

CSS3实现各种图形的示例代码

饼图

12、饼图

复制代码
代码如下:

<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>

CSS3实现各种图形的示例代码

饼图

13、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 = "<a href="http://www.w3.org/2000/svg">http://www.w3.org/2000/svg</a>";
var svg = document.createElementNS(NS, "svg");
var circle =document.createElementNS(NS, "circle");
var title = document.createElementNS(NS, "title");</p> <p> 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>

CSS3实现各种图形的示例代码

js绘制饼图

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

总结
以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用Css3能有所帮助,如果有疑问大家可以留言交流。

HTML / CSS 相关文章推荐
css3实现背景模糊的三种方式
Mar 09 HTML / CSS
深入理解css中vertical-align属性
Apr 18 HTML / CSS
css3学习系列之移动属性详解
Jul 04 HTML / CSS
css3实现小箭头各种图形效果
Jul 08 HTML / CSS
HTML5打开本地app应用的方法
Mar 31 HTML / CSS
html5 localStorage本地存储_动力节点Java学院整理
Jul 06 HTML / CSS
HTML5上传文件显示进度的实现代码
Aug 30 HTML / CSS
多视角3D可旋转的HTML5 Logo动画
Mar 02 HTML / CSS
canvas实现二维码和图片合成的示例代码
Aug 01 HTML / CSS
基于Html5 canvas实现裁剪图片和马赛克功能及又拍云上传图片 功能
Jul 09 HTML / CSS
利用 Canvas实现绘画一个未闭合的带进度条的圆环
Jul 26 HTML / CSS
Canvas图片分割效果的实现
Jul 29 HTML / CSS
利用CSS3实现平移动画效果示例代码
Oct 12 #HTML / CSS
CSS3模拟IOS滑动开关效果
Sep 28 #HTML / CSS
CSS3打造磨砂玻璃背景效果
Sep 28 #HTML / CSS
利用CSS3实现折角效果实例源码
Sep 28 #HTML / CSS
CSS3 animation实现简易幻灯片轮播特效
Sep 27 #HTML / CSS
CSS3实现复选框动画特效示例代码
Sep 27 #HTML / CSS
利用CSS3实现单选框动画特效示例代码
Sep 26 #HTML / CSS
You might like
古巴咖啡 Cubita琥爵咖啡 独特的加勒比海风味咖啡
2021/03/06 新手入门
php 缩略图实现函数代码
2011/06/23 PHP
谨慎使用PHP的引用原因分析
2012/09/06 PHP
CI框架Session.php源码分析
2014/11/03 PHP
php多线程并发实现方法
2016/09/30 PHP
PHP获取文本框、密码域、按钮的值实例代码
2017/04/19 PHP
JS命名空间的另一种实现
2013/08/09 Javascript
jQuery 隐藏和显示 input 默认值示例
2014/06/03 Javascript
基于jquery和svg实现超炫酷的动画特效
2014/12/09 Javascript
jquery自定义表单验证插件
2016/10/12 Javascript
Html中 IFrame的用法及注意点
2016/12/22 Javascript
node.js的事件机制
2017/02/08 Javascript
node使用UEditor富文本编辑器的方法实例
2017/07/11 Javascript
Angular项目从新建、打包到nginx部署全过程记录
2017/12/09 Javascript
小程序开发中如何使用async-await并封装公共异步请求的方法
2019/01/20 Javascript
vue子路由跳转实现tab选项卡
2019/07/24 Javascript
使用axios请求接口,几种content-type的区别详解
2019/10/29 Javascript
jQuery操作动画完整实例分析
2020/01/10 jQuery
[01:50]2014DOTA2西雅图邀请赛 专访欢乐周宝龙
2014/07/08 DOTA
[02:00]DAC2018主宣传片——龙征四海,剑问东方
2018/03/20 DOTA
Python的Flask框架中Flask-Admin库的简单入门指引
2015/04/07 Python
在Python中用split()方法分割字符串的使用介绍
2015/05/20 Python
NetworkX之Prim算法(实例讲解)
2017/12/22 Python
python新手学习可变和不可变对象
2020/06/11 Python
Python 实现集合Set的示例
2020/12/21 Python
男女时尚与复古风格在线购物:RoseGal(全球免费送货)
2017/07/19 全球购物
网站域名和主机:Domain.com
2019/04/01 全球购物
英国户外服装品牌:Craghoppers
2019/04/25 全球购物
《桂花雨》教学反思
2014/04/12 职场文书
体育系毕业生求职自荐信
2014/04/16 职场文书
街道党工委党的群众路线教育实践活动对照检查材料思想汇报
2014/10/05 职场文书
2014年工商所工作总结
2014/12/09 职场文书
家装电话营销开场白
2015/05/29 职场文书
汉字听写大会观后感
2015/06/12 职场文书
JavaScript实现简单图片切换
2021/04/29 Javascript
idea下配置tomcat避坑详解
2022/04/12 Servers