javascript基于SVG创建的复古的摇摆时钟特效代码

所属分类: 网页特效-日期时间    2024-01-13 11:28:11

javascript基于SVG创建的复古的摇摆时钟特效代码 ie兼容6
反馈问题  查看演示  登录后下载 温馨提示
登录会员即可享受免费下载
 我要建站

javascript基于SVG创建的复古的摇摆时钟特效代码(共3个文件)

    • index.html

使用方法

/**[minimum JS only to set current time and sound the chimes]**/
(function() {
	'use strict';
	let	seconds = document.getElementById('second_time'),
			munutes = document.getElementById('minute_time'),
			hours = document.getElementById('hour_time'),
		 	s, m, h, S, M, H, time;
	
	/*[set the clock to correct time and let CSS handle the rest]*/
	function windup () {
		time = new Date(),
		s = time.getSeconds(),
		m = time.getMinutes(),
		h = time.getHours(),
		S = s * 6,
		M = m * 6 + s / 10,
		H = h * 30 + 0.5 * m;

		seconds.style.transform = 'rotate(' + S + 'deg)';
		munutes.style.transform = 'rotate(' + M + 'deg)';
		hours.style.transform = 'rotate(' + H + 'deg)';
		
		console.log('windup: ' + h + ':' + m + ':' + s);
		tick.volume = chime.volume = 1;
	}
	setTimeout(windup, 0);
	
	/*[main visibility API function]*/
	// use visibility API to check if current tab is active or not
	let vis = (function(){
		 let stateKey, 
			  eventKey, 
			  keys = {
						 hidden: 'visibilitychange',
						 webkitHidden: 'webkitvisibilitychange',
						 mozHidden: 'mozvisibilitychange',
						 msHidden: 'msvisibilitychange'
		 };
		 for (stateKey in keys) {
			  if (stateKey in document) {
					eventKey = keys[stateKey];
					break;
			  }
		 }
		 return function(c) {
			  if (c) document.addEventListener(eventKey, c);
			  return !document[stateKey];
		 }
	})();
	
	/*[HTML5 Visibility API]****************************************/
	// check if current tab is active or not
	vis( function () {
		if (vis()) {	
		setTimeout( function () {
					// tween resume() code goes here
					windup();
					console.log('tab is visible - has focus');
			  }, 300);		
		 } else {
			  // tween pause() code goes here
			  console.log('tab is invisible - has blur');
			 tick.volume = chime.volume = 0.1;
		 }
	});
	
	// check if browser window has focus		
	let notIE = (document.documentMode === undefined),
		 isChromium = window.chrome;
	if (notIE && !isChromium) {
		 // checks for Firefox and other  NON IE Chrome versions
		 $(window).on('focusin', function () { 
			  // tween resume() code goes here
			  setTimeout( function () {  
					windup();
					console.log('focus');
			  }, 300);
		 }).on('focusout', function () {
			  // tween pause() code goes here
			  console.log('blur');
			 tick.volume = chime.volume = 0.1;
		 });
	} else {
		 // checks for IE and Chromium versions
		 if (window.addEventListener) {
			  // bind focus event
			  window.addEventListener('focus', function (event) {
					// tween resume() code goes here
					setTimeout( function () {
						windup();
						console.log('focus');
					},300);
			  }, false);
			  // bind blur event
			  window.addEventListener('blur', function (event) {
					// tween pause() code goes here
					 console.log('blur');
				  tick.volume = chime.volume = 0.1;
			  }, false);
		 } else {
			  // bind focus event
			  window.attachEvent('focus', function (event) {
					// tween resume() code goes here
					setTimeout( function () {
						windup();
						console.log('focus');
					},300);
			  });
			  // bind focus event
			  window.attachEvent('blur', function (event) {
					// tween pause() code goes here
					console.log('blur');
				   tick.volume = chime.volume = 0.1;
			  });
		 }
	}
	/*[end HTML5 Visibility API]************************************/
	
	/*[hourly and quarterly chimes]*/
	const tick = document.getElementById('tick');
	const chime = document.getElementById('chime');
	const sound_dir = 'http://www.gerasimenko.com/sandbox/codepen/sounds/';

	let bell, tock = 'tock.wav';

	tick.src = sound_dir + tock;

	function hourly_chime(n) {
		console.log('plays left: ' + n);
		if (n === 0) {
			return;
		} else {
			chime.pause();
			chime.currentTime = 0;
			chime.play();
			n--;
			setTimeout( function () {
				hourly_chime(n);
			}, 3000);
		}
	}

	setInterval( function () {
		time = new Date(),
		s = time.getSeconds(),
		m = time.getMinutes(),
		h = time.getHours();
		
		console.log('watch: ' + h + ':' + m + ':' + s);
		tick.play();

		if (s === 0 && (m === 15 || m === 30 || m === 45)) {
			bell = 'ding-tone.wav';
			chime.src = sound_dir + bell;
			hourly_chime(m / 15);
		} else if (s === 0 && m === 0) {
			bell = 'bell-japanese.wav';
			chime.src = sound_dir + bell;
			h > 12 ? h = h - 12 : h;
			hourly_chime(h);
		}
	}, 1000);
})();

站长提示:
1. 平台上所有素材资源,需注册登录会员方能正常下载。
2. 会员用户积极反馈网站、素材资源BUG或错误问题,每次奖励20K币
3. PHP源码类素材,如需协助安装调试,或你有二次开发需求,可联系苦力吧客服。
4. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服
相关资源 / 日期时间

jquery轻量级网页在线倒计时时钟插件

一个极小的网页倒计时时钟插件,可自定义设置倒计时:天数、小时、分钟、秒数。
  日期时间
 157  

jquery响应式点击弹出可选日期范围的日期选择器

一款可选范围的日期选择器插件,鼠标点击input文本框,弹出日期选择器,可选择日期或日期范围。
  日期时间
 119  

jquery文本框可手动输入可弹出框点选的日期时间选择器插件

一款input文本框日期选择器特效,可以手动分段输入,还可以点选右侧按钮,触发日期时间弹出层选择。
  日期时间
 141  

jquery弹出层时间选择器插件

timeDurationPicker是基于jQuery UI编写的时间选择器,可选择年、月、天、小时、分钟和秒,并赋值显示到DIV元素上。
  日期时间
 148  

评论数(0) 回复有机会获得K币 用户协议

^_^ 还没有人评论,快来抢个沙发!
😀
  • 😀
  • 😊
  • 😂
  • 😍
  • 😑
  • 😷
  • 😵
  • 😛
  • 😣
  • 😱
  • 😋
  • 😎
  • 😵
  • 😕
  • 😶
  • 😚
  • 😜
  • 😭
发表评论