Skip to content

Instantly share code, notes, and snippets.

View ntnyq's full-sized avatar
🐣
Learning rust

ntnyq ntnyq

🐣
Learning rust
View GitHub Profile
@ntnyq
ntnyq / detect-img-light.js
Created September 29, 2018 11:43
检测图片的明暗度。
/* This function will convert each color to gray scale and return average of all pixels, so final value will be between 0 (darkest) and 255 (brightest) */
function getImageLightness(imageSrc,callback) {
var img = document.createElement("img");
img.src = imageSrc;
img.style.display = "none";
document.body.appendChild(img);
var colorSum = 0;
img.onload = function() {
@ntnyq
ntnyq / singleton.js
Created October 17, 2018 06:00
单例模式
var ntnyq = function () {
function Goy () {}
Goy.fn = Goy.prototype;
return new Goy();
}();
@ntnyq
ntnyq / eventEmitter.js
Created October 17, 2018 06:33
仿Vue的EventEmitter实现
var EventEmiter = function (){
this._events = {};
};
EventEmiter.prototype.on = function (event, cb){
if (Array.isArray(event)){
for (let i = 0, l = event.length; i < l; i++){
this.on(event[i], cb);
}
} else {
@ntnyq
ntnyq / fix-ios-scroll-top.js
Created January 25, 2019 10:40
修复移动端软键盘弹起,导致页面滚动后无法回滚的问题。
(function (doc, win) {
var body = doc.body;
var hasScrollTopChange = false; // 连续点击Input
var lastScrollTop = body.scrollTop; // 获取软键盘唤起时浏览器滚动部分的高度
var scrollTimer = null;
$('input[type="text"], input[type="tel"], input[type="number"], input[type="password"], textarea')
.focus(function () {
// 获取焦点时触发
clearInterval(scrollTimer);
@ntnyq
ntnyq / raf.js
Created March 1, 2019 17:13
raf-fallback-to-setTimeout
let lastTime = 0
const prefixes = 'webkit moz ms o'.split(' ') // 各浏览器前缀
let requestAnimationFrame
let cancelAnimationFrame
const isServer = typeof window === 'undefined'
if (isServer) {
requestAnimationFrame = function() {
return
@ntnyq
ntnyq / getImageSize.ts
Created May 31, 2019 06:37
Get the size of img HTMLElement
export function getImageSize (img: HTMLElement) {
const p = new Promise((resolve, reject) => {
if (img.natutralWidth) {
resolve({ width: img.naturalWidth, height: img.naturalHeight })
}
const image = new Image()
image.onload = () => {
resolve({ width: image.width, height: image.height })
@ntnyq
ntnyq / img-preload.js
Created June 19, 2019 02:58
Preload images with promise
const loadImg = (img) => {
const isArray = Array.isArray(img)
if (!isArray) {
const oImg = new Image()
oImg.src = img
return new Promise(resolve => {
oImg.onload = () => {
resolve()
}
})
@ntnyq
ntnyq / anime.css
Last active July 2, 2019 00:51
Common css animation
.msg {
animation: animeMsg 4.5s ease-in-out infinite;
}
@keyframes animeMsg {
0% {
opacity: 0;
}
50% {
@ntnyq
ntnyq / globalThis.js
Last active July 26, 2019 14:08
get Global This
function getGlobalThis () {
if (typeof self !== 'undefined') return self
if (typeof window !== 'undefined') return window
if (typeof global !== 'undefined') return global
// This may still return wrong result ⚠️
if (typeof this !== 'undefined') return this
throw new Error('Unable to local global `this`')
}
@ntnyq
ntnyq / netease-outer-player.md
Created July 31, 2019 14:58
[Netease music outer player] #API

Outer chain link player

<iframe
  src="https://music.163.com/outchain/player?type=2&id=27896818&auto=1&height=66" 
  frameborder="0" 
  border="1"
  marginwidth="0" 
 marginheight="0"