您的位置:首页 > Web前端 > JavaScript

public.js

2016-06-30 11:47 423 查看
'use strict';

// 获取CSS
function getStyle(obj, attr) {
return (obj.currentStyle || getComputedStyle(obj, false))[attr];
}

// 设置CSS
function setStyle(obj, attr, val) {
switch (typeof attr) {
case 'string':
return obj.style[attr] = val;

case 'object':
for (var i in attr) {
obj.style[i] = attr[i];
}
}
}

// 获取随机整数
function rnd(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}

// 获取DOM的绝对位置
function getPos(obj) {
var t = obj.offsetTop,
l = obj.offsetLeft,
p = obj.offsetParent;
while (p) {
t += p.offsetTop,
l += p.offsetLeft,
p =  p.offsetParent;
}
return {top: t, left: l};
}

// 获取鼠标划入的方向
function hoverDir(e, obj) {
var oh = document.documentElement,
ob = document.body,
sw = obj.offsetWidth,
sh = obj.offsetHeight;
var sx = getPos(obj).left + sw / 2 - e.clientX - (oh.offsetLeft || ob.offsetLeft),
sy = getPos(obj).top  + sh / 2 - e.clientY - (oh.scrollTop  || ob.scrollTop);
return (Math.round((Math.atan2(sy, sx) * 180 / Math.PI + 180) / 90) % 4 + 1) % 4 + 1;
}

// 初始化 rem
function initRem() {
var w = window.innerWidth,
e = 6.4,
o = document.documentElement;
o.style.fontSize = (Math.floor(w / e) >= 100 ? 100 : Math.floor(w / e)) + 'px';
}

// 获取GET参数
function parseParams(property) {
if (!property) return;
var arr = location.search.substring(1).split('&'), i, temp;
for (i = 0; i < arr.length; i ++) {
temp = arr[i].split('=');
if (temp[0] === property) return decodeURIComponent(temp[1]);
}
}

// Class是否存在
function hasClass(obj, sClass) {
return !!(obj.className.match(new RegExp('(^|\\s)' + sClass + '(\\s|$)')));
}

// 添加Class
function addClass(obj, sClass) {
!hasClass(obj, sClass) && (obj.className += ' ' + sClass);
}

// 删除Class
function removeClass(obj, sClass) {
hasClass(obj, sClass) && (obj.className = obj.className.replace(sClass, '').replace(/(^\s+|\s+$)/, ''));
}

// 把DOM A 放入到 DOM B 之后
function insertAfter(obj, target) {
var nextDom = target.nextElementSibling || target.nextSibling;
nextDom ? nextDom.parentNode.insertBefore(obj, nextDom) : target.parentNode.appendChild(obj);
}

// 字符按键
function getCharCode(e, isTrue) {
if (e.charCode) {
return !isTrue ? e.charCode : String.fromCharCode(e.charCode);
}
else {
return !isTrue ? e.keyCode : String.fromCharCode(e.keyCode);
}
}

// 获取动态创建的元素
function getTarget(e) {
return e.target || e.srcElement;
}

// 获取最近的元素
function getRelatedTarget(e) {
if (e.relatedTarget) {
return e.relatedTarget;
}
else {
switch (e.type.toLowerCase()) {
case 'mouseover':
return e.fromElement;

case 'mouseout':
return e.toElement;
}
}
}

// 取消传统事件冒泡
function stopPropagation(e) {
window.event ? event.cancelBubble = true : e.stopPropagation();
}

// 取消事件默认行为
function preventDefault(e) {
window.event ? event.returnValue = false : e.preventDefault();
}

// URL添加?
function unifyPath(options) {
options = options || {};
options.query = location.href.split('?')[1];
unifyPath.path = options.url || '';
unifyPath.data = options.data || '';
if (typeof options.query === 'undefined') {
history.replaceState(null, null, location.href.split('#')[0] + '?XX' + location.hash);
unifyPath(options);
}
else {
if (options.query === options.url) {
options.callback && options.callback(options.url);
}
}
}

// 添加一条历史记录
function pushState(json) {
json = json || {};
json.url = json.url ? '?' + json.url : '';
json.data = json.data || {};
json.title = json.title || null;
history.pushState(json.data, json.title, json.url);
}

// 点击前进,后退按钮时触发
function popState(json) {
json = json || {};
window.onpopstate = function () {
if (history.state) {
json.have && json.have(history.state);
}
else {
json.none && json.none(unifyPath.data, location.href.split('?')[1] === unifyPath.path);
}
};
}

// 在数组中查找xxx
function findInArr(arr, val) {
if (!arr || !arr.length) return false;
for (var i in arr) {
if (arr[i] === val) return true;
}
return false;
}

// 数组中是否有重复项
function isRepeat(arr) {
var json = {};
for (var i in arr) {
if (json[arr[i]]) return true;
json[arr[i]] = true;
}
return false;
}

// 是不是数字
function isNumber(n) {
return !(isNaN(n) || isNaN(parseFloat(n)));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: