HTML+CSS画一朵向日葵
前几天看到一张图片,倔强的向日葵。(BGM,《倔强》)
看着挺有感触,就想用CSS做一个向日葵。
最终效果图如下:
演示地址:
http://suohb.com/work/sunflower.html
主要的难点就在花瓣的处理上,css暂时没有做到这样的尖角圆弧。
我想到的做法是用两个椭圆的部分弧度截取出来,拼接成一个花瓣样式。
原理如下:
CSS部分
.petal{
position:absolute;
width:50px;
height:130px;
transform-origin:50% 150%;
opacity:.9;
}
.petal > div:nth-child(1){
position:absolute;
left:;
top:;
width:50%;
height:100%;
overflow:hidden;
}
.petal > div:nth-child(1) > div{
position:absolute;
left:;
top:-20px;
width:160px;
height:250px;
background:#F00;
border-radius:50%;
box-shadow: 0 0 5px #000;
}
.petal > div:nth-child(2){
position:absolute;
left:50%;
top:;
width:50%;
height:100%;
overflow:hidden;
}
.petal > div:nth-child(2) > div{
position:absolute;
right:;
top:-20px;
width:160px;
height:250px;
background:#F00;
border-radius:50%;
border-radius:50%;
box-shadow: 0 0 5px #000;
}
HTML部分:
<div class="petal">
<div>
<div></div>
</div>
<div>
<div></div>
</div>
</div>
这样就画出一个花瓣,
然后我们将花瓣使用js创建出来,新增一个花瓣外层div#box;将生成的花瓣插入里边。花瓣的的transform-origin属性为正下方一段距离。复制并旋转
代码如下:
function addPetal(){
var len = 21 ,
i = 0 ,
scale = 1 ,
var fragment = document.createDocumentFragment();
for(i = 0 ; i < len ; i ++){
fragment.appendChild(getOnePetal(scale,Math.round(360/len*i)));
}
box.appendChild(fragment);
}
function getOnePetal(size,deg){
var div = document.createElement("div");
div.className = "petal" ;
div.innerHTML = "<div><div></div></div><div><div></div></div>";
div.style.left = 155 + "px";
div.style.top = 0 ;
div.style.transform = "rotate("+deg+"deg) scale("+size+")";
return div ;
}
现在基本上已经看出向日葵的轮廓,我们将花瓣多复制几层,越向内层越小。给花瓣加点阴影有写层次感。
到这里就完成一大半了,然后做向日葵中心部分,画一个渐变色圆,给他加一些线条。
先在向日葵中心圆上部话一般圆形div,只要border。设置tranform-origin到向日葵的中心位置。复制这个圆并旋转。得到下图:
这也是一个很有意思的图形。给中心圆加上overflow:hidden;放在向日葵中心
做到这里主要难点都已经做完了。接下来就是把花瓣主色调改成黄色渐变,花瓣角度做一点随机处理,中心加一些花蕊,就得到了一颗向日葵。
更多特效请关注这个微信公众号
最终完整代码:
<!doctype html>
<html>
<head>
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Cache-Control" content="no-cache" />
<meta http-equiv="Expires" content="0" />
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no" />
<style type="text/css">
.petal{
position:absolute;
width:50px;
height:130px;
transform-origin:50% 150%;
opacity:.9;
}
.petal > div:nth-child(1){
position:absolute;
left:0;
top:0;
width:50%;
height:100%;
overflow:hidden;
}
.petal > div:nth-child(1) > div{
position:absolute;
left:0;
top:-20px;
width:160px;
height:250px;
background-image:linear-gradient(95deg,#fef10c 0%,#ffc701 8%,#fef10c 15%,#ffc701 20%);
box-shadow:0 0 10px rgba(0,0,0,.3);
border-radius:50%;
}
.petal > div:nth-child(2){
position:absolute;
left:50%;
top:0;
width:50%;
height:100%;
overflow:hidden;
}
.petal > div:nth-child(2) > div{
position:absolute;
right:0;
top:-20px;
width:160px;
height:250px;
background-image:linear-gradient(-95deg,#fef10c 0%,#ffc701 8%,#fef10c 15%,#ffc701 20%);
box-shadow:0 0 10px rgba(0,0,0,.3);
border-radius:50%;
}
#box{
width:370px;
height:370px;
margin: 0 auto;
position: relative;
}
.pistilShadow{
position:absolute;
left: 180px;
top:195px;
width:120px;
height:120px;
border-radius:50%;
transform:translate(-50%,-50%);
box-shadow: -5px 5px 80px #bd3d0e;
}
.pistil{
position:absolute;
left: 180px;
top:195px;
width:160px;
height:160px;
border-radius:50%;
transform:translate(-50%,-50%);
box-shadow: 0 0 80px #bd3d0e inset;
background:#325302;
overflow:hidden;
}
.pistilLine{
position:absolute;
left:20%;
top:-50%;
transform-origin: center bottom;
width:60%;
height:100%;
border-radius:50%;
border:solid 1px #2f1307;
}
.pistil2{
position:absolute;
left: 180px;
top:195px;
width:60px;
height:60px;
border-radius:50%;
transform:translate(-50%,-50%);
box-shadow: 0 0 25px #bd3d0e inset;
background:#325302;
}
.dot{
position:absolute;
left:28px;
top:0;
width:4px;
height:4px;
border-radius:50%;
background:#fded00;
box-shadow: 0 0 15px #fded00 inset;
transform-origin:center 30px;
}
</style>
</head>
<body>
<div id="box"></div>
<script> function addPetal(){
var len = 21 ,
layer = 3 ,
i = 0 ,
j = 0 ,
changeScale = 0.1 ,
scale = 1 ,
div;
var fragment = document.createDocumentFragment();
var pistil = document.createElement("div");
pistil.className = "pistil" ;
var pistil2 = document.createElement("div");
pistil2.className = "pistil2" ; for(j = 0 ; j < layer ; j ++){
for(i = 0 ; i < len ; i ++){
fragment.appendChild(getOnePetal(scale,Math.round(360/len*i + Math.random()*10)));
}
div = document.createElement("div");
div.className = "pistilShadow" ;
fragment.appendChild(div);
len -= 5 ;
scale -= changeScale ;
changeScale += changeScale ;
}
len = 40 ;
for(var i = 0 ;i < len ; i ++){
div = document.createElement("div");
div.className = "pistilLine" ;
div.style.transform = "rotate("+Math.round(360/len*i)+"deg)" ;
pistil.appendChild(div);
}
len = 30;
scale = 1 ;
changeScale = 0.15 ;
for(j = 0 ; j < layer ; j ++){
for(i = 0 ; i < len ; i ++){
pistil2.appendChild(getOneDot(scale,Math.round(360/len*i+j*10)));
}
len -= 4 ;
scale -= changeScale ;
}
fragment.appendChild(pistil);
fragment.appendChild(pistil2); box.appendChild(fragment);
}
function getOnePetal(size,deg){
var div = document.createElement("div");
div.className = "petal" ;
div.innerHTML = "<div><div></div></div><div><div></div></div>";
div.style.left = 155 + "px";
div.style.top = 0 ;
div.style.transform = "rotate("+deg+"deg) scale("+size+")";
return div ;
}
function getOneDot(size,deg){
var div = document.createElement("div");
div.className = "dot" ;
div.style.transform = "rotate("+deg+"deg) scale("+size+")";
return div ;
}
addPetal();
</script>
</body>
</html>
HTML+CSS画一朵向日葵的更多相关文章
- 一步一步教你用CSS画爱心
今天小颖给大家分享一个用CSS画的爱心,底下有代码和制作过程,希望对大家有所帮助. 第一步: 先画一个正方形.如图: <!DOCTYPE html> <html> <he ...
- 参考bootstrap中的popover.js的css画消息弹框
前段时间小颖的大学同学给小颖发了一张截图,图片类似下面这张图: 小颖当时大概的给她说了下,其实小颖也不知道上面那个三角形怎么画嘻嘻,给她说了DOM结构,具体的css让她自己百度,今天小颖自己参考boo ...
- Effective前端3:用CSS画一个三角形
p { text-indent: 2em } .triangle-container p { text-indent: 0 } img { margin: 15px 0 } 三角形的场景很常见,打开一 ...
- 用css画出三角形
看到有面试题里会有问到如何用css画出三角形 众所周知好多图形都可以拆分成三角形,所以说会了画三角形就可以画出很多有意思的形状 画出三角形的原理是调整border(边框)的四个方向的宽度,线条样式以及 ...
- 用纯css画个三角形
用纯css画个三角形以下是源代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...
- 用css画图标
css3的属性 transform(转换) 用途很广泛,功能也很强大,为了熟悉它的各种转换方式(平移 translate,旋转 rotate,扭曲 skew,放缩 scale),我做了一些平常常用的一 ...
- 如何用css画出三角形
看到有面试题里会有问到如何用css画出三角形 众所周知好多图形都可以拆分成三角形,所以说会了画三角形就可以画出很多有意思的形状 画出三角形的原理是调整border(边框)的四个方向的宽度,线条样式以及 ...
- 用css画出三角形【转】
看到有面试题里会有问到如何用css画出三角形 众所周知好多图形都可以拆分成三角形,所以说会了画三角形就可以画出很多有意思的形状 画出三角形的原理是调整border(边框)的四个方向的宽度,线条样式以及 ...
- CSS画三角形引发的一些思考
今天刷知乎时看到了一个问题,有谁能详细讲一下css如何画出一个三角形?怎么想都想不懂? - 知乎.很巧,刚入前端坑的我前不久也遇到过这个问题,今天再来谈一谈这个问题则是因为知乎的一些答案引发了我的 ...
随机推荐
- Hive笔记——技术点汇总
目录 · 概况 · 手工安装 · 引言 · 创建HDFS目录 · 创建元数据库 · 配置文件 · 测试 · 原理 · 架构 · 与关系型数据库对比 · API · WordCount · 命令 · 数 ...
- Jquery-全选和取消的一个坑
在做一个商城的购物车的时候遇到了一个坑, 购物车一般都有全选按钮, 再次点击就会全部消除, 在网上查到的答案全部都是使用attr来做的, 无一例外都不能用, 之后才知道要使用jquery的prop和r ...
- Protocol Buffers与FlatBuffers效率对比
Protocol Buffers是Google跨语言.跨平台的通用序列化库.FlatBuffers同样出自Google,而且也跨语言跨平台,但更强调效率,专门为游戏开发打造.在游戏界混了几年,各种各样 ...
- 使用Hibernate模板调用存储过程
前提是该Dao类已经已经继承了org.springframework.orm.hibernate5.support.HibernateDaoSupport,并且在整个项目中已经配置好了事务,或者是手动 ...
- Head First 设计模式 第3章 装饰者模式
第3章 装饰者模式 1.定义/说明 动态.透明的将职责附加到对象上(或从对象上撤销),而不影响其他对象.若要扩展功能,装饰者模式提供了比继承更富有弹性的替代方案. 2.介绍 首先让我们先来介绍一下场景 ...
- centos7用户,组及文件权限管理
centos7安装过程中如果没有创建用户的话,默认只有ROOT用户,这个用户是具有最高权限的帐户,可以做任何事情,但实际生产环境中我们一般不会使用这个用户,因为权限太大了,很危险. 所以在生产环境中就 ...
- The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (new View.OnClickListener(){}, String, int)
package comxunfang.button; import android.support.v7.app.ActionBarActivity; import android.os.Bundle ...
- com.mysql.jdbc.exceptions.MySQLSyntaxErrorException错误
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the ...
- semantic UI first web
官方文档:https://semantic-ui.com/introduction/getting-started.html semantic UI: SemanticUI是一款语义化设计的前端开源 ...
- TensorFlow学习笔记2——数据类型及简单运算
0. 小试牛刀 首先,激活tensorflow环境( source activate tensorflow ),随后在ipython里: import tensorflow as tf sess = ...