美文网首页
8.6 封装自己的 js库

8.6 封装自己的 js库

作者: 康轩 | 来源:发表于2017-05-30 23:48 被阅读0次

下面 就封装 滚动 与 显示隐藏的 效果

  • 获取scrollTop和scrollLeft
    */
    function scroll() {//滚动的封装
    if(window.pageYOffset !== null){ // ie9+ 和 最新浏览器
    return {
    left: window.pageXOffset,
    top: window.pageYOffset
    }
    }else if(document.compatMode == 'CSS1Compat'){ // 非怪异浏览器
    return{
    left: document.documentElement.scrollLeft,
    top: document.documentElement.scrollTop
    }
    }
    return{
    left: document.body.scrollLeft,
    top: document.body.scrollTop
    }
    }

//封装显示

function show(tagId) {
return document.getElementById(tagId).style.display = 'block';
}

//这个封装隐藏
function hide(tagId) {
return document.getElementById(tagId).style.display = 'none';
}

function $(tagId) {
return document.getElementById(tagId);
}

//封装隐藏 显示 用这上 下 两种都可以
/*function hide(obj) {
return obj.style.display = 'none';
}

function show(obj) {
return obj.style.display = 'block';
}

function $(id) {
return typeof id === 'string' ? document.getElementById(id): id;
}*/

相关文章

网友评论

      本文标题:8.6 封装自己的 js库

      本文链接:https://www.haomeiwen.com/subject/mgxdfxtx.html