本文目錄一覽:
求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-hant/n/153164.html