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美化表单控件全集
Jun 29 HTML / CSS
CSS3动画之流彩文字效果+图片模糊效果+边框伸展效果实现代码合集
Aug 18 HTML / CSS
CSS3样式linear-gradient的使用实例
Jan 16 HTML / CSS
css3打造一款漂亮的卡哇伊按钮
Mar 20 HTML / CSS
基于HTML5 Canvas 实现弹出框效果
Jun 05 HTML / CSS
html5 canvas-2.用canvas制作一个猜字母的小游戏
Jan 07 HTML / CSS
HTML5的一个显示电池状态的API简介
Jun 18 HTML / CSS
canvas绘制圆角头像的实现方法
Jan 17 HTML / CSS
HTML5 video进入全屏和退出全屏的实现方法
Jul 28 HTML / CSS
html5跳转小程序wx-open-launch-weapp踩坑
Dec 02 HTML / CSS
如何利用 CSS Overview 面板重构优化你的网站
Oct 24 HTML / CSS
CSS实现渐变色边框(Gradient borders)的5种方法
Mar 25 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
php获取访问者浏览页面的浏览器类型
2017/01/23 PHP
nodejs分页类代码分享
2014/06/17 NodeJs
详解AngularJS中的依赖注入机制
2015/06/17 Javascript
JavaScript操作表单实例讲解(上)
2016/06/20 Javascript
封装获取dom元素的简单实例
2016/07/08 Javascript
ES6中的数组扩展方法
2016/08/26 Javascript
JS实现禁止鼠标右键的功能
2016/10/15 Javascript
jQuery UI制作选项卡(tabs)
2016/12/13 Javascript
Nodejs多站点切换Htpps协议详解及简单实例
2017/02/23 NodeJs
jQuery插件FusionCharts绘制的3D环饼图效果示例【附demo源码】
2017/04/02 jQuery
AngularJS路由Ui-router模块用法示例
2017/05/29 Javascript
JS失效 提示HTML1114: (UNICODE 字节顺序标记)的代码页 utf-8 覆盖(META 标记)的冲突的代码页 utf-8
2017/06/23 Javascript
Vue中的scoped实现原理及穿透方法
2018/05/15 Javascript
详解JavaScript中的强制类型转换
2019/04/15 Javascript
layui 选择列表,打勾,点击确定返回数据的例子
2019/09/02 Javascript
js实现固定区域内的不重叠随机圆
2019/10/24 Javascript
浅谈关于vue中scss公用的解决方案
2019/12/02 Javascript
vue el-table实现行内编辑功能
2019/12/11 Javascript
在vue中使用jsonp进行跨域请求接口操作
2020/10/29 Javascript
[46:04]Liquid vs VP Supermajor决赛 BO 第四场 6.10
2018/07/05 DOTA
Python中的面向对象编程详解(上)
2015/04/13 Python
Python编程中time模块的一些关键用法解析
2016/01/19 Python
Apache如何部署django项目
2017/05/21 Python
用python标准库difflib比较两份文件的异同详解
2018/11/16 Python
python并发编程多进程 模拟抢票实现过程
2019/08/20 Python
Python实现密码薄文件读写操作
2019/12/16 Python
python-OpenCV 实现将数组转换成灰度图和彩图
2020/01/09 Python
Python实现仿射密码的思路详解
2020/04/23 Python
Python flask路由间传递变量实例详解
2020/06/03 Python
python 解决pycharm运行py文件只有unittest选项的问题
2020/09/01 Python
创先争优公开承诺书
2014/08/30 职场文书
工作作风建设心得体会
2014/10/22 职场文书
项目投资意向书范本
2015/05/09 职场文书
人事行政部各岗位职责说明书!
2019/07/15 职场文书
送给自己的励志语句:要安静的优秀,悄无声息的坚强
2019/11/26 职场文书
利用Java连接Hadoop进行编程
2022/06/28 Java/Android