Skip to content

Instantly share code, notes, and snippets.

@luokebi
luokebi / gist:2941769
Created June 16, 2012 15:58
Canvas:Draw rounded rectangle
function roundRect(ctx, x, y, width, height, radius, fill, stroke) {
if (typeof stroke == "undefined" ) {
stroke = true;
}
if (typeof radius === "undefined") {
radius = 5;
}
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.lineTo(x + width - radius, y);
@luokebi
luokebi / gist:2966345
Created June 21, 2012 15:18
DOM Insert event
document.addEventListener("DOMNodeInserted",function(event){addlink(event)},false);
function addlink(event){
var node = event.target;
if(node instanceof HTMLLIElement){
}
}
@luokebi
luokebi / gist:3057531
Created July 6, 2012 01:46
delete a item in a indexed array
//delete a item in indexed array
Array.prototype.remove=function(dx)
{
if(isNaN(dx)||dx>this.length){return false;}
for(var i=0,n=0;i<this.length;i++)
{
if(this[i]!=this[dx])
{
this[n++]=this[i]
}
@luokebi
luokebi / gist:6101540
Created July 29, 2013 01:01
js数组去重1
function removeDuplicates(arr) {
var temp = {},
r = [],
i,k;
for (i = 0;i < arr.length;i++) {
temp[arr[i]] = true;
}
var r = [];
for (k in temp) {
@luokebi
luokebi / gist:6101855
Last active December 20, 2015 08:39
jQuery插件
// 基本jQuery插件形式1
function($) {
// 向jQuery中被保护的“fn”命名空间中添加你的插件代码,用“pluginName”作为插件的函数名称
$.fn.pluginName = function(options) {
// 返回“this”(函数each()的返回值也是this),以便进行链式调用。
return this.each(function() {
// 此处运行代码,可以通过“this”来获得每个单独的元素
@luokebi
luokebi / gist:6182868
Created August 8, 2013 08:49
textarea自适应高度
var autoTextarea = function (elem, extra, maxHeight) {
extra = extra || 0;
var isFirefox = !!document.getBoxObjectFor || 'mozInnerScreenX' in window,
isOpera = !!window.opera && !!window.opera.toString().indexOf('Opera'),
addEvent = function (type, callback) {
elem.addEventListener ?
elem.addEventListener(type, callback, false) :
elem.attachEvent('on' + type, callback);
},
getStyle = elem.currentStyle ? function (name) {
function throttle( fn, time ) {
var t = 0;
return function() {
var args = arguments, ctx = this;
clearTimeout(t);
t = setTimeout( function() {
fn.apply( ctx, args );
}, time );
};
@luokebi
luokebi / star.html
Created September 17, 2013 09:34
css五角星评分效果
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
.star_bg{
width: 120px;
height: 20px;
background: url('star.png') repeat-x;
@luokebi
luokebi / Dpromise.js
Created September 17, 2013 14:48
Douglas Crouckford "promise" implementation
function make_promise () {
var status = 'unresolved',
outcome,
waiting = [],
dreading = [];
function vouch(deed, func) {
switch (status) {
case 'unresolved':
(deed === 'fulfilled' ? waiting : dreading).push(func);
@luokebi
luokebi / gist:6603755
Last active December 23, 2015 07:49
HTML to Dom
function Html2Dom(str) {
var tempDiv = document.createElement('div');
tempDiv.innerHTML = str;
var dom = tempDiv.childNodes[0];
return dom;
}