Skip to content

Instantly share code, notes, and snippets.

View bh-lay's full-sized avatar
🏅
Github 认证工程师

剧中人 bh-lay

🏅
Github 认证工程师
View GitHub Profile
function isMobile(agent) {
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(agent);
};
@bh-lay
bh-lay / escapeHtml.js
Created August 7, 2017 11:12
html 转义工具
function escapeHtml(code) {
var escapeMap = {
"&": "&",
"<": "&lt;",
">": "&gt;",
'"': "&quot;",
"'": "&#x27;",
"`": "&#x60;"
};
var escaper = function (match) {
@bh-lay
bh-lay / drag-handle-pc-mobile.js
Last active January 10, 2020 16:59
拖拽常用代码
function preventDefault (event) {
window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty()
event.preventDefault && event.preventDefault()
event.stopPropagation && event.stopPropagation()
}
function getParam(clientX, clientY, startX, startY) {
let xOffset = clientX - startX
let yOffset = clientY - startY
return {
@bh-lay
bh-lay / get-img-contain-to-rect-config.js
Last active January 10, 2020 17:04
两种常见的图像放置在区域内的计算方法
function getImgContainToRectConfig (outerWidth, outerHeight, imgWidth, imgHeight) => {
let newWidth = outerWidth;
let newHeight = outerWidth * imgHeight / imgWidth;
if (newHeight > outerHeight) {
newHeight = outerHeight;
newWidth = outerHeight * imgWidth / imgHeight;
}
return {
top: (outerHeight - newHeight) / 2,
left: (outerWidth - newWidth) / 2,