【CSS实例】

article/2025/11/11 15:26:13

学习目标:

css样式学习、总结,知识巩固


学习内容:

在这里我将会发布一些自己学习过程中完成的css实例,可能是跟着网上学习的练习作品,也可能是自己的随意的一个想法。每个作品中会列出编写思路,和编写过程中出现的问题。

目的就是巩固css的基础知识,防止遗忘

目录

一、css画时钟

1、涉及到的css技术

2、思路

3、实现

      1、页面基本布局

       2、css画出圆形作为表盘,页面上显示一个圆形

      3、画刻度

    4、指针和中心点的制作

    5、css完成,利用js使指针动起来

4、问题

 5、全部代码

二、 夜晚星空

 1、最终效果图

2、主要用到的CSS

3、步骤

1、html结构

2、天空

3、满天繁星

效果如下

4、流星

5、月亮

 6、加烟雾

四、问题



一、css画时钟

Document

1、涉及到的css技术

  •      利用css绘制圆形
.circle{border-radius: 50%;// 将一个div变为圆形
}
  •     画线并将线按照中心点旋转
{transform:rotate(6deg);//旋转角度transform-origin: center 150px;//改变旋转中心点
}

2、思路

   1、在页面上绘制一个div,长宽相等,css加圆角;

   2、绘制表盘刻度及数字,使用旋转;利用js循环画出所有刻度和数字;

   3、绘制时分秒指针;

   4、利用setInterval让指针动起来。

3、实现

      1、页面基本布局

<--外部圆形————表盘--> 
<div class="time-clock"> <!-- 表盘 刻度 数字 --><ul></ul><!-- 时针 --><div id="hour" class="hour"></div><!-- 分针 --><div id="minute" class="minute"></div><!-- 秒针 --><div id="second" class="second"></div><!-- 中心点 --><div class="ball"></div></div>

       2、css画出圆形作为表盘,页面上显示一个圆形

.time-clock {margin: 50px;width: 300px;height: 300px;border: 3px solid black;border-radius: 50%;position: relative;
}

      3、画刻度

     html元素中的 ul下加<li><span>数字</span></li>实现刻度和数字的画法。

我们画了一个300px*300px的表盘,要再这个圆里面画出刻度线并把这个圆等分;

分析:刻度:12*5=60;表盘上的刻度总共有60个刻度。

          度数:360/60=6deg;每个刻度占的度数;

          偏移量:i*6  i 为表盘上的刻度6为偏移度数,得出每个刻度的偏移度

          旋转中心:150px;我们画了一个直径300的圆,那它的中心点在150px处

接下来我们利用css角度偏移控制,刻度线的位置,利用js循环画出所有刻度线

先看css

.time-clock {margin: 50px;width: 300px;height: 300px;border: 3px solid black;border-radius: 50%;position: relative;ul {list-style: none;position: relative;width: 100%;height: 100%;li {width: 2px;height: 8px;position: absolute;background: black;left: 50%; transform-origin: center 150px;span {position: absolute;top: 15px;left: -5px;}}}
}

js画出所有线和数字

    let ul = document.querySelector("ul");let num = 0;for(let i=0;i<60;i++){let li = document.createElement('li');li.style.transform = `rotate(${i*6}deg)`;if(i%5 == 0){li.style.height='16px'// 一个大格的线要长一些let span = document.createElement('span');let clockNum = i==0 ? 12 : num;span.innerText = clockNum +'';span.style.transform = `rotate(-${i*6}deg)`;num++li.appendChild(span);}ul.appendChild(li);}

结果如下:


    4、指针和中心点的制作

表盘画完了,我们开始画指针,这里指针就是三个div设置宽度和长度。中心点是一个小圆画法同第一步,主要是确认中心点。指针上不要使用transform,因为一会儿要写脚本通过transfrom改变指针的角度,让指针动起来,css中使用的transform会被脚本中的所替代。

 

.time-clock {margin: 50px;width: 300px;height: 300px;border: 3px solid black;border-radius: 50%;position: relative;.hour,.minute,.second{position: absolute;left:50%;top:50%;background:black;transform-origin: center bottom;}.hour{width:6px;height:50px;// 时针设置到中心点 margin: -50px 0 0 -3px;}.minute{width:4px;height:80px;// 时针设置到中心点 margin: -80px 0 0 -3px;}.second{width:2px;height: 120px;margin: -120px 0 0 -3px;}.ball{width:20px;height:20px;position: absolute;background: black;border-radius: 50%;left:50%;top:50%;transform: translate(-50%,-50%);}ul {list-style: none;position: relative;width: 100%;height: 100%;li {width: 2px;height: 8px;position: absolute;background: black;left: 50%; transform-origin: center 150px;span {position: absolute;top: 15px;left: -5px;}}}
}

    5、css完成,利用js使指针动起来

 let hour = document.querySelector('#hour');let minute = document.querySelector('#minute');let second = document.querySelector('#second');// hour.style.transform=`rotate(${5*30}deg)`;setInterval(()=>{let date = new Date();let h = date.getHours();let m = date.getMinutes();let s = date.getSeconds();hour.style.transform=`rotate(${h*30+m/2}deg)`;minute.style.transform=`rotate(${m*6}deg)`;second.style.transform=`rotate(${s*6}deg)`;},1000)

4、问题

犯了一个粗心的问题,开始想着居中,把body的display设置成了flex布局,结果当页面缩小的时候表盘也缩小了。成了这个样子。

 5、全部代码

这里的css使用less写的,生成的css放到了 css文件夹下;

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><link rel="stylesheet" href="./css/time.css">
</head>
<body><div class="time-clock"><!-- 表盘 刻度 数字 --><ul></ul><!-- 时针 --><div id="hour" class="hour"></div><!-- 分针 --><div id="minute" class="minute"></div><!-- 秒针 --><div id="second" class="second"></div><!-- 中心点 --><div class="ball"></div></div>
</body>
<script>let ul = document.querySelector("ul");let num = 0;for(let i=0;i<60;i++){let li = document.createElement('li');li.style.transform = `rotate(${i*6}deg)`;if(i%5 == 0){li.style.height='16px'// 一个大格的线要长一些let span = document.createElement('span');let clockNum = i==0 ? 12 : num;span.innerText = clockNum +'';span.style.transform = `rotate(-${i*6}deg)`;num++li.appendChild(span);}ul.appendChild(li);}let hour = document.querySelector('#hour');let minute = document.querySelector('#minute');let second = document.querySelector('#second');setInterval(()=>{let date = new Date();let h = date.getHours();let m = date.getMinutes();let s = date.getSeconds();hour.style.transform=`rotate(${h*30+m/2}deg)`;minute.style.transform=`rotate(${m*6}deg)`;second.style.transform=`rotate(${s*6}deg)`;},1000)
</script>
</html>
// out: ../css/
* {margin: 0;padding: 0;
}body {width: 100%;height: 100%;
}.time-clock {margin: 50px;width: 300px;height: 300px;border: 3px solid black;border-radius: 50%;position: relative;.hour,.minute,.second{position: absolute;left:50%;top:50%;background:black;transform-origin: center bottom;}.hour{width:6px;height:50px;// 时针设置到中心点 margin: -50px 0 0 -3px;}.minute{width:4px;height:80px;// 时针设置到中心点 margin: -80px 0 0 -3px;}.second{width:2px;height: 120px;margin: -120px 0 0 -3px;}.ball{width:20px;height:20px;position: absolute;background: black;border-radius: 50%;left:50%;top:50%;transform: translate(-50%,-50%);}ul {list-style: none;position: relative;width: 100%;height: 100%;li {width: 2px;height: 8px;position: absolute;background: black;left: 50%; transform-origin: center 150px;span {position: absolute;top: 15px;left: -5px;}}}
}

二、 夜晚星空

 1、最终效果图

Document

看到网上有人用css画了烟雾的效果,想要模仿。开始背景就是黑色感觉只有雾有些单调,就琢磨着加了些东西,按着自己的想法画了星星,月亮,流星等,就成了上面的图。

真正页面是动态的(* ̄︶ ̄)

2、主要用到的CSS

渐变

动画

模糊

3、步骤

自己练习的时候最开始画的是雾,开始只想练习这个来着。现在写步骤,会从背景、星空开始写,最后画雾。

1、html结构

 <body><!-- 星星 --><div class="star"><span class="square">✦</span><span class="square">✦</span><span class="square">✦</span><span class="square">✦</span><!-- 流星 --><section><span></span><span></span><span></span><span></span><span></span></section></div><!-- 月亮 --><div class="moon"></div><!-- 烟雾 --><div class="mist"></div></body>

2、天空

html,
body {width: 100%;height: 100vh;
}// body{
body {background: radial-gradient(300% 100% at top, #01012b 10%, #775280 40%, #eb8499 65%, #f7eca7);overflow: hidden;position: relative;
}

3、满天繁星

随机在天上画上大小不同的小圆点,@keyframes blink 闪烁起来。animation-delay 延迟

 // 星星let star=document.getElementsByClassName('star');for(let i = 0;i<150;i++){let size=Math.floor(Math.random()*3)+'px';let div = document.createElement('div');div.style=`width:${size};height:${size};top:${Math.random()*30}vh;left:${Math.random()*100}vw;transform:scale(${Math.random()*2});animation-delay:${Math.random()*2}s;animation: blink ${Math.floor(Math.random()*4)}s ease infinite alternate;`;star[0].appendChild(div);}
.star {width: 100vw;height: 50vh;position: relative;div {border-radius: 50%;background-color: white;position: absolute;} 
}
@keyframes blink {0% {box-shadow: 0 0 0 0 #fff;}100% {box-shadow: 0 0 1.4px 1.4px #fff;}
}

效果如下

加入✦这样的星星

.star .square {display: block;position: absolute;top: 20px;right: 380px;font-size: 30px;z-index: 101;color: #fff;animation-delay: .5s;animation: lights 1.5s ease infinite alternate;
}.star .square:nth-child(2) {font-size: 20px;top: 20%;right: 60%;animation-delay: .8s;
}.star .square:nth-child(3) {font-size: 15px;top: 10px;right: 600px;animation-delay: 1s;
}.star .square:nth-child(4) {font-size: 20px;top: 60px;right: 180px;
}
@keyframes lights {0%,100% {transform: scale(1);}50% {transform: scale(1.5);}
}

4、流星

// 星星
.star {width: 100vw;height: 50vh;position: relative;div {border-radius: 50%;background-color: white;position: absolute;}section{span{position: absolute;width: 4px;height:4px;border-radius: 50%;top:30px;right:300px;background-color: white;box-shadow: 0 0 0 3px rgba(255,255,255,.1),0 0 0 3px rgba(255,255,255,.1),0 0 9px rgba(255, 255, 255, 0.1);animation:meteor 3s linear infinite;opacity: 0;&::after{position: absolute;content: '';width:100px;height: 1px;top:50%;background:linear-gradient(100deg,#fff,transparent);}}span:nth-child(2){top:0;right:780px;animation-delay: 0.2s;animation-duration: 2s;}span:nth-child(3){top:0;right:680px;animation-delay: 0.4s;animation-duration: 4s;}span:nth-child(4){top:0;right:900px;animation-delay: 0.6s;animation-duration: 2.5s;}span:nth-child(5){top:0;right:1200px;animation-delay: 0.5s;animation-duration: 3s;}}
}
@keyframes meteor{0%{transform: rotate(315deg) translateX(0);opacity:1;}70%{opacity:1}100%{transform:rotate(315deg) translateX(-1000px);opacity:0}}

5、月亮

.moon {position: absolute;display: flex;top: 50px;right: 100px;width: 200px;height: 200px;border-radius: 50%;opacity: .8;justify-content: center;align-items: center;background: radial-gradient(circle at 90% 120%, #ffffff, #f8faa4 80%, #faf602 100%);box-shadow: 0 0 150px 60px #f0efb6;
}

 6、加烟雾

随机画出一些长短不一的线条或者细长的椭圆之类的,随意吧。

let num=40;let mist=document.getElementsByClassName('mist');for(i=0;i<num;i++){let span = document.createElement('span');span.style=`margin-left:${Math.random()*30}vw;width:${Math.random()*20}vw;filter:blur(${Math.floor((Math.random()*8)+6)}px);height:${Math.random()*5}px;--i:${Math.floor((Math.random()*40)+1)}`;// margin-top: ${Math.random()*10}px;mist[0].appendChild(span);}

加上模糊效果  上面代码中的filter:blur(${Math.floor((Math.random()*8)+6)}px);数值自己随意调。

动起来

@keyframes fog {0% {opacity: 0;transform: translate(0) scaleY(1);}15% {opacity: 1;transform: translate(15vw, 30px) scaleY(4);}30% {opacity: 0.9;transform: translate(30vw, 40px) scaleY(6);}45% {opacity: 0.8;transform: translate(45vw, 60px) scaleY(8);}60% {opacity: 0.7;transform: translate(60vw, 70px) scaleY(10);}70% {opacity: 0.6;transform: translate(70vw, 50px) scaleY(8);}80% {opacity: 0.5;transform: translate(80vw, 40px) scaleY(6);}90% {opacity: 0.4;transform: translate(90vw, 30px) scaleY(4);}100% {opacity: 0;transform: translate(100vw, -100px) scaleY(2);}
}

完成;

四、问题

没遇到什么问题,自己瞎琢磨着乱写,想到哪就写哪,有些css的属性方法不知道就从网上搜,主要是为了熟悉css的各种属性。各种参数自己多试试就知道了。

开始想着这个有些麻烦,但是写完后感觉好像也没用到啥,主要就是渐变、动画。


http://chatgpt.dhexx.cn/article/WgISfL0Y.shtml

相关文章

codewar代码练习1——8级晋升7级

最近发现一个不错的代码练习网站codewar&#xff08;http://www.codewars.com&#xff09;。注册了一个账号&#xff0c;花了几天的茶余饭后时间做题&#xff0c;把等级从8级升到了7级。本文的目的主要介绍使用感受及相应题目&#xff0c;可供大家参考。 新人注册为8级&#xf…

codewar python 遗忘点

2019独角兽企业重金招聘Python工程师标准>>> 1、计算字符串中特定字符串出现的次数 s this is a new technology,and I want to learn this. print(s.count(this, 0, len(s))) #目标字符串区分大小写 2、数字左边补0的方法,字符串补空格 #python中有一个zfil…

Codewar - Bit Counting

2019独角兽企业重金招聘Python工程师标准>>> Write a function that takes an (unsigned) integer as input, and returns the number of bits that are equal to one in the binary representation of that number. Example: The binary representation of 1234 is…

codewar 代码练习1——8级晋升7级

最近发现一个不错的代码练习网站codewar&#xff08;http://www.codewars.com&#xff09;。注册了一个账号&#xff0c;花了几天的茶余饭后时间做题&#xff0c;把等级从8级升到了7级。本文的目的主要介绍使用感受及相应题目&#xff0c;可供大家参考。 新人注册为8级&#xf…

codewar 代码练习2——7级晋升6级

7级晋升到6级的过程中以做6级题以及以前未完成的题目为主&#xff0c;一般选择算法题或者基础题。相比之前从8级升级7级&#xff08;参见此博客&#xff1a;http://blog.csdn.net/m0_37324740/article/details/78408249&#xff09;的难度有所提前&#xff0c;并且一些题目结合…

R数据分析,codewar的年终总结,和一周年总结

前阵子单位各个部门都在要求弄总结&#xff0c;想想自己这个公众号也写了快一年了&#xff0c;专门回去翻了翻&#xff0c;这个公众号发布的第一篇文章是在2021年的1月17日&#xff0c;我想2022年的1月17日我就把现在敲的文字推出来吧&#xff0c;也算是一个年终和周年总结。 …

CodeWar题目

打算把不同网站上面的题目分开整理&#xff0c;免得麻烦。Code War上面我还是刷了一堆6级及以下的题目的&#xff0c;不过价值不大&#xff0c;这种不太能够训练实际解决问题的能力&#xff0c;所以我已经很久没上过了&#xff0c;有时间了可能会重新上去刷题吧&#xff0c;到时…

Codewar 笔记

1. Weight for weight 题目&#xff1a; For example 99 will have “weight” 18, 100 will have “weight” 1 so in the list 100 will come before 99. Given a string with the weights of FFC members in normal order can you give this string ordered by “weights”…

codewar刷题,苦海造舟之始

今天又是被惨虐的一天&#xff0c;尽管今天是我这篇处女座发布的日子。   事情是这样的&#xff0c;身为一个刚迈步进入编程领域的小白&#xff0c;在无忧无虑&#xff0c;轻松惬意的心情下刷完了一套python课后&#xff0c;偶然间&#xff0c;很突然地了解到codewars这么个玩…

Codewar一些积累No.2 从矩阵的加法体会vector的用法

用代码实现矩阵加法问题 最近在Codewar上看到一个有趣的问题是关于矩阵的加法问题. 题目中, 我所要编写的函数的传入参数是两个向量, 而且此向量是嵌套的, 具体内容如下: std::vector<std::vector<int> > matrixAddition(std::vector<std::vector<int> …

Java到底好不好学

Java到底好不好学 答案是&#xff1a;不难学。很多人都以为编程是个很高深的东西&#xff0c;其实不然&#xff0c;真正学习了你会发现编程比你高中学的数理化要简单的多。说它不难呢&#xff0c;如果学深入了&#xff0c;还算有很多东西要学习&#xff0c;比如你学Java&#…

java面试为何那么难

java面试为何那么难 “面试造火箭、工作拧螺丝”&#xff0c;曾经这么一句调侃的话总是用来形容IT行业中的面试情况。作为一个流浪的程序猿&#xff0c;多年以来作为应聘者也好、面试官也罢&#xff0c;渐渐感受到java开发的面试不再仅仅在“造火箭”那么容易。 我的就职历程…

java面试为何那么难?

“面试造火箭、工作拧螺丝”&#xff0c;曾经这么一句调侃的话总是用来形容IT行业中的面试情况。作为一个流浪的程序猿&#xff0c;多年以来作为应聘者也好、面试官也罢&#xff0c;渐渐感受到java开发的面试不再仅仅在“造火箭”那么容易。 五年前的java面试是怎么样的 用HT…

女生学java开发难吗?女生适合学java吗?

女生学java开发&#xff1f;Java开发看上去是一项系统性很强、入门很难的“高大上”学科&#xff0c;前端、代码这些普通人基本不会接触到的名词&#xff0c;吓怕了众多初学者。大部分人对于Java程序员都有一个既定印象&#xff0c;那就是程序员都是男生。女程序员可以说是“稀…

自学java难吗?给java初学者的一些建议。

自学java到底难不难&#xff1f; 其实学习java说难不难&#xff0c;说简单也不简单。如今互联网十分发达&#xff0c;各种学习资料&#xff0c;视频&#xff0c;文档都可以在网上找到。可以说如今是一个全民自学的时代&#xff0c;你要你有决心和时间&#xff0c;足不出户便能…

java编程难学吗?

java是一门面向对象编程语言&#xff0c;不仅吸收了C语言的各种优点&#xff0c;还摒弃了C里难以理解的多继承、指针等概念&#xff0c;因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表&#xff0c;极好地实现了面向对象理论&#xff0c;…

初学者的困境,Java自学难吗

Java自学起来难吗&#xff1f;动力节点小编告诉你&#xff0c;虽然Java适合新手入门&#xff0c;但是难度不能算简单哦&#xff0c;毕竟也是一门知识体系比较多的技术知识。在学习Java编程时&#xff0c;您会遇到一些简单的概念&#xff0c;如变量和函数。但也有更抽象、复杂的…

学python和java哪个难?,java和python哪个难学

java和python哪个好学 ①python比Java简单&#xff0c;学习成本低&#xff0c;开发效率高;②Java运行效率高于python&#xff0c;尤其是纯python开发的程序&#xff0c;效率极低;③Java相关资料多&#xff0c;尤其是中文资料;④Java版本比较稳定&#xff0c;python2和3不兼容导…

该说不说,Java面试是真的难

作为一名优秀的程序员&#xff0c;技术面试都是不可避免的一个环节&#xff0c;一般技术面试官都会通过自己的方式去考察程序员的技术功底与基础理论知识。 如果你参加过一些大厂面试&#xff0c;肯定会遇到一些这样的问题&#xff1a; 1、看你项目都用的框架&#xff0c;熟悉…

java到底难在哪里?

作为一个已经上岸和还不错的程序员来说&#xff0c;java到底难在哪里&#xff0c;在我看来可能难在坚持吧&#xff0c;毕竟过程是难熬的&#xff0c;毕竟走出了新手村. 今天我把读者自学上岸并成功入行的经验分享给大家&#xff0c;希望能帮助到大家。他自学的时候经常来咨询我…