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

原生js日历控件年月日时间选择器代码

单击input文本框弹出日历弹出层,可点选指定日期,还可:清空、选择今日等。
  日期时间
 9234  0

jquery点击input文本框弹出年份选择器插件

YearPicker.js是jQuery的一个轻量级但可自定义的年份选择器,点击input文本框弹出层,带年份左右箭头切换可选择。
  日期时间
 7246  0

jquery多种模式下日期时间选择器插件

一款layui出品的laydate时间日期选择器,包含多种模式触发弹出选择器窗口。
  日期时间
 4233  0

jquery轻量级秒表计时器插件

该插件有两种不同的操作方式,一种是可点击复位归零;另一种为从外部更新倒计时。
  日期时间
 9245  0

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

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