Skip to content

Instantly share code, notes, and snippets.

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

剧中人 bh-lay

🏅
Github 认证工程师
View GitHub Profile
@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,
@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 / escapeHtml.js
Created August 7, 2017 11:12
html 转义工具
function escapeHtml(code) {
var escapeMap = {
"&": "&",
"<": "&lt;",
">": "&gt;",
'"': "&quot;",
"'": "&#x27;",
"`": "&#x60;"
};
var escaper = function (match) {
function isMobile(agent) {
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(agent);
};
@bh-lay
bh-lay / moveItems.js
Created December 27, 2016 09:16
数组中的常见操作
// 移动数组中指定索引的位置
function moveItems(arr, indexFrom, indexTo) {
arr.splice(indexTo, 0, arr.splice(indexFrom, 1)[0]);
return arr;
}
moveItems([0,1,2,3,4,5,6,7,8,9],0,7);
@bh-lay
bh-lay / randomFlag.js
Created December 27, 2016 02:23
简单的随机生成唯一标识方法
var randomFlag = (new Date().getTime()*100000 + Math.ceil( Math.random() * 100000 )).toString(36);
@bh-lay
bh-lay / GCJTobaidu.js
Created December 25, 2016 16:25
各地图产品间的坐标转换
//GCJ-02转换BD-09
function GCJTobaidu($lat, $lng){
var $v = Math.PI * 3000.0 / 180.0;
var $x = $lng;
var $y = $lat;
var $z = Math.sqrt($x * $x + $y * $y) + 0.00002 * Math.sin($y * $v);
var $t = Math.atan2($y, $x) + 0.000003 * Math.cos($x * $v);
return {
@bh-lay
bh-lay / LoadScript.js
Created December 25, 2016 15:41
简短的加载JS方法
/**
* 加载JS
*/
function LoadScript(url, callback){
var urls = typeof url === 'string' ? [url] : url;
if (!Array.isArray(urls)){
throw 'argument type error';
return;
@bh-lay
bh-lay / isNaN.js
Last active August 4, 2016 10:32
判断输入值是否为NaN
// 判断输入值是否为NaN
function isNaN( input ){
return input !== input;
}
@bh-lay
bh-lay / isSameObj.js
Created June 14, 2016 06:23
判断两个对象是否相等
function isJSON( input ){
var returns = true;
try{
JSON.stringify( input )
} catch ( e ){
returns = false;
}
return returns;
}
function isSameObj( objA, objB ){