基于html+css+js实现动态时钟

参照B站的一个视频,利用html+css+js实现了动态时钟。
视频链接为: link

动态时钟的效果

基于HTML+css+js实现的动态时钟

完整代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>clock</title>
    <style type="text/css">
      *
      {
        /*外边距为0*/
        margin: 0;
        /*内边距为0*/
        pading: 0;
        /*规定一个带边框的框*/
        box-sizing: border-box;
      }
      /*将时钟固定在页面中间的弹性盒子,时钟大小多大,盒子就有多大*/
      body
      {
        /*固定时钟的长方形盒子为弹性布局*/
        display: flex;
        /*居中*/
        justify-content: center;
        /*交叉轴的中点对齐*/
        align-items: center;
        /*该块元素,高度如果小于100vh,那么这个块元素就不会再自动变小,而是保持100vh不变*/
        /*vh指相对长度单位,view height*/
        min-height: 100vh;
        background: #091921;
      }
      /*放置时钟的正方形盒子*/
      .clock
      {
        /*时钟宽度*/
        width: 400px;
        /*时钟高度*/
        height: 400px;
        /*时钟盒子为弹性布局*/
        display: flex;
        /*盒子中的内容居中*/
        justify-content: center;
        /*交叉轴的中点对齐*/
        align-items: center;
        background: url("clock.png");
        /*把背景图片放大到适合元素容器的尺寸,图片比例不变,超出容器的部分可能会被裁掉*/
        background-size: cover;
        /*盒子的框实线*/
        border: 4px solid #091921;
        /*把盒子框实线锐化为圆角*/
        border-radius: 50%;
        box-shadow: 0 -15px 15px rgba(255,255,255,0.05),
        inset 0 -15px 15px rgba(255,255,255,0.05),
        0 15px 15px rgba(0,0,0,0.3),
        inset 0 -15px 15px rgba(255,255,255,0.3);

      }
      /*时钟中心*/
      /*在clock元素前插入中心*/
      .clock:before
      {
        content: '';
        /*与clock的位置有关*/
        /*绝对定位的盒子,在没有规定left和top时,是相对于父元素的content左上角定位的,不包含padding*/
        /*绝对定位与padding和父级相对定位的box-sizing有关*/
        position: absolute;
        width: 20px;
        height: 20px;
        background: #ffffff;
        border-radius: 50%;
        z-index: 10000;
      }
      .clock .hour,
      .clock .min,
      .clock .sec
      {
        position: absolute;
      }
      /*设置时针分针秒针的位置*/
      .clock .hour ,hr
      {
        width: 0;
        height: 0;
      }
      .clock .min ,mn
      {
        width: 0;
        height: 0;
      }
      .clock .sec ,sec
      {
        width: 0;
        height: 0;
      }
      .hr, .mn, .sc
      {
        display: flex;
        /*justify-content: flex-end;*/
        justify-content: center;
        /*align-items: center;*/
        position: absolute;
        border-radius: 50%;
      }
      .hr:before
      {
        content: '';
        position: absolute;
        width: 8px;
        height: 80px;
        background: #4169E1;
        z-index: 10;
        border-radius: 6px 6px 0 0;
      }
      .mn:before
      {
        content: '';
        position: absolute;
        width: 4px;
        height: 90px;
        background: #fff;
        z-index: 11;
        border-radius: 6px 6px 0 0;
      }
      .sc:before
      {
        content: '';
        position: absolute;
        width: 2px;
        height: 150px;
        background: #fff;
        z-index: 12;
        border-radius: 6px 6px 0 0;
      }
    </style>

<!--    <link rel="stylesheet" href="style.css">-->
</head>
<body>
  <div class="clock">
    <div class="hour">
      <div class="hr" id="hr"></div>
    </div>
    <div class="min">
      <div class="mn" id="mn"></div>
    </div>
    <div class="sec">
      <div class="sc" id="sc"></div>
    </div>
  </div>

  <script type="text/javascript">
    const deg = 6;
    const nor = 180;
    const hr = document.querySelector('#hr');
    const mn = document.querySelector('#mn');
    const sc = document.querySelector('#sc');

    setInterval(() => {
      let day = new Date();
      let hh = day.getHours() * 30;
      let mm = day.getMinutes() * deg;
      let ss = day.getSeconds() * deg;

      hr.style.transform = `rotateZ(${(hh)+(mm/12)+177}deg)`;
      mn.style.transform = `rotateZ(${mm+nor}deg)`;
      sc.style.transform = `rotateZ(${ss+nor}deg)`;
      // hr.style.transform = rotateZ(+ hh + deg);
      // mn.style.transform = rotateZ(+ mm + deg);
      // sc.style.transform = rotateZ(+ ss + deg);
    })

  </script>
</body>
</html>

动态时钟背景图
clock

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现HTML CSS轮播图,可以按照以下步骤进行操作: 1. 创建HTML结构,使用\<div>元素包裹轮播图容器,并在其中添加\<div>元素作为图片项的容器。每个图片项需要设置一个类名,例如item1,item2等,并在其中添加一个\<img>元素作为图片的标签。同时,为了实现轮播效果,可以添加一个具有唯一标识的\<input>元素,以及对应的\<label>元素,用于切换图片。 2. 使用CSS设置轮播图容器的样式,包括设置宽度、高度、位置等。可以使用position: relative;来将轮播图容器设置为相对定位。 3. 使用CSS设置图片项的样式,并使用background-image属性为每个项设置不同的背景图片。可以使用background-size: cover;来将背景图片扩展至足够大,以覆盖背景区域。同时,可以使用position: absolute;和left属性来设置每个图片项的位置。 4. 使用CSS设置切换按钮的样式。可以使用CSS伪类选择器:checked来表示选中状态的\<input>元素,通过选择器的方式来控制图片项的显示与隐藏。可以将\<label>元素设置为轮播图容器的子元素,并使用position: absolute;和top属性来调整按钮的位置。 5. 使用CSS过渡效果transition来控制图片项的切换动画。可以使用transition: all 0.5s;将所有属性的变化都设置为0.5秒的过渡效果。 通过以上步骤,就可以实现一个基于HTMLCSS的轮播图效果。具体的代码实现请参考和中的引用内容。 : 环绕倒影加载特效 html css 气泡浮动背景特效 html css 简约时钟特效 html css js 赛博朋克风格按钮 html css 仿网易云官网轮播图 html css js 水波加载动画 html css 导航栏滚动渐变效果 html css js 书本翻页 html css 3D立体相册 html css 霓虹灯绘画板效果 html css js 记一些css属性总结(一) Sass总结笔记 position: absolute; 绝对定位1. transition: all 0.5s; 给个过渡效果,后面切图时好看。 background-size: cover; 把背景图像扩展至足够大,以使背景图像完全覆盖背景区域。 4.每张图片自己的样式,就是都先偏移相应距离: .item1{ background-image: url(./img/1.jpg); } .item2{ background-image: url(./img/4.jpg); left: 100%; } .item3{ background-image: url(./img/11.jpg); left: 200%; } : :checked CSS 伪类选择器表示任何处于选中状态的radio(), checkbox () 或(“select”) 元素中的option HTML元素(“option”)。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值