2020-03-20 22:38:54 +08:00

102 lines
2.4 KiB
JavaScript

//封装id
function my$(id){
return document.getElementById(id)
}
//获取文本内容兼容代码
function getInnerText(element){
if(element.textContent){
return element.textContent
}else if(element.getInnerText){
return element.innerText
}
}
//获取文本内容兼容代码
function getInnerText(element,value){
if(element.textContent){
element.textContent = value
}else if(element.getInnerText){
element.innerText = value
}
}
//为任意元素绑定事件的兼容
function addEventListener(ele,type,fn){
if(ele.addEventListener){
ele.addEventListener(type,fn,false)
}else if(ele.attachEvent){
ele.attachEvent('on'+type,fn)
}else{
els['on'+type] = fn
}
}
//解绑事件的兼容性
function removeEventListener(ele,type,fn){
if(ele.removeEventListener){
ele.removeEventListener(type,fn,false)
}else if(ele.detachEvent){
ele.detachEvent('on'+type,fn)
}else{
ele['on'+type] = null
}
}
// 事件参数兼容性
var ev = window.event || e
//获取元素的任意一个样式的属性值
function getStyle(ele,attr){
return window.getComputedStyle ? window.getComputedStyle(ele,null)[attr] : ele.currentStyle[attr] || 0
}
//对于页面scrollLeft scrollTop 有需要处理兼容
function getScroll(){
return{
left:window.PageXoffset || document.body.scrollLeft || document.documentElement.scrollLeft || 0,
top:window.PageYoffset || document.body.scrollTop || document.documentElement.scrollTop || 0
}
}
function setNumIterator() {
if(!Number.prototype[Symbol.iterator]) {
Object.defineProperty(
Number.prototype,
Symbol.iterator,
{
writable: true,
configurable:true,
enumerable: false,
value: function iterator(){
var i, inc, done = false, top = +this;
// 正向迭代还是反向迭代?
inc = 1 * (top < 0 ? -1 : 1);
return {
// 使迭代器本身成为iterable
[Symbol.iterator](){return this;},
next() {
if(!done) {
// 初始迭代总是0
if(i == null) {
i = 0
}
// 正向迭代
else if(top >= 0) {
i = Math.min(top, i + inc);
}
// 反向迭代
else {
i = Math.max(top, i + inc);
}
// 本次迭代后结束?
if (i == top) done = true;
return {value: i, done: flase};
} else {
return {done: true};
}
}
}
}
}
)
}
}