hexo-theme-butterfly/source/js/utils.js
Jerry eb11e4afeb 🐛 修復POST-META關閉閲讀分鐘和訪問量後,評論量前有分割線的bug
🐛 修復canvas_ribbon透明度變為1再變回預設值bug
🐛 修復當default_cover沒設置時,會出現無圖片的bug
🎨 部分js調整
2020-04-02 22:16:01 +08:00

127 lines
3.1 KiB
JavaScript

function debounce (func, wait, immediate) {
var timeout
return function () {
var context = this
var args = arguments
var later = function () {
timeout = null
if (!immediate) func.apply(context, args)
}
var callNow = immediate && !timeout
clearTimeout(timeout)
timeout = setTimeout(later, wait)
if (callNow) func.apply(context, args)
}
};
function throttle (func, wait, options) {
var timeout, context, args
var previous = 0
if (!options) options = {}
var later = function () {
previous = options.leading === false ? 0 : new Date().getTime()
timeout = null
func.apply(context, args)
if (!timeout) context = args = null
}
var throttled = function () {
var now = new Date().getTime()
if (!previous && options.leading === false) previous = now
var remaining = wait - (now - previous)
context = this
args = arguments
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout)
timeout = null
}
previous = now
func.apply(context, args)
if (!timeout) context = args = null
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining)
}
}
return throttled
}
function sidebarPaddingR () {
var innerWidth = window.innerWidth
var clientWidth = document.body.clientWidth
var paddingRight = innerWidth - clientWidth
if (innerWidth !== clientWidth) {
$('body').css('padding-right', paddingRight)
}
}
// iPadOS
function isIpad () {
return navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1
}
function isTMobile () {
var ua = navigator.userAgent
var pa = /iPad|iPhone|iPod|Android|Opera Mini|BlackBerry|webOS|UCWEB|Blazer|PSP|IEMobile|Symbian/g
return window.screen.width < 992 && pa.test(ua)
}
function isMobile () {
return this.isIpad() || this.isTMobile()
}
function isDesktop () {
return !this.isMobile()
}
function scrollTo (name) {
var scrollOffset = $(name).offset()
$('body,html').animate({
scrollTop: scrollOffset.top
})
};
function loadScript (url, callback) {
var script = document.createElement('script')
script.type = 'text/javascript'
if (script.readyState) { // IE
script.onreadystatechange = function () {
if (script.readyState === 'loaded' ||
script.readyState === 'complete') {
script.onreadystatechange = null
callback()
}
}
} else { // Others
script.onload = function () {
callback()
}
}
script.src = url
document.body.appendChild(script)
};
function snackbarShow (text, showAction, duration) {
var a = (typeof showAction !== 'undefined') ? showAction : false
var d = (typeof duration !== 'undefined') ? duration : 2000
var position = GLOBAL_CONFIG.Snackbar.position
var bg = document.documentElement.getAttribute('data-theme') === 'light' ? GLOBAL_CONFIG.Snackbar.bgLight : GLOBAL_CONFIG.Snackbar.bgDark
Snackbar.show({
text: text,
backgroundColor: bg,
showAction: a,
duration: d,
pos: position
})
}
window.debounce = debounce
window.throttle = throttle
window.isMobile = isMobile
window.loadScript = loadScript