Skip to content

Instantly share code, notes, and snippets.

View xuanfeng's full-sized avatar

ivan xuanfeng

View GitHub Profile
@xuanfeng
xuanfeng / extend.js
Last active January 3, 2016 02:39
extend()函数:用一个或多个其他对象来扩展一个对象,返回被扩展的对象
// 定义一个扩展函数,用来将第二个以及后续参数复制只第一个参数
// 这里我们处理了IE bug:在多数IE版本中
// 如果o的属性拥有一个不可枚举的同名属性,则for/in循环
// 除非我们先是检测它
var extend = (function(){ //将这个函数的返回值赋值给extend
// 在修复它之前,首先检查是否存在bug
for(var p in {toString: null}){
return function extend(o){
// 如果代码执行到这里,那么for/in循环会正确工作并返回
// 一个简单版本的extend()函数
@xuanfeng
xuanfeng / inherit.js
Last active January 3, 2016 02:39
通过原型继承创建一个新对象
function inherit(p){
if(p ==null) throw TypeError();
if(Object.create){
return Object.create(p);
}
var t = typeof p;
if(t !== "object" && t !== "function") throwTypeError();
function f(){};
f.prototype = p;
return new f();
@xuanfeng
xuanfeng / dateFormat.js
Last active November 9, 2015 06:09
时间格式化(年、月、日、时、分、秒)、获取时间区间
// 时间格式化
// 使用:var d = new Date().format('yyyy-MM-dd');
Date.prototype.format = function(format) {
var date = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
@xuanfeng
xuanfeng / iLocalStorage.js
Last active August 29, 2015 14:22
LocalStorage 本地存储兼容函数
/*
* 名称:本地存储函数
* 功能:兼容各大浏览器存储
* 作者:轩枫
* 日期:2015/06/11
* 版本:V2.0
*/
/**
* LocalStorage 本地存储兼容函数
@xuanfeng
xuanfeng / hideWxMenu.js
Created May 26, 2015 03:46
隐藏微信右上角按钮
//隐藏微信菜单
function initWeixinBridge(){
WeixinJSBridge.call('hideOptionMenu');
}
if (typeof WeixinJSBridge == "object" &&
typeof WeixinJSBridge.invoke == "function") {
initWeixinBridge();
} else {
if (document.addEventListener) {
document.addEventListener("WeixinJSBridgeReady", initWeixinBridge, false);
@xuanfeng
xuanfeng / css3Style.js
Created May 4, 2015 06:17
设置移动端transition、translate样式
// 设置动画、坐标移动样式(硬件加速、浏览器兼容、原生方式)
var CSS_PREFIX_MAP = ["webkit", "moz", "ms", "o", ""],
NUMBER_REG = /\-?[0-9]+\.?[0-9]*/g;
var setTransition = function(ele, css) {
var name, prefix, _i, _len, _results;
_results = [];
for (_i = 0, _len = CSS_PREFIX_MAP.length; _i < _len; _i++) {
prefix = CSS_PREFIX_MAP[_i];
name = prefix ? "" + prefix + "Transition" : "transition";
@xuanfeng
xuanfeng / bigger.js
Last active August 29, 2015 14:20
JS逼格(实用)代码-移动端
// 1. 设置变量为undefined
var UNDEFINED = void 0;
// 2. 判断支不支持touch事件
var IsTouch = 'ontouchend' in window;
// 3. 各端事件兼容
START_EVENT = IsTouch ? 'touchstart' : 'mousedown';
@xuanfeng
xuanfeng / regex_demo.js
Last active August 29, 2015 14:19
正则表达式记录
/* 部分替换
需求: <link href="/" rel="canonical"> 需要将href替换成data-href
解决:通过()获取某一组固定字符串,用$1表示,再进行替换
*/
var str = '<link href="/" rel="canonical"><link href="/" rel="canonical"><link data-id="" href="/" rel="canonical">';
res = str.replace(/(link.*?)href/ig, '$1data-href');
@xuanfeng
xuanfeng / splitSpace.js
Last active August 29, 2015 14:13
split时过滤空白
$('#Participants').val().split(';').filter(function (n) { return n != '' })
@xuanfeng
xuanfeng / timeHello.js
Last active August 29, 2015 14:13
JS按时间区间显示不同信息
// 按不同的时间段显示不同的“问好”信息,一般做法是
// if(hour>=0 && hour <=5;){
// }else if(hour>=6 && hour <=8;){
// }else if(hour>=9 && hour <=11;){}
// ...
// 这里换了一种做法,看起来相对简洁一点!
/*
Test case:
for(var i = 0,iMax = 23; i <=iMax;i++){