Skip to content

Instantly share code, notes, and snippets.

@cycold
Created February 12, 2015 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cycold/112a883d6e260b0c8936 to your computer and use it in GitHub Desktop.
Save cycold/112a883d6e260b0c8936 to your computer and use it in GitHub Desktop.
commone js functions
function isFunction(fun){
return typeof fun === 'function';
}
function isObject(obj){
return toString.apply(obj) == '[object Object]';
}
function isString(str){
return typeof str === 'string';
}
function isNumeric(num){
return num - parseFloat(num) >= 0;
}
function isArray(arr){
// return Object.prototype.toString.apply(arr) === '[object Array]';
return toString.apply(arr) === '[object Array]';
}
function hasClass(obj, str){
return obj.className.match(new RegExp( '(\\s|^)'+ str + '(\\s|$)' ) );
}
function addClass(obj, str){
if( !hasClass( obj, str) )
obj.className += ' ' + str;
}
function removeClass(obj, str){
if( hasClass( obj, str ) ){
var reg = new RegExp('(\\s|^)' + str +'(\\s|$)');
obj.className = obj.replace(reg, '');
}
}
function inArray(value,arr,index){
if( typeof value === 'string' || typeof value === 'number' ){
for (var i = 0, len = arr.length; i < len; i++) {
if( arr[i] === value ){
return index ? i : true;
}
}
return false;
}
}
function inObject(value, obj){
if( typeof value === 'string' ){
return value in obj;
}
}
function toggleClass(obj, str){
if( hasClass( obj, str ) ){
removeClass( obj, str);
}else{
addClass( obj, str );
}
}
//延迟函数
function sleep(milliSeconds) {
var startTime = new Date().getTime();
while (new Date().getTime() < startTime + milliSeconds);
}
//关闭浏览器tab 函数
function closeWin() {
window.opener = null;
window.open("", "_self");
window.close();
}
//优先使用原生js选择
function $$(selector, context){
if( document.querySelectorAll )
return (context || document).querySelectorAll(selector);
else
return jQuery(selector, context);
}
//对象转数组
function objectToArray(obj){
var arr = [], i;
for( i in obj )
arr.push(obj[i]);
return arr;
}
//数组转对象
function arrayToObject(arr){
var obj = {},i;
if( arr ){
for( var i, len = arr.length; i < len; i++ ){
obj[arr[i]] = arr[i];
}
return obj;
}
}
//限制数值
function constrain(val, min, max){
return Math.max(min, Math.min(val, max));
}
//检查浏览器支持的css属性值
function testProps(propsArray){
for(var i = 0, len = propsArray.length; i < len; i++){
if( document.body.style[propsArray[i]] !== undefined )
return true;
}
return false;
}
//转换浏览器前缀 -webkit-
function testPrefix(){
var prefix = ['Webkit','Moz','O','ms'];
for( var i = 0, len = prefix.length; i < len; i++ ){
if( testProps([prefix[i] + 'Transform']) ){
return '-' + prefix[i].toLowerCase() + '-';
}
}
return '';
}
function transform(ele,x,y,z){
var perspectiveProperty = getProps("Perspective");
var transformProperty = getProps("Transform");
//支持3d
if(perspectiveProperty){
ele.style[transformProperty] = "translate3d("+ x + "px," + y +"px," + z + ")";
}
//支持2d
else if(transformProperty){
ele.style[transformProperty] = "translate(" + x + "px," + y + "px)";
}
//不支持css3
else{
ele.style.positoin = "absolute";
ele.style.left = x + "px";
ele.style.top = y + "px";
}
}
function transition(ele,property,duration,timingFunction,delay){
var transitionProperty = getProps("Transition");
if(transitionProperty){
//注意单位(firefox下一定要有单位)
ele.style[transitionProperty] = getProps(property) +" "+ duration +"s "+ timingFunction +" "+ delay + "s";
}else{
alert("浏览器不支持transition");
}
}
function haveProperty(propsArray){
for(var i = 0, len = propsArray.length; i < len; i++){
if(document.body.style[propsArray[i]] !== undefined)
return true;
}
return false;
}
function upperFirstLetter(str){
return str.replace(/^[a-z]/,function(){
//console.log(arguments);
return arguments[0].toUpperCase();
});
}
function getProps(props){
if(document.body.style[props.toLowerCase()] !== undefined){
return props.toLowerCase();
} else{
var profixes = ['Webkit','Moz','ms'];
for(var i = 0, len = profixes.length; i < len; i++){
if(document.body.style[profixes[i] + upperFirstLetter(props)] !== undefined){
return profixes[i] + upperFirstLetter(props);
}
}
return false;
}
}
function getEngine(){
var docStyle = document.documentElement.style;
var engine;
if (window.opera && Object.prototype.toString.call(opera) === '[object Opera]') {
engine = 'presto';
} else if ('MozAppearance' in docStyle) {
engine = 'gecko';
} else if ('WebkitAppearance' in docStyle) {
engine = 'webkit';
} else if (typeof navigator.cpuClass === 'string') {
engine = 'trident';
}
return vendorPrefix = {
trident: 'ms',
gecko: 'Moz',
webkit: 'Webkit',
presto: 'O'
}[engine];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment