- 求滚动条的滚动距离
function getScrollOffset() {
if (window.pageOffset) {
return {
x: window.pageXOffset,
y: window.pageYOffset
}
} else {
return {
x: document.body.scrollLeft + document.documentElement.scrollLeft,
y: document.body.scrollTop + document.documentElement.scrollTop
}
}
}
function getDate() {
var date = new Date();
var Year = date.getFullYear();
var month = date.getMonth();
var Date1 = date.getDate();
var Hours = date.getHours();
var minutes = date.getMinutes();
var secod = date.getSeconds();
return Year + "年" + (month + 1) + '月' + Date1 + '日' + Hours + '时' + minutes + '分' + (secod + 1) + '秒';
}
function retParent(elem, n) {
while (elem && n) {
elem = elem.parentElement;
n--;
}
return elem;
}
4 . n为正 返回后面的兄弟元素节点,n为负 返回前面的 为0 返回自己
function retSibling(elem, n) {
while (elem && n) {
if (n > 0) {
n--;
elem = elem.nextElementSibling
} else {
elem = elem.previousElementSibling
n++
}
}
return elem;
}
Element.prototype.inserAfter = function (target, after) {
var before = after.nextElementSibling;
if (before == null) {
this.appendChild(target);
} else {
this.insertBefore(target, before)
}
}
Element.prototype.mychildren = function () {
var childs = this.childNodes,
len = childs.length,
arr = [];
for (var i = 0; i < len; i++) {
if (childs[i].nodeType == 1) {
arr.push(childs[i])
}
}
return arr;
}
Element.prototype.invertedChild = function () {
var childs = this.children,
len = childs.length; //当前len等于5 5-2=3;
for (var i = len - 2; i >= 0; i--) { // i = 3 ;i >= 0 从下标0开始数就是3就是倒数第二个
this.appendChild(childs[i]);
}
return this;
}
function Inherit(Target, Origin) {
function F() { }
F.prototype = Origin.prototype;
Target.prototype = new F();
Target.prototype.constuctor = Target; //自己的是哪个构造出来的
Target.prototype.uber = Origin.prototype //自己最终是继承哪个的原型
}
Fater.prototype.lastName = 'yrg'
function Fater() { }
function Son() { }
Inherit(Son, Fater)
var fater = new Fater();
var son = new Son()
function jc(n) {
if (n < 2) {
return 1
}
return n * jc(n - 1);
}
function fb(n) {
if (n <= 2) {
return 1;
} else {
return fb(n - 1) + fb(n - 2)
}
}
function type(Target) {
var template = {
"[object Array]": "Arrat",
"[object Object]": "Object",
"[object Number]": "Object-Number",
"[object Boolean]": "Boolean",
"[object String]": "String",
"[object Function]": "Function"
}
if (Target == null) {
return "null"
} else if (typeof (Target) == "object") { //引用值
var str = Object.prototype.toString.call(Target);
return template[str]
} else {
return typeof (Target)
}
}
- 数组去重
Array.prototype.unique = function () {
var obj = {},
arr1 = []; //返回新的数组
for (var i = 0; i < this.length; i++) {
if (!obj[this[i]]) { //obj没有值的话,undefined 取反说明已经有值
obj[this[i]] = 'abc';
arr1.push(this[i]);
}
}
return arr1;
}
function getViewprotOffset() {
if (window.innerWidth) {
return {
w: window.innerWidth,
y: window.innerHeight
}
} else {
if (document.compatMode == "BackCompat") {
return {
w: document.body.clientWidth,
h: document.body.clientHeight
}
} else {
return {
w: document.documentElement.clientWidth,
h: document.documentElement.clientHeight
}
}
}
}
function getElementStyle(elem , prop){
if(window.getComputedStyle){
return window.getComputedStyle(elem, null)[prop]
}else{
return elem.currentStyle[prop]
}
}