時鐘的css和js,css做時鐘

本文目錄一覽:

求css動態時鐘以及日曆的代碼(運行能用的,沒用的不要寫上來了,謝謝)(與電腦時間同步)

我這裡有一段js動態時鐘的代碼,日曆的比較複雜,沒有研究:

scripttype=”text/javascript”

setInterval(function() {

var week;

var date;

var today = new Date();

var year = today.getFullYear();

var month = today.getMonth() + 1;

var day = today.getDate();

var ss = today.getDay();

var hours = today.getHours();

var minutes = today.getMinutes();

var seconds = today.getSeconds();

date = year + “年” + month +”月” + day + “日 “;

if (ss == 0) week = “星期日”;

if (ss == 1) week = “星期一”;

if (ss == 2) week = “星期二”;

if (ss == 3) week = “星期三”;

if (ss == 4) week = “星期四”;

if (ss == 5) week = “星期五”;

if (ss == 6) week = “星期六”;

if (minutes = 9) minutes =”0″ + minutes;

if (seconds = 9) seconds =”0″ + seconds;

document.getElementById(‘p_time’).innerHTML = “今天是:” + date

+ week + ” ” + hours + “:” + minutes + “:” +

seconds;

},

1000);

/script

這裡一定要注意,html里要有一個id為p_time的div。

怎樣用html和css做時鐘的轉動效果

css 有個 animation 可以實現動畫,僅僅是動起來,沒法實現實時與系統對時(需要js)

60秒跳動60次旋轉360度。(可以使用linear 線性運動)

# animation:anim_mm 60s linear infinite;

animation:mm 60s steps(60) infinite;

@keyframes mm{

to{ transform:rotate(360deg) ;}

}

如何用jQuery和CSS3製作數字時鐘

這個時鐘不需要很多HTML,這是因為它很大的一部分,像工作日的名稱和數字都是動態生成的。 下面是你需要在你頁面上使用時鐘時要有的標籤:

index.html

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

主元素為#clock的div,包含.display的div,用於容納平日列表、AM/PM標記、鬧鈴和時間。 下面代碼為每個數字生成一個標籤:

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

.digits元素包含6個像這樣帶span的div,每個div為時鐘的一個數字。就像你在上面片段中所見到的一樣,這些div擁有一個從0到9的樣式名稱,並且包含7個帶獨立樣式的span元素,這些span是數字的一部分,像老的數字時鐘一樣:

數字說明

它們完全用CSS樣式渲染且默認設置為 opacity:0 。定義在它們父div上的樣式將決定它們的可見性。下面是數字「0」的CSS:

assets/css/styles.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;

}

除了中間一個,所有的片斷都是可見的,我已經向所有的這些span添加了CSS3轉換屬性,當在數字之間切換時出現漸變效果。

樣式表裡有很多其他CSS,我不再這列舉。我相信最好的方式去學習CSS如何工作就是在Firebug、Chrome的審查器或你瀏覽器里的開發者工具里即時審查demo的代碼。

黑色主題

jQuery 代碼

要想要時鐘工作,我們將使用jQuery生成每個數字的標籤,並且設置一個定時器每秒鐘更新一次樣式,為了更簡單,我們使用moment.js 庫(快速開始) 來補償JavaScript原生日期和時間方法的缺陷。

assets/js/script.js

$(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; i8; 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’);

});

});

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/153164.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-14 03:03
下一篇 2024-11-14 03:03

相關推薦

  • JS Proxy(array)用法介紹

    JS Proxy(array)可以說是ES6中非常重要的一個特性,它可以代理一個數組,監聽數據變化並進行攔截、處理。在實際開發中,使用Proxy(array)可以方便地實現數據的監…

    編程 2025-04-29
  • 解析js base64並轉成unit

    本文將從多個方面詳細介紹js中如何解析base64編碼並轉成unit格式。 一、base64編碼解析 在JavaScript中解析base64編碼可以使用atob()函數,它會將b…

    編程 2025-04-29
  • Node.js使用Body-Parser處理HTTP POST請求時,特殊字符無法返回的解決方法

    本文將解決Node.js使用Body-Parser處理HTTP POST請求時,特殊字符無法返回的問題。同時,給出一些相關示例代碼,以幫助讀者更好的理解並處理這個問題。 一、問題解…

    編程 2025-04-29
  • t3.js:一個全能的JavaScript動態文本替換工具

    t3.js是一個非常流行的JavaScript動態文本替換工具,它是一個輕量級庫,能夠很容易地實現文本內容的遞增、遞減、替換、切換以及其他各種操作。在本文中,我們將從多個方面探討t…

    編程 2025-04-28
  • CSS sans字體家族

    CSS sans字體家族是一組基於CSS的無襯線字體,具有在不同設備和瀏覽器上保持一致的特性。本文將從優勢、使用、自定義等多個方面對CSS sans字體家族進行詳細介紹。 一、優勢…

    編程 2025-04-28
  • JS圖片沿着SVG路徑移動實現方法

    本文將為大家詳細介紹如何使用JS實現圖片沿着SVG路徑移動的效果,包括路徑製作、路徑效果、以及實現代碼等內容。 一、路徑製作 路徑的製作,我們需要使用到SVG,SVG是可縮放矢量圖…

    編程 2025-04-27
  • 如何使用JS調用Python腳本

    本文將詳細介紹通過JS調用Python腳本的方法,包括使用Node.js、Python shell、child_process等三種方法,以及在Web應用中的應用。 一、使用Nod…

    編程 2025-04-27
  • 如何反混淆美團slider.js

    本文將從多個方面詳細闡述如何反混淆美團slider.js。在開始之前,需要明確的是,混淆是一種保護JavaScript代碼的方法,其目的是使代碼難以理解和修改。因此,在進行反混淆操…

    編程 2025-04-27
  • Python要學JS嗎?

    Python和JavaScript都是非常受歡迎的編程語言。然而,你可能會問,既然我已經學了Python,是不是也需要學一下JS呢?在本文中,我們將圍繞這個問題進行討論,並從多個角…

    編程 2025-04-27
  • 解決js ajax post 419問題

    對於使用ajax post請求時出現的419問題,我們需要進行以下幾個方面的闡述,包括返回碼的含義、可能出現的情況、解決方案等內容。 一、解析419返回碼 419返回碼錶示用戶超時…

    編程 2025-04-27

發表回復

登錄後才能評論