天介绍了前端系列:如何用CSS代码来写26个字母,前端福利,没有看过的可以看我之前的文章,或者打开下面的网址来浏览:http://www.toutiao.com/i6366084511695897089/
今天介绍的是,用css代码来写几何图形或者不规则的图形,当然看这些都需要一点点的div+css的基础,还有了解新的html5+css3的知识点,这样你才能更好的体会感触。
正方形代码
长方形代码
圆形代码
椭圆的代码
正三角形的代码
倒三角行的代码
左三角形代码
右三角形代码
梯形代码
爱心代码
太极代码
还有一些其他的代码,就不一一的发图了,有兴趣的可以打开下面的网站来看这些代码
https://css-tricks.com/examples/ShapesOfCSS/
馨客栈导航:http://www.mackxin.com/nav.html
馨客栈交流:http://www.mackxin.com/xininn.html
关注分享,体验乐趣
分享是一种态度
学无止境,学海无涯,好好学习,天天向上
近公司项目也挺忙的,所以很少时间来写文章。最近公司有个需求要实现带曲线的上升效果,我这边实现了安卓版本,今天特意用js也写了一份,就当时学习了。JS最终实现如下图,这个图片是不动的,打开是有动画的。
js曲线学习
大致就是这么一个效果,但是里面涉及的逻辑还是挺多的。因为有好几张UI效果图,目前我就贴出来一张,如果能实现这一张的效果基本其他的也容易搞定了,但是这里我就不分享Android的代码了,毕竟是公司项目,所以很多信息必透露,这里分享JS实现的代码。
原图
<html>
<head>
</head>
<body>
<canvas id="canvas" style="border:1px solid #aaa;display:block;margin:50px auto;">
当前浏览器不支持Canvas,请更换浏览器后再试
</canvas>
<script>
function l(){
this.canvas; // canvas节点
this.ctx; // canvas绘图上下文
this.x; // 横向坐标
this.y; // 纵向坐标
this.rx; //椭圆长轴
this.ry; //椭圆短轴
this.degree1;//初始角度
this.degree2;//结束角度
this.degreecur; //当前角度
};
l.prototype.init = function(){
var canvas = document.getElementById("canvas");
var context=canvas.getContext('2d');
canvas.width = 800;
canvas.height = 800;
this.canvas = canvas;
this.ctx = context;
this.degree1 = 20;
this.degree2 = 120;
this.degreecur = this.degree2;
this.x = 200;
this.y = 200;
this.rx = 150;
this.ry = 50;
this.increase = 1;
};
l.prototype.update = function(){
this.ctx.clearRect(0,0,this.canvas.width,this.canvas.height);
this.ctx.lineWidth = 2;
// 绘制弧线
this.drawLine('#000', this.degree1, this.degree2);
this.drawLine('#f00', this.degreecur, this.degree2);
this.drawCr();
this.degreecur -= this.increase;
if(this.degreecur < this.degree1){
this.degreecur = this.degree2;
}
};
l.prototype.drawCr = function(){
var o = this.degreecur * Math.PI/180;
var a = this.rx;
var b = this.ry;
var x = 0;
var y = 0;
x = a * Math.cos(o);
y = b * Math.sin(o);
x += this.x;
y += this.y;
this.ctx.beginPath();
this.ctx.strokeStyle = '#0f0';
this.ctx.fillStyle = '#0f0';
this.ctx.arc(x,y,5,0,2*Math.PI);
this.ctx.fill();
this.ctx.closePath();
};
l.prototype.drawLine = function(color, degreeStart, degreeEnd){
this.ctx.beginPath();
this.ctx.strokeStyle = color;
//起点x.起点y,半径x,半径y,旋转的角度,起始角,结果角,顺时针还是逆时针
this.ctx.ellipse(this.x,this.y,this.rx,this.ry,0,degreeStart*Math.PI/180,degreeEnd*Math.PI/180,false);
this.ctx.stroke();
this.ctx.closePath();
};
var l1 = new l();
// dot移动效果
function animateUpdate() {
l1.update(); // 更新dot的当前位置
setTimeout(animateUpdate, 25);
}
window.onload = function(){
l1.init();
animateUpdate()
}
</script>
</body>
</html>
如果需要验证的可以直接复制上面的代码运行就能看到效果。这里不做演示,本次分享就到此为止,谢谢大家了。如果喜欢就关注,点赞吧,谢谢了。
家好,我是一碗周,一个不想被喝(内卷)的前端。如果写的文章有幸可以得到你的青睐,万分有幸~
过年了~ 过年了~ 过年了~
辞旧迎新过年啦 张灯结彩春节啦~
金鸡起舞送福啦 新的一年福来啦~
文章开头几句歌词,顿时显得喜庆了不,我们的灯笼是下面这个样子的。
画灯笼我们肯定不能画一个静态的灯笼,我们先来复习一下CSS中的 animation 属性,该是是一个简写属性,由 animation-name , animation-duration , animation-timing-function , animation-delay , animation-iteration-count , animation-direction , animation-fill-mode 和 animation-play-state 属性组成。这里我们就不展开讲解了,具体可以跳转到MDN学习。
我们先来看一下下面这个示例:
animation: swing 3s infinite ease-in-out;
在上面的例子中使用了一个名为 swing 的动画序列,动画序列通过 @keyframes 创建,执行时间 3s ,动画循环执行,最后 ease-in-out 表示动画执行的节奏。
接下来我们就分步骤实现。
首先我们定义HTML结构,代码如下:
<!-- 灯笼容器 -->
<div class="lantern-con">
<!-- 提着灯笼的线 -->
<div class="lantern-line"></div>
<!-- 灯笼主要区域 -->
<div class="lantern-light">
</div>
</div>
然后我们画一个椭圆,然后通过 ::before 和 ::after ,绘制上下的两个灯笼盖,CSS如下:
/* 灯笼容器 */
.lantern-con {
position: fixed;
left: 160px;
}
/* 灯笼中间红色区域 */
.lantern-light {
position: relative;
width: 120px;
height: 90px;
background-color: red;
margin: 30px;
border-radius: 50%;
box-shadow: -5px 5px 50px 4px #fa6c00;
/* 设置旋转点 */
transform-origin: top center;
animation: swing 3s infinite ease-in-out;
}
/* 灯笼顶部和底部的样式 */
.lantern-light::before,
.lantern-light::after {
content: '';
position: absolute;
border: 1px solid #dc8f03;
width: 60px;
height: 12px;
/* 背景渐变 */
background: linear-gradient(
to right,
#dc8f03,
#ffa500,
#dc8f03,
#ffa500,
#dc8f03
);
left: 30px;
}
/* 顶部位置 */
.lantern-light::before {
top: -7px;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
}
/* 底部位置 */
.lantern-light::after {
bottom: -7px;
border-bottom-left-radius: 5px;
border-bottom-right-radius: 5px;
}
/* 提着灯笼的线的样式 */
.lantern-line {
width: 2px;
height: 50px;
background-color: #dc8f03;
position: absolute;
left: 88px;
}
/* 灯笼的动画效果 */
@keyframes swing {
0% {
transform: rotate(-6deg);
}
50% {
transform: rotate(6deg);
}
100% {
transform: rotate(-6deg);
}
}
现在就实现了一个比较基础灯笼外形,效果如下:
灯笼的内部线条是通过两个矩形实现了,通过 border-radius 属性将其设置为椭圆,然后绘制边,呈现灯笼线条。
<div class="lantern-light">
<!-- 灯笼中间的线条 -->
<div class="lantern-circle">
<div class="lantern-rect">
<!-- 灯笼中间的文字内容 -->
<div class="lantern-text">灯笼</div>
</div>
</div>
</div>
对应的CSS如下:
/* 灯笼中间的线条 */
.lantern-circle,
.lantern-rect {
height: 90px;
border-radius: 50%;
border: 2px solid #dc8f03;
background-color: rgba(216, 0, 15, 0.1);
}
/* 外层 */
.lantern-circle {
width: 100px;
margin: 12px 8px 8px 10px;
}
/* 内层 */
.lantern-rect {
margin: -2px 8px 8px 26px;
width: 45px;
}
/* 文字样式 */
.lantern-text {
font-size: 28px;
font-weight: bold;
text-align: center;
color: #dc8f03;
margin-top: 4px;
}
现在我们来绘制一下灯笼穗,这个东西比较简单,最主要的是添加一个动画效果,其HTML结构如下:
<!-- 灯笼主要区域 -->
<div class="lantern-light">
<!-- more code -->
<!-- 灯笼穗 -->
<div class="lantern-tassel-top">
<div class="lantern-tassel-middle"></div>
<div class="lantern-tassel-bottom"></div>
</div>
</div>
主要区域 -->
CSS如下:
/* 灯穗 */
.lantern-tassel-top {
width: 5px;
height: 20px;
background-color: #ffa500;
border-radius: 0 0 5px 5px;
position: relative;
margin: -5px 0 0 59px;
/* 让灯穗也有一个动画效果 */
animation: swing 3s infinite ease-in-out;
}
.lantern-tassel-middle,
.lantern-tassel-bottom {
position: absolute;
width: 10px;
left: -2px;
}
.lantern-tassel-middle {
border-radius: 50%;
top: 14px;
height: 10px;
background-color: #dc8f03;
z-index: 2;
}
.lantern-tassel-bottom {
background-color: #ffa500;
border-bottom-left-radius: 5px;
height: 35px;
top: 18px;
z-index: 1;
}
到这我们就把这个灯笼画完了。
本篇文章到这就结束了,都看完了就点个赞支持一下,谢谢了~
原作者姓名: 一碗周
原文链接: https:// juejin.cn/post/70513709 71932033038
*请认真填写需求信息,我们会在24小时内与您取得联系。