时钟效果

时钟

原生js实现;

知识点:递归技巧或思想、window.onload页面加载完成后、Date对象、三元运算补零技巧。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>测试</title>
</head>
<body>

    <span class="year" id="data"></span>
    <span class="hour" id="now"></span>

<script type="text/javascript">

    window.onload = showTime;

    function showTime()
        {
          var today=new Date();
          var year=today.getFullYear();
          var month=today.getMonth()+1;
          var day=today.getDate();
          var hours=today.getHours();
          var minutes=today.getMinutes();
          var seconds=today.getSeconds();
          //如果是单位数字,前面补0
          month=month<10? "0"+month :month;
          day=day<10? "0"+day :day;
          hours=hours<10? "0"+hours :hours;
          minutes=minutes<10? "0"+minutes :minutes;
          seconds=seconds<10? "0"+seconds :seconds;
          //时间信息连成字符串
          var kk=year+"年"+month+"月"+day+"日 ";
          var now = hours+":"+minutes+":"+seconds;
          //获取id=result的内容
          var obj=document.getElementById("data");
          var hm=document.getElementById("now");
          obj.innerHTML=kk;
          hm.innerHTML=now;
          //延时器
          window.setTimeout("showTime()",1000);//自己内部调自己,递归思想,由于时钟一直存在,不需要递归出口;
        }

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

Jquery实现:引用jquery.jclock.js

注意 在引用jquery.jclock.js之前要先引用jquery

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>测试</title>

    <script src="../js/jquery-1.7.2.min.js"></script>
   <script src="../js/jquery.jclock.js"></script>

</head>
<body>

   <span id="clock"></span>

<script type="text/javascript">

     $(function(){

        $("#clock").jclock({withDate:true, withWeek:true});
 });

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

jquery.jclock.js 这个不好找;直接贴源码吧

(function($) {
  $.fn.jclock = function(options) {
    $.fn.jclock.timerID = null;
    $.fn.jclock.running = false;
    $.fn.jclock.el = null;
    var version = ‘0.1.1‘;
  // Download by http://www.codefans.net
    // options
    var opts = $.extend({}, $.fn.jclock.defaults, options);

    return this.each(function() {
      $this = $(this);
      $.fn.jclock.el = $this;

      var o = $.meta ? $.extend({}, opts, $this.data()) : opts;

      $.fn.jclock.withDate = o.withDate;
    $.fn.jclock.withWeek = o.withWeek;
    $.fn.jclock.timeNotation = o.timeNotation;
      $.fn.jclock.am_pm = o.am_pm;
      $.fn.jclock.utc = o.utc;

      $this.css({
        fontFamily: o.fontFamily,
        fontSize: o.fontSize,
        backgroundColor: o.background,
        color: o.foreground
      });

      $.fn.jclock.startClock();

    });
  };

  $.fn.jclock.startClock = function() {
    $.fn.jclock.stopClock();
    $.fn.jclock.displayTime();
  }
  $.fn.jclock.stopClock = function() {
    if($.fn.jclock.running) {
      clearTimeout(timerID);
    }
    $.fn.jclock.running = false;
  }
  $.fn.jclock.displayTime = function(el) {
  var date = $.fn.jclock.getDate();
  var week = $.fn.jclock.getWeek();
  var time = $.fn.jclock.getTime();
    $.fn.jclock.el.html(date + week + time);
    timerID = setTimeout("$.fn.jclock.displayTime()",1000);
  }
  $.fn.jclock.getDate = function() {
  if($.fn.jclock.withDate == true) {
      var now = new Date();
      var year, month, date;

      if($.fn.jclock.utc == true) {
      year = now.getUTCFullYear();
    month = now.getUTCMonth() + 1;
    date = now.getUTCDate();
    } else {
      year = now.getFullYear();
        month = now.getMonth() + 1;
        date = now.getDate();
    }

      month = ((month < 10) ? "0" : "") + month;
      date = ((date < 10) ? "0" : "") + date;

      var dateNow = year + "-" + month + "-" + date + " ";
  } else {
      var dateNow = "";
  }
    return dateNow;
  }
  $.fn.jclock.getWeek = function() {
    if($.fn.jclock.withWeek == true) {
    var now = new Date();
    var week;

    if($.fn.jclock.utc == true) {
      week = now.getUTCDay();
    } else {
      week = now.getDay();
    }

    $.each(["日","一","二","三","四","五","六"],function(i,n) {
        if(i == week) {week = n; return;}
    });

    var weekNow = "周" + week + " ";
  } else {
    var weekNow = "";
  }
  return weekNow;
  }
  $.fn.jclock.getTime = function() {
    var now = new Date();
    var hours, minutes, seconds;

    if($.fn.jclock.utc == true) {
      hours = now.getUTCHours();
      minutes = now.getUTCMinutes();
      seconds = now.getUTCSeconds();
    } else {
      hours = now.getHours();
      minutes = now.getMinutes();
      seconds = now.getSeconds();
    }

    if ($.fn.jclock.timeNotation == ‘12h‘) {
      hours = ((hours > 12) ? hours - 12 : hours);
    } else {
      hours   = ((hours <  10) ? "0" : "") + hours;
    }

    minutes = ((minutes <  10) ? "0" : "") + minutes;
    seconds = ((seconds <  10) ? "0" : "") + seconds;

    var timeNow = hours + ":" + minutes + ":" + seconds;
    if ( ($.fn.jclock.timeNotation == ‘12h‘) && ($.fn.jclock.am_pm == true) ) {
      timeNow += (hours >= 12) ? " P.M." : " A.M."
    }

    return timeNow;
};

  // plugin defaults
  $.fn.jclock.defaults = {
  withDate:false,
  withWeek:false,
    timeNotation: ‘24h‘,
    am_pm: false,
    utc: false,
    fontFamily: ‘‘,
    fontSize: ‘‘,
    foreground: ‘‘,
    background: ‘‘
  };
})(jQuery);
时间: 2024-12-28 16:54:29

时钟效果的相关文章

第35天:时钟效果

时钟效果案例 1.得到现在的时分秒2.旋转角度原理一圈360°   60s   1s/6°旋转second.style.WebkitTransform="rotate(60deg)";//每秒旋转60度 案例: 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>时钟效果</

1.1 结合date()和setTimeOut()实现简单的时钟效果

一.简单的时钟效果 首先要熟悉日期函数date()以及其常用的方法 getFullYear()  返回完整的年份 比如:2016 getMonth()  返回月份(从0开始,需+1) getDate()  返回日期 getHours()  返回小时数 getMinutes()  返回分钟数 getSeconds()  返回秒数 getDay() 返回星期几 (0-6,0是星期天,1是星期一) 写一个简单的html页面,用来展示时钟效果 <!DOCTYPE html> <html>

原生javascript实现网页显示日期时钟效果

刚接触javascript中Date内置对象时,以为这些方法都太简单了,结果要自己实际操作写一个时钟效果还真一时把我难住了,主要有几点大家要注意的.先看实际效果 要实现这样的效果 某年某月某日星期几几时几分几秒 先看代码效果 <script type="text/javascript"> window.onload=function(){ showDate(); countdown(); } function showDate(){ var myDate=new Date(

转CSS3+js实现多彩炫酷旋转圆环时钟效果

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>多彩炫酷环形时钟效果</title> <style type="text/css"> #fancyClock{ margin:40px auto; he

transform实现的时钟效果

又来一个时钟效果了,这个的实现不需要canvas,都是div.ul.li画出的,好玩有真实. 哈哈~ 需要的js才能实现到走动这个效果,但js的内容不多,也不难. 主要是一个css里transform的使用的思路,transform里有很多变幻属性,而普通的时钟 在我心中就是个圆圆的东西,那么是不是可以旋转这个属性(rotate)实现了,它的刻度 使用旋转且把旋转点设置在圆心,那不就可以绕着圆心转了吗,而时针它们的底部不是和 圆心接触的吗,那么设置时针的底部为旋转点不就OK了,大概的说了说思路.

ios开发核心动画三:隐式动画与时钟效果

一:隐式动画 #import "ViewController.h" @interface ViewController () /** <#注释#> */ @property (nonatomic, weak) CALayer *layer; @property (weak, nonatomic) IBOutlet UIView *redView; @end @implementation ViewController - (void)viewDidLoad { [super

canvas实现的时钟效果

最近在网上看到了一个css3实现的可爱时钟,觉得很nice,然后就想着用canvas试试实现这个时钟效果. 首先,要实现时钟需要先计算时钟上的数字应该占整个圆的大小. 因为一个圆是360度,所以数字之间就是360/12=30度,所以我在圆上画出数字的代码是这样的: drawNumber:function(){ this.context.save(); for(var i = 0;i < 12; i++){ var angle = i*30; angle = angle*Math.PI/180;

java-swing模拟实现时钟效果

1 import java.awt.Color; 2 import java.awt.Font; 3 import java.awt.Graphics; 4 import java.util.Calendar; 5 import java.util.GregorianCalendar; 6 7 import javax.swing.JFrame; 8 import javax.swing.JPanel; 9 10 /** 11 * 12 * @author YYCat 13 * 14 */ 15

代码写响应式时钟效果

只需3个文件,就可以实现钟表效果,还是先看图再来看代码效果 时间是用的北京时间 再来看它的源代 html文件 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml&qu

使用canvas实现数字时钟效果

<canvas id="canvas"></canvas> <script> (function(){ var digit= [ [ [0,0,1,1,1,0,0], [0,1,1,0,1,1,0], [1,1,0,0,0,1,1], [1,1,0,0,0,1,1], [1,1,0,0,0,1,1], [1,1,0,0,0,1,1], [1,1,0,0,0,1,1], [1,1,0,0,0,1,1], [0,1,1,0,1,1,0], [0,0,1,