前端常用代码整理(不断更新中)
文章目录 一. js1.随机函数代码2.倒计时代码3.精确显示时间代码4.表单删除常用代码5.排他思想核心代码6.轮播图案例核心代码 一. js 1.
文章目录
- 一. js
- 1.随机函数代码
- 2.倒计时代码
- 3.精确显示时间代码
- 4.表单删除常用代码
- 5.排他思想核心代码
- 6.轮播图案例核心代码
一. js
1.随机函数代码
function getRandom(min, max) {return Math.floor(Math.random() * (max - min + 1)) + min}
2.倒计时代码
let now = +new Date()let last = +new Date('这里写想要达到的时间')let count = (last - now) / 1000let h = parseInt(count / 60 / 60 % 24)h = h < 10 ? '0' + h : hlet m = parseInt(count / 60 % 60)m = m < 10 ? '0' + m : mlet s = parseInt(count % 60);s = s < 10 ? '0' + s : s
3.精确显示时间代码
let arr = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']let div = document.querySelector('div')getTime()setInterval(getTime, 1000)function getTime() {let date = new Date()let year = date.getFullYear()let month = date.getMonth() + 1let date1 = date.getDate()let hour = date.getHours()let min = date.getMinutes()let sec = date.getSeconds()let day = date.getDay()div.innerHTML = `今天是: ${year}年${month}月${date1}日 ${hour}:${min}:${sec} ${arr[day]}`}
4.表单删除常用代码
tbody.addEventListener('click', function (e) {if (e.target.tagName === 'A') {arr.splice(e.target.id, 1)render()}})
5.排他思想核心代码
let items = document.querySelectorAll('.item')for (let i = 0; i < items.length; i++) {items[i].addEventListener('click', function () {document.querySelector('.aside .active').classList.remove('active')this.classList.add('active')})}
6.轮播图案例核心代码
for (let i = 0; i < lis.length; i++) {lis[i].addEventListener('mouseenter', function () {document.querySelector('.indicator .active').classList.remove('active')this.classList.add('active')document.querySelector('.slides ul .active').classList.remove('active')piclis[i].classList.add('active')text.innerHTML = `第${i + 1}张图的描述信息`index = i})}let index = 0 next.addEventListener('click', function () {index++index = index % lis.lengthcommon()})prev.addEventListener('click', function () {index--if (index < 0) {index = lis.length - 1}common()})function common() {document.querySelector('.indicator .active').classList.remove('active')lis[index].classList.add('active')document.querySelector('.slides ul .active').classList.remove('active')piclis[index].classList.add('active')text.innerHTML = `第${index + 1}张图的描述信息`}let timer = setInterval(function () {next.click()}, 1000)main.addEventListener('mouseenter', function () {clearInterval(timer)})main.addEventListener('mouseleave', function () {timer = setInterval(function () {next.click()}, 1000)})
版权声明
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处。如若内容有涉嫌抄袭侵权/违法违规/事实不符,请点击 举报 进行投诉反馈!