点击进入更详细教程及源码下载     在线演示

我们经常会在网站中看见一个时钟的效果。今天向大家分享一个使用jQuery和CSS3实现一个数字时钟教程

 
代码:
 
<div id="clock" class="light">
    <div class="display">
        <div class="weekdays"></div>
        <div class="ampm"></div>
        <div class="alarm"></div>
        <div class="digits"></div>
    </div>
</div>
<div class="zero">
    <span class="d1"></span>
    <span class="d2"></span>
    <span class="d3"></span>
    <span class="d4"></span>
    <span class="d5"></span>
    <span class="d6"></span>
    <span class="d7"></span>
</div>
 
CSS
 
/* 0 */
 
#clock .digits div.zero .d1,
#clock .digits div.zero .d3,
#clock .digits div.zero .d4,
#clock .digits div.zero .d5,
#clock .digits div.zero .d6,
#clock .digits div.zero .d7{
    opacity:1;
}
 
jquery code
 
$(function(){
 
    // Cache some selectors
 
    var clock = $('#clock'),
        alarm = clock.find('.alarm'),
        ampm = clock.find('.ampm');
 
    // Map digits to their names (this will be an array)
    var digit_to_name = 'zero one two three four five six seven eight nine'.split(' ');
 
    // This object will hold the digit elements
    var digits = {};
 
    // Positions for the hours, minutes, and seconds
    var positions = [
        'h1', 'h2', ':', 'm1', 'm2', ':', 's1', 's2'
    ];
 
    // Generate the digits with the needed markup,
    // and add them to the clock
 
    var digit_holder = clock.find('.digits');
 
    $.each(positions, function(){
 
        if(this == ':'){
            digit_holder.append('<div class="dots">');
        }
        else{
 
            var pos = $('<div>');
 
            for(var i=1; i<8; i++){
                pos.append('<span class="d' + i + '">');
            }
 
            // Set the digits as key:value pairs in the digits object
            digits[this] = pos;
 
            // Add the digit elements to the page
            digit_holder.append(pos);
        }
 
    });
 
    // Add the weekday names
 
    var weekday_names = 'MON TUE WED THU FRI SAT SUN'.split(' '),
        weekday_holder = clock.find('.weekdays');
 
    $.each(weekday_names, function(){
        weekday_holder.append('<span>' + this + '</span>');
    });
 
    var weekdays = clock.find('.weekdays span');
 
    // Run a timer every second and update the clock
 
    (function update_time(){
 
        // Use moment.js to output the current time as a string
        // hh is for the hours in 12-hour format,
        // mm - minutes, ss-seconds (all with leading zeroes),
        // d is for day of week and A is for AM/PM
 
        var now = moment().format("hhmmssdA");
 
        digits.h1.attr('class', digit_to_name[now[0]]);
        digits.h2.attr('class', digit_to_name[now[1]]);
        digits.m1.attr('class', digit_to_name[now[2]]);
        digits.m2.attr('class', digit_to_name[now[3]]);
        digits.s1.attr('class', digit_to_name[now[4]]);
        digits.s2.attr('class', digit_to_name[now[5]]);
 
        // The library returns Sunday as the first day of the week.
        // Stupid, I know. Lets shift all the days one position down,
        // and make Sunday last
 
        var dow = now[6];
        dow--;
 
        // Sunday!
        if(dow < 0){
            // Make it last
            dow = 6;
        }
 
        // Mark the active day of the week
        weekdays.removeClass('active').eq(dow).addClass('active');
 
        // Set the am/pm text:
        ampm.text(now[7]+now[8]);
 
        // Schedule this function to be run again in 1 sec
        setTimeout(update_time, 1000);
 
    })();
 
    // Switch the theme
 
    $('a.button').click(function(){
        clock.toggleClass('light dark');
    });
 
});

使用jQuery和CSS3实现一个数字时钟的更多相关文章

  1. Python练习——约瑟夫环问题、用类方法描述一个数字时钟

    一.约瑟夫环问题 有15个基督徒和15个非基督徒在海上遇险,为了能让一部分人活下来不得不将其中15个人扔到海里面去,有个人想了个办法就是大家围成一个圈,由某个人开始从1报数,报到9的人就扔到海里面,他 ...

  2. 使用jQuery和CSS3制作数字时钟(jQuery篇) 附源码下载

    HTML 和上一篇文章:使用jQuery和CSS3制作数字时钟(CSS3篇)一样的HTML结构,只是多了个>date用来展示日期和星期的. <div id="clock" ...

  3. JS制作一个创意数字时钟

    通过js代码制作一个创意数字时钟 通过JS代码实现创意数字时钟效果如下:由数字化的卡通形象图片取代常规的数字显示当前实时北京时间.具体效果示例: 核心重点: (1)Date方法的初步了解 (2)构建模 ...

  4. 一个简易的 LED 数字时钟实现方法

    这个应该是已经有很多人做过的东西,我应该只是算手痒,想写一下,所以,花了点时间折腾了这个,顺便把 Dark Mode 的处理也加上了. 首先可以很明确的一点,这个真没技术含量存在,只是需要点耐心. L ...

  5. jquery和css3实现滑动导航菜单

    效果预览:http://keleyi.com/keleyi/phtml/html5/15/ 有都中颜色可供选择,请使用支持HTML5/CSS3的浏览器访问. HTML源代码: <!DOCTYPE ...

  6. 使用jQuery和CSS3生成的搜索框变形全屏搜索效果

    在线演示 本地下载 使用jQuery和CSS3过渡变形效果生成的一个搜索框变形效果实现,可以帮助你更好利用页面格式和内容.实验性质的代码,请大家在产品环境里自己修改使用!

  7. C#开发漂亮的数字时钟

    今天用C#做了一个漂亮的数字时钟.界面如下. 实现技术:主要是通过Graphics类的DrawImage方法来绘制数字时钟中所有的数字,这些数字是从网上找的一些图片文件.时钟使用DateTime中No ...

  8. 一个模拟时钟的时间选择器 ClockPicker

    最近开发的一个模拟时钟的时间选择器 ClockPicker,用于 Bootstrap,或者单独作为一个 jQuery 插件. 源代码托管在 GitHub 上: ClockPicker 最近项目中需要用 ...

  9. 使用jQuery和css3实现了仿淘宝ued博客左边的菜单切换动画

    今天看到淘宝ued博客的左侧导航菜单的动画好,要使用jQuery和css3我做一个简单的示例,主要用途是实现jQuery 事件和css3 transition属性. 个元素来实现鼠标滑动到某个导航的背 ...

随机推荐

  1. 13.翻译系列:Code-First方式配置多对多关系【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code- ...

  2. 583. Delete Operation for Two Strings

    Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 t ...

  3. JavaScript中关于页面URL地址的获取

    1 window.location.host; //返回url的主机部分,例如 www.xxx.com window.location.hostname; //返回url的主机名,例如 www.xxx ...

  4. Python3基础语法你学会了么

      编码 默认:源码文件以UTF-8编码,字符串都是unicode字符串 指定:   标识符 第一个字符:字母表中的字符或下划线 _ 其它部分:由字母.数字.下划线 _ 组成 大小写敏感 python ...

  5. HDFS环境搭建(单节点配置)

    [参考文章]:hadoop集群搭建(hdfs) 1. Hadoop下载 官网下载地址: https://hadoop.apache.org/releases.html,进入官网根据自己需要下载具体的安 ...

  6. thinkpad的E480安装ubuntu后wifi无法使用问题解决

    买了新电脑,安装ubuntu新系统之后,遇到了一个比较麻烦的问题,在ubuntu中,无法使用wifi. 用新产品就是要当小白鼠啊,查了一下资料,发现这个使用的rtl8821ce的wifi芯片,该wif ...

  7. 90 % Java 程序员被误导的一个性能优化策略

    我们经常看到一些 Java 性能优化的书或者理念,说不要在循环内定义变量,这样会占用过多的内存影响性能,而要在循环外面定义.接触 Java 这么久以来,相信很多 Java 程序员都被这种代码性能优化策 ...

  8. Mysql配置主从同步的基本步骤

    # 配置主从同步的基本步骤 #总结为如下的步骤: # .在主服务器上,必须开启二进制日志机制和配置一个独立的ID # .在每一个从服务器上,配置一个唯一的ID,创建一个用来专门复制主服务器数据的账号 ...

  9. cannot download, /home/azhukov/go is a GOROOT, not a GOPATH

    问题详情: go环境安装好后,运行go代码也没有问题 下载govendor包的时候提示: cannot download, /home/azhukov/go is a GOROOT, not a GO ...

  10. 深入理解kafka设计原理

    最近开研究kafka,下面分享一下kafka的设计原理.kafka的设计初衷是希望作为一个统一的信息收集平台,能够实时的收集反馈信息,并需要能够支撑较大的数据量,且具备良好的容错能力. 1.持久性 k ...