1.滚动条
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
console.log(scrollTop);
//如果要监听滚动条,监听onscroll事件即可
document.body.onscroll = function() {
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
console.log(scrollTop);
}
2.获取样式兼容
function getStyle(dom, styleName){
return dom.currentStyle?dom.currentStyle[styleName]getComputedStyle(dom)[styleName];
}
3.事件对象兼容
e = e || window.event;
4.阻止事件冒泡兼容
event.stopPropagation? event.stopPropagation():event.cancelBubble=true;
5.阻止默认行为兼容
function stopPreventDefault(e) {
if (e && e.preventDefault) {
e.preventDefault();//阻止浏览器默认行为(W3C)
} else {
window.event.returnValue = false;//IE中阻止浏览器行为
}
return false;
}
6.事件监听兼容
var addListen = function(element, name, fn) {
if(element.addEventListener) {
element.addEventListener(name, fn);
}else if(element.attachEvent) {
element.attachEvent('on' + name, fn);
}else {
element['on' + name] = fn;
}
}
网友评论