Skip to content

Instantly share code, notes, and snippets.

View yeyuguo's full-sized avatar
🎯
Focusing

Struggling Youth yeyuguo

🎯
Focusing
  • ...
  • China-BeiJing(Now)
View GitHub Profile
@yeyuguo
yeyuguo / loadScript.js
Created June 29, 2022 02:22
用于加载 script 资源测试库
function loadScript(url, callback) {
try {
let script = document.createElement("script");
if (script.readyState) {
// IE
script.onreadystatechange = function () {
if (
script.readyState === "loaded" ||
script.readyState === "complete"
@yeyuguo
yeyuguo / 判断图片大小.js
Last active May 30, 2022 11:17
判断图片尺寸 和 大小
// 处理文件大小
function handleSize(file, size) {
const isLimitSize = file.size / 1024 / 1024 < size
if (!isLimitSize) {
Message.error(`上传图片大小不能超过${size}M`)
return false
}
return true
}
@yeyuguo
yeyuguo / 清除URL参数-不更新历史记录.js
Last active May 23, 2022 04:55
不刷新页面,清除url参数
/** 不刷新页面, 清除 url 参数 */
function clearAutoParams(name) {
try {
// 获取正则结果
const getRegRes = function(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var result = window.location.search.substr(1).match(reg);
return result
}
const result = getRegRes(name)
window.addEventListener('scroll', function(){
// todo 添加距离底部位置
// const isBottom = getScrollHeight() <= getDocumentTop() + getWindowHeight()
if(getScrollHeight() == getDocumentTop() + getWindowHeight()){
//当滚动条到底时,触发内容
alert("滑动到的底部");
}
})
@yeyuguo
yeyuguo / validate.js
Last active April 2, 2022 09:38
一些校验规则
/** 校验表格 */
export function validForm(formObj, success, error) {
if (!formObj) return
formObj.validate(valid => {
console.log('valid: ', valid);
if (valid) {
success && success()
} else {
error && error(valid)
@yeyuguo
yeyuguo / 计算当前轮播可设置个数和宽度.js
Created March 30, 2022 08:50
计算当前轮播可设置个数和宽度
function getUsefulWidthCount({
containerWidth,
cardWidth=356,
borderWidth=10,
}={}) {
if(!containerWidth || containerWidth<=0) return [0, 0]
let maxCountPer = 0, resultWith = 0
const addNewCardWidth = () => (maxCountPer + 1) * (cardWidth + borderWidth) - borderWidth
while(cardWidth>0 && containerWidth>0 && addNewCardWidth() <= containerWidth) {
@yeyuguo
yeyuguo / plugin-register.js
Last active July 9, 2021 07:31
所有插件注册与执行 (洋葱机制源码实现, 以及的所有场景模拟)
// 解决 node 不能接收 try catch 的 promise
if(process) {
process.on('unhandledRejection', (reason, p) => {
console.warn('Unhandled Rejection at: Promise', p, 'reason:', reason);
// application specific logging, throwing an error, or other logic here
});
}
@yeyuguo
yeyuguo / createRandomList.js
Created June 21, 2021 05:36
创建随时数组
function createRandom(start, end) {
const range = end - start
return start + Math.round(range * Math.random())
}
function createList(max=100) {
const getInt = ()=> createRandom(1, max)
const result = []
let count = 0
while(count < max) {
@yeyuguo
yeyuguo / parseStrParams.js
Created June 1, 2021 09:48
格式化 url 字符为对象
function parseStrParam(url) {
const paramsStr = /.+\?(.+)$/.exec(url)[1] // 将 ? 后面的字符串取出来
const paramsArr = paramsStr.split('&') // 将字符串以 & 分割后存到数组中
let paramsObj = {}
// 将 params 存到对象中
paramsArr.forEach(param => {
if (/=/.test(param)) { // 处理有 value 的参数
let [key, val] = param.split('=') // 分割 key 和 value
val = decodeURIComponent(val) // 解码
val = /^\d+$/.test(val) ? parseFloat(val) : val // 判断是否转为数字