4、Web API |
您所在的位置:网站首页 › dom操作添加节点 › 4、Web API |
一、日期对象
1.1 实例化
目标:能够实例化日期对象 在代码中发现了 new 关键字时,一般将这个操作称为实例化创建一个时间对象并获取时间![]() 目标:能够使用日期对象中的方法写出常见日期 使用场景:因为日期对象返回的数据我们不能直接使用,所以需要转换为实际开发中常用的格式 需求:将当前时间以:YYYY-MM-DD HH:mm 形式显示在页面 2008-08-08 08:08 案例分析: ①:调用日期对象方法进行转换 ②:记得数字要补0 ③:字符串拼接后,通过 innerText 给 标签 div{ width: 300px; height: 40px; border: 1px solid pink; text-align: center; line-height: 40px; } const div = document.querySelector('div') function getMyDate() { const date = new Date() let h = date.getHours() let m = date.getMinutes() let s = date.getSeconds() h = h < 10 ? '0'+ h : h m = m < 10 ? '0'+ m : m s = s < 10 ? '0'+ s : s return `今天是:${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日 ${h}:${m}:${s}` } div.innerHTML = getMyDate() setInterval(function(){ div.innerHTML = getMyDate() },1000)时间的另一种方法(简单) const div = document.querySelector('div') // 得到日期对象 const date = new Date() div.innerHTML = date.toLocaleString() //2023/6/23 11:47:04 // div.innerHTML = date.toLocaleDateString() //2023/6/23 // div.innerHTML = date.toLocaleTimeString() //11:49:14 setInterval(function(){ const date = new Date() div.innerHTML = date.toLocaleString() },1000) 1.3 时间戳目标:能够获得当前时间戳 使用场景: 如果计算倒计时效果,前面方法无法直接计算,需要借助于时间戳完成什么是时间戳: 是指1970年01月01日00时00分00秒起至现在的毫秒数,它是一种特殊的计量时间的方式算法: 将来的时间戳 - 现在的时间戳 = 剩余时间毫秒数 剩余时间毫秒数 转换为 剩余时间的 年月日时分秒 就是 倒计时时间 比如 将来时间戳 2000ms - 现在时间戳 1000ms = 1000ms 1000ms 转换为就是 0小时0分1秒![]() 需求:计算到下课还有多少时间 案例分析: ①:用将来时间减去现在时间就是剩余的时间 ②:核心: 使用将来的时间戳减去现在的时间戳 ③:把剩余的时间转换为 天 时 分 秒 注意: 通过时间戳得到是毫秒,需要转换为秒在计算转换公式: d = parseInt(总秒数/ 60/60 /24); // 计算天数 h = parseInt(总秒数/ 60/60 %24) // 计算小时 m = parseInt(总秒数 /60 %60 ); // 计算分数 s = parseInt(总秒数%60); // 计算当前秒数 Document .countdown { width: 240px; height: 305px; text-align: center; line-height: 1; color: #fff; background-color: brown; /* background-size: 240px; */ /* float: left; */ overflow: hidden; } .countdown .next { font-size: 16px; margin: 25px 0 14px; } .countdown .title { font-size: 33px; } .countdown .tips { margin-top: 80px; font-size: 23px; } .countdown small { font-size: 17px; } .countdown .clock { width: 142px; margin: 18px auto 0; overflow: hidden; } .countdown .clock span, .countdown .clock i { display: block; text-align: center; line-height: 34px; font-size: 23px; float: left; } .countdown .clock span { width: 34px; height: 34px; border-radius: 2px; background-color: #303430; } .countdown .clock i { width: 20px; font-style: normal; }今天是2222年6月1日 下班倒计时 00 : 25 : 20 18:30:00下课 // 获得今天时间的标签 const next = document.querySelector('.next') // 写今天的时间函数 function today() { const date = new Date() return `今天是${date.getFullYear()}年${date.getMonth()+1}月${date.getDate()}日` } // console.log(today()) next.innerHTML = today() // 随机颜色函数 // 1. 自定义一个随机颜色函数 function getRandomColor(flag = true) { if (flag) { // 3. 如果是true 则返回 #ffffff let str = '#' let arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'] // 利用for循环随机抽6次 累加到 str里面 for (let i = 1; i Slide 1 Slide 2 Slide 3 Slide 4 Slide 5 Slide 6 Slide 7 Slide 8 Slide 9 var swiper = new Swiper(".mySwiper", { pagination: { el: ".swiper-pagination", }, //自动播放 autoplay: { delay: 1000,//1秒切换一次 disableOnInteraction: false, //鼠标触摸之后继续自动播放 }, // 用键盘控制 keyboard: { enabled: true, onlyInViewport: true, }, }); 五、综合案例 1.游乐园轮播图 2.学生信息表案例
|
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |