当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 纯js时钟特效详细代码分析实例教程

纯js时钟特效详细代码分析实例教程

2020年04月22日  | 移动技术网IT编程  | 我要评论

电子时钟是网上常见的功能,在学习date对象和定时器功能时,来完成一个电子时钟的制作是不错的选择。学习本教程之前,读者需要具备html和css技能,同时需要有简单的javascript基础。

先准备一个html元素,用来放置时钟。新建一个div元素,它的id命名为clock,如下所示:

<div id="clock" class="clock_con"></div><!--基础时钟元素-->

本实例电子时钟的格式设定为 (yyyy-mm-dd hh:mm:ss) ,用js来组合一个简单的时钟字符串放到clock元素中。

本实例把时钟功能封装到函数中,所以先创建一个creatclock函数,在creatclock中再来编写具体代码。

建议在完成某一个前端功能时,应先分析功能的具体操作。再根据具体操作把实现功能的方法分成多个步骤,接下来一个步骤一个步骤去完成它。来看一下用js组合这样一串字符,需要哪些步骤:

1 调用date对象,获取计算机的本地时间
  1.1 调用date对象
  1.2 获取当前年份
  1.3 获取当前月份,月份是从0开始计数,所以需要加1才是正确的月份
  1.4 获取当前日期
  1.5 获取当前小时
  1.6 获取分钟
  1.7 获取秒数
2. 格式化获取到的时间数据
  2.1 单数字前添加字符串0,用以符合时钟格式
  2.2 组合时间数据为字符串
3. 在clock元素中实时显示时间
  3.1 获取clock元素
  3.2 修改clock元素中的时间
  3.3 使用定时器实时更新时间

具体代码如下:

function fncreatclock(){
  //声明时间相关变量
  var dlocal,nyear,nmonth,ndate,nhours,nminutes,nseconds;

  //1 获取计算机本地时间
  function fngetdate(){ 
    //1.1 调用date对象
    dlocal = new date();
    //1.2 获取当前年份
    nyear = dlocal.getfullyear(); 
    //1.3 获取当前月份,月份是从0开始计数,所以需要加1才是正确的月份
    nmonth = dlocal.getmonth() + 1; 
    //1.4 获取当前日期
    ndate = dlocal.getdate(); 
    //1.5 获取当前小时
    nhours = dlocal.gethours(); 
    //1.6 获取分钟
    nminutes = dlocal.getminutes(); 
    //1.7 获取秒数
    nseconds = dlocal.getseconds(); 
  }

  //2.1 封装一个函数,用于把单数字前添加字符串0,例如1改为01
  function fntodouble(num){  
    //声明一个返回结果
    var sresult = ''; 
    if(num<10){
      //判断数字小于10则是单数字,需要在前面添加字符串0
      sresult = '0' + num;
    }else{
      //数字为10以上转换为字符串
      sresult = '' + num;
    }
    //返回格式化后的字符串
    return sresult; 
  }

  function fnformatdate(){
    //2.2 组合时间数据为字符串。本实例主要针对初学者,所以这里用的是最简单的格式化方式,即把所有数据用+号相连
    return nyear + '-' + fntodouble(nmonth) + '-' + fntodouble(ndate) +
           ' ' + fntodouble(nhours) + ':' + fntodouble(nminutes) + ':' + fntodouble(nseconds);
  }

  //3.1 获取clock元素
  var eclock = document.getelementbyid('clock'); 
  //获取时间 
  fngetdate();
  //3.2 修改clock元素中的时间
  eclock.innerhtml = fnformatdate(); 

  //使用定时器实时更新时间
  setinterval(function(){ 
    //3.3 每秒更新时间
    fngetdate();  
    //3.3 修改clock元素中的时间
    eclock.innerhtml = fnformatdate(); 
  },1000); 
}
fncreatclock();

此时的效果如图所示:

 

现在做出来的时钟看起来感觉有点简陋,也可以尝试把数字换成图片,这样所呈现效果就会好很多。这里暂时忽略日期部分,只为时间部分添加图片效果,还是先看一下需要哪些html元素,代码如下:

<div id="imgclock" class="clock_container"><!--图片时钟元素-->
  <div id="imghours" class="img_box">
    <span class="img_0"></span>
    <span class="img_0"></span>
  </div>
  <div class="img_point"></div>
  <div id="imgminutes" class="img_box">
    <span class="img_0"></span>
    <span class="img_0"></span>
  </div>
  <div class="img_point"></div>
  <div id="imgseconds" class="img_box">
    <span class="img_0"></span>
    <span class="img_0"></span>
  </div>
</div>

还需要准备0-9共10张图片,可以自己制作类似效果的数字图片。css代码可以自己根据需要编写,也可以复制以下代码使用:

  .clock_container{
    margin-top:60px;
    font-size:0;
    text-align:center;
  }
  .clock_container div{
    display:inline-block;
  }
  .clock_container .img_box span{
    display:inline-block;
    width:80px;
    height:100px;
    margin:0 2px;
    border-radius:8px;
    background-color:#77a6b6;
  }
  .clock_container .img_0{
    background:url(img/img_0.png) no-repeat;
  }
  .clock_container .img_1{
    background:url(img/img_1.png) no-repeat;
  }
  .clock_container .img_2{
    background:url(img/img_2.png) no-repeat;
  }
  .clock_container .img_3{
    background:url(img/img_3.png) no-repeat;
  }
  .clock_container .img_4{
    background:url(img/img_4.png) no-repeat;
  }
  .clock_container .img_5{
    background:url(img/img_5.png) no-repeat;
  }
  .clock_container .img_6{
    background:url(img/img_6.png) no-repeat;
  }
  .clock_container .img_7{
    background:url(img/img_7.png) no-repeat;
  }
  .clock_container .img_8{
    background:url(img/img_8.png) no-repeat;
  }
  .clock_container .img_9{
    background:url(img/img_9.png) no-repeat;
  }
  .clock_container .img_point{
    width:60px;
    height:100px;
    background:url(img/img_point.png) no-repeat center;
  }

按照惯例,完成功能前先整理步骤。这里再多添加一个步骤,在imgclock元素中来完成图片美化后的时钟效果,步骤如下:

4. 在imgclock元素中,用图片作为背景实时修改时间
  4.1 分别获取时(imghours)、分(imgminutes)、秒(imgseconds)元素
  4.2 根据时间修改时、分、秒元素的class,用来改变背景样式
  4.3 使用定时器更新元素背景

修改后的代码如下:

function fncreatclock(){
  //声明时间相关变量
  var dlocal,nyear,nmonth,ndate,nhours,nminutes,nseconds;

  //1 获取计算机本地时间
  function fngetdate(){ 
    //1.1 调用date对象
    dlocal = new date();
    //1.2 获取当前年份
    nyear = dlocal.getfullyear(); 
    //1.3 获取当前月份,月份是从0开始计数,所以需要加1才是正确的月份
    nmonth = dlocal.getmonth() + 1; 
    //1.4 获取当前日期
    ndate = dlocal.getdate(); 
    //1.5 获取当前小时
    nhours = dlocal.gethours(); 
    //1.6 获取分钟
    nminutes = dlocal.getminutes(); 
    //1.7 获取秒数
    nseconds = dlocal.getseconds(); 
  }

  //2.1 封装一个函数,用于把单数字前添加字符串0,例如1改为01
  function fntodouble(num){  
    //声明一个返回结果
    var sresult = ''; 
    if(num<10){
      //判断数字小于10则是单数字,需要在前面添加字符串0
      sresult = '0' + num;
    }else{
      //数字为10以上转换为字符串
      sresult = '' + num;
    }
    //返回格式化后的字符串
    return sresult; 
  }
  
  //获取时间 
  fngetdate();
  
  //4.1 获取图片背景的小时元素
  var eimghours = document.getelementbyid('imghours');
  //获取小时的第一个元素
  var ehours1 = eimghours.getelementsbytagname('span')[0]; 
  //获取小时的第二个元素 
  var ehours2 = eimghours.getelementsbytagname('span')[1];  
  //4.1 获取图片背景的分钟元素
  var eimgminutes = document.getelementbyid('imgminutes');
  //获取分钟的第一个元素
  var eminutes1 = eimgminutes.getelementsbytagname('span')[0]; 
  //获取分钟的第二个元素 
  var eminutes2 = eimgminutes.getelementsbytagname('span')[1];  
  //4.1 获取图片背景的秒元素
  var eimgseconds = document.getelementbyid('imgseconds');  
  //获取秒的第一个元素
  var eseconds1 = eimgseconds.getelementsbytagname('span')[0];
  //获取秒的第二个元素  
  var eseconds2 = eimgseconds.getelementsbytagname('span')[1];  

  // 4.2 根据时间修改时、分、秒元素的class,用来改变背景样式
  function fnchangeimgbg(){ 
    ehours1.classname = 'img_' + fngetimgnum(nhours,0);
    ehours2.classname = 'img_' + fngetimgnum(nhours,1);
    eminutes1.classname = 'img_' + fngetimgnum(nminutes,0);
    eminutes2.classname = 'img_' + fngetimgnum(nminutes,1);
    eseconds1.classname = 'img_' + fngetimgnum(nseconds,0);
    eseconds2.classname = 'img_' + fngetimgnum(nseconds,1);
  }

  //此函数用来计算根据当前时间的数字
  function fngetimgnum(num,bit){ 
    //声明一个返回结果
    var nresult = 0;
    //判断是个位还是十位,0代表十位,1代表个位  
    if(bit===0){  
      //使用math.floor可以向下取整,即不管是0.1还是0.9,都是取整数0。此方法用来获取时间的十位
      nresult = math.floor(num/10);
    }else{
      //通过fntodouble函数把时间格式化双字符串,再取后面一个字符获取个位,前面的+号用于转换为数字
      nresult = +fntodouble(num).slice(1);
    }
    return nresult;
  }
  fnchangeimgbg();

  //使用定时器实时更新时间
  setinterval(function(){ 
    //3.3 每秒更新时间
    fngetdate();  
    fnchangeimgbg();  //4.3 使用定时器更新元素背景
  },1000); 
}
fncreatclock();

此时的效果比单独的文字还是会增色不少,如图所示:

 

 

我想要求效果再漂亮一点,让图片不是很突兀的改变,而是有一个滚动的动画。要实现这样的效果,图片需要改成一张0-9的竖形排列图,每个数字的高度要和时钟元素高度一致。再通过修改元素背景图片的位置,即可产生滚动的动画效果。

此效果需要的html元素如下所示:

<div id="animationclock" class="clock_container"><!--动画时钟元素-->
  <div id="animationhours" class="animation_box">
    <span></span>
    <span></span>
  </div>
  <div class="img_point"></div>
  <div id="animationminutes" class="animation_box">
    <span></span>
    <span></span>
  </div>
  <div class="img_point"></div>
  <div id="animationseconds" class="animation_box">
    <span></span>
    <span></span>
  </div>
</div>

在css里面给每一个元素加上同一个背景图片,需要加上transition过渡样式用来产生动画效果,如下所示:

.clock_container .animation_box span{
  display:inline-block;
  width:80px;
  height:100px;
  margin:0 2px;
  border-radius:8px;
  background-color:#77a6b6;
  background-image:url(img/img_all.png);
  background-repeat:no-repeat;
  transition:.2s;
}

再随着时间改变给每一个时间元素修改背景图片的位置,即修改background-repeat-y的样式即可。按照惯例,还是先列步骤:

5. 在animationclock元素中,滚动动画显示时钟的实时变化
  5.1 分别获取时(imghours)、分(imgminutes)、秒(imgseconds)元素
  5.2 根据时间修改时、分、秒元素的背景图片位置
  5.3 使用定时器更新元素背景图片位置

修改后的代码如下:

function fncreatclock(){
  //声明时间相关变量
  var dlocal,nyear,nmonth,ndate,nhours,nminutes,nseconds;

  //1 获取计算机本地时间
  function fngetdate(){ 
    //1.1 调用date对象
    dlocal = new date();
    //1.2 获取当前年份
    nyear = dlocal.getfullyear(); 
    //1.3 获取当前月份,月份是从0开始计数,所以需要加1才是正确的月份
    nmonth = dlocal.getmonth() + 1; 
    //1.4 获取当前日期
    ndate = dlocal.getdate(); 
    //1.5 获取当前小时
    nhours = dlocal.gethours(); 
    //1.6 获取分钟
    nminutes = dlocal.getminutes(); 
    //1.7 获取秒数
    nseconds = dlocal.getseconds(); 
  }

  //2.1 封装一个函数,用于把单数字前添加字符串0,例如1改为01
  function fntodouble(num){  
    //声明一个返回结果
    var sresult = ''; 
    if(num<10){
      //判断数字小于10则是单数字,需要在前面添加字符串0
      sresult = '0' + num;
    }else{
      //数字为10以上转换为字符串
      sresult = '' + num;
    }
    //返回格式化后的字符串
    return sresult; 
  }

   //获取时间 
  fngetdate();
  
  //此函数用来计算根据当前时间的数字
  function fngetimgnum(num,bit){ 
    //声明一个返回结果
    var nresult = 0;
    //判断是个位还是十位,0代表十位,1代表个位  
    if(bit===0){  
      //使用math.floor可以向下取整,即不管是0.1还是0.9,都是取整数0。此方法用来获取时间的十位
      nresult = math.floor(num/10);
    }else{
      //通过fntodouble函数把时间格式化双字符串,再取后面一个字符获取个位,前面的+号用于转换为数字
      nresult = +fntodouble(num).slice(1);
    }
    return nresult;
  }

  //5.1 获取动画时钟的小时元素
  var eanimationhours = document.getelementbyid('animationhours');  
  //获取小时的第一个元素
  var ehours3 = eanimationhours.getelementsbytagname('span')[0];
  //获取小时的第二个元素  
  var ehours4 = eanimationhours.getelementsbytagname('span')[1];  
  //5.1 获取动画时钟的分钟元素
  var eanimationminutes = document.getelementbyid('animationminutes');
  //获取分钟的第一个元素  
  var eminutes3 = eanimationminutes.getelementsbytagname('span')[0];
  //获取分钟的第二个元素  
  var eminutes4 = eanimationminutes.getelementsbytagname('span')[1];  
  //5.1 获取动画时钟的秒元素
  var eanimationseconds = document.getelementbyid('animationseconds');
  //获取秒的第一个元素  
  var eseconds3 = eanimationseconds.getelementsbytagname('span')[0];
  //获取秒的第二个元素  
  var eseconds4 = eanimationseconds.getelementsbytagname('span')[1];  

  // 5.2 根据时间修改时、分、秒元素的背景图片位置
  function fnanimationbg(){
    ehours3.style.backgroundpositiony = -fngetimgnum(nhours,0) * 100 + 'px';
    ehours4.style.backgroundpositiony = -fngetimgnum(nhours,1) * 100 + 'px';
    eminutes3.style.backgroundpositiony = -fngetimgnum(nminutes,0) * 100 + 'px';
    eminutes4.style.backgroundpositiony = -fngetimgnum(nminutes,1) * 100 + 'px';
    eseconds3.style.backgroundpositiony = -fngetimgnum(nseconds,0) * 100 + 'px';
    eseconds4.style.backgroundpositiony = -fngetimgnum(nseconds,1) * 100 + 'px';
  }
  fnanimationbg();

  //使用定时器实时更新时间
  setinterval(function(){ 
    //3.3 每秒更新时间
    fngetdate();  
    //5.3 使用定时器更新元素背景图片位置
    fnanimationbg();
  },1000); 
}
fncreatclock();

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网