時間代碼格式化工具函數的封裝
小伙伴們,多封點工具函數,多封裝點公共組件,多寫點公共樣式,照顧下互聯網行業的新人把。。。。~~~~~
/** yyyymmdd(new Date) -> "2018-07-23" */
export function yyyymmdd(date, delimiter = '-') { // 曝光函數出去 在需要用得地方引入這個文件 { yyyymmdd } 這種方式引入 yyyymmdd() // 直接傳入參數
const yyyy = date.getFullYear().toString();
const mm = (date.getMonth() + 1).toString();
const dd = date.getDate().toString();
return yyyy + delimiter + (mm[1] ? mm : `0${mm[0]}`)
+ delimiter + (dd[1] ? dd : `0${dd[0]}`);
}
滾動動畫函數
export function scroll_to(el, from = 0, to, duration = 500) {
function callback() {
return window.setTimeout(callback, 1000 / 60);
}
if (!window.requestAnimationFrame) {
window.requestAnimationFrame = (
window.webkitRequestAnimationFrame
|| window.mozRequestAnimationFrame
|| window.msRequestAnimationFrame
|| callback
);
}
const difference = Math.abs(from - to);
const scroll_step = Math.ceil(difference / duration * 50);
function scroll(start, end, step) {
if (start === end) {
return;
}
let d = (start + step > end) ? end : start + step;
if (start > end) {
d = (start - step < end) ? end : start - step;
}
if (el === window) {
window.scrollTo(d, d);
} else {
el.scrollTop = d;
}
window.requestAnimationFrame(() => scroll(d, end, step));
}
scroll(from, to, scroll_step);
}
小編不容易點個贊再走把