1.时间戳转时间
参数:
timeStamp (Number | String)
10位或13位时间戳
format (String)
所需要转换的时间格式,format是一串可被替换的字符串,完全体是这样的"Y-M-D h:m:s:ms"
,其中Y-M-D代表年月日,h:m:s:ms代表时分秒和毫秒。你可以根据你的需要来自行调整格式,例如"Y/M/D"年月日,或者只需要时分秒"h:m:s"。
示例:
timeStampToTime('1651732198148','h:m:s') //14:29:58
timeStampToTime('1651732198148','s.ms') //58.148
timeStampToTime('1651732198148','Y/M/D') //2022-05-05
function timeStampToTime(timeStamp,format){
if(typeof timeStamp != 'number') timeStamp = Number(timeStamp)
if(String(timeStamp).length==10){
timeStamp = Number(timeStamp+'000')
}
var date = new Date(timeStamp)
return format.replace(/ms/g,('00'+date.getMilliseconds()).slice(-3))
.replace(/s/g,('0'+date.getSeconds()).slice(-2))
.replace(/m/g,('0'+date.getMinutes()).slice(-2))
.replace(/h/g,('0'+date.getHours()).slice(-2))
.replace(/D/g,('0'+date.getDate()).slice(-2))
.replace(/M/g,('0'+(date.getMonth()+1)).slice(-2))
.replace(/Y/g,date.getFullYear())
}
2.文案复制
参数:
content (String)
所需复制的字符串
返回值:
(Boolean)
返回值是个布尔值,用于判断是否复制成功
function copyToClip(content) {
try{
var aux = document.createElement('input');
aux.setAttribute('value', content);
document.body.appendChild(aux);
aux.select();
document.execCommand('copy');
document.body.removeChild(aux);
return true
}catch(err){
return false
}
}
3.带过渡效果的返回顶部
function toTop(){
loop()
var top;
var stop = false
function loop(){
if(top<1 || stop) {
if(!stop) document.documentElement.scrollTop = 0
return
}
top = document.documentElement.scrollTop
document.documentElement.scrollTop = top*0.9
setTimeout(()=>{
loop()
},15)
}
document.onscroll = function (e){
if(top<document.documentElement.scrollTop){
stop = true
}
}
}