JS codes

作者: jacktown | 来源:发表于2017-06-23 11:48 被阅读0次

begin: 20170621
version: 20170724

BOM相关

获取屏幕宽度

//edition:20170623
//from:js高程(p198)
//获取视口宽高
var pageWidth = window.innerWidth,
    pageHeight = window.innerHeight;
if(typeof pageWidth !== "number"){
    //for lt IE 9
    if(document.compatMode === "CSS1Compat"){
        pageWidth = document.documentElement.clientWidth;
        pageWidth = document.documentElement.clientHeight;
    }else{
        pageWidth = document.body.clientWidth;
        pageHeight = document.body.clientHeight;
    }
}

获取URL的查询字符串参数

//func: 获取location对象中的查询字符串参数
//edition: 20170623
//from: js高程(p207)
function getQueryStringArgs(){
    var qs = (location.search.length > 0)?location.search.substring(1):"",  
        //get query string and delete the start "?"
        args = {},  //to store args
        items = (qs.length > 0) ? qs.split("&") : [],
        item = null,
        name = "",
        value = "",
        i = 0,
        len = items.length;
    for(i = 0; i < len; i++){
        //add each item to args
        item = items[i].split("=");
        name = decodeURIComponent(item[0]);
        value = decodeURIComponent(item[1]);
        args[name] = value;
    }
    return args;
}

插件检测


//func: 插件检测
//edition: 20170623
//from: js高程(p212) (modified)
function hasPlugin(name){
    if(typeof name !== "string"){
        return false;
    }
    var identifier = "";

    switch(name.toLowerCase()){
        //get the plugin's identifier in IE arrording to name
        case flash:
            identifier = "ShockwaveFlash.ShockwaveFlash";
            break;
        case quicktime:
            identifier = "QuickTime.QuickTime";
            break;
    }

    var result = hasNotIEPlugin(name);
    if(!result){
        result = hasIEPlugin(identifier);
    }
    return result;
}

function hasIEPlugin(identifier){
    try{
        new ActiveXObject(identifier);
        return true;
    }catch(ex){
        return false;
    }
}

function hasNotIEPlugin(name){
    name = name.toLowerCase();
    for(var i = 0; i < navigator.plugins.length; i++){
        if(navigator.plugins[i].name.toLowerCase().indexOf(name) > 0){
            return true;
        }
    }
    return false;
}

对象是否具有某一方法

//func: 检测object对象是否具有property属性
//edition: 20170623
//from: js高程(p219) (modified)
function isHostMethod(object,property){
    var t = typeof object[property];
    t = t.toLowerCase();
    return t === "function" ||
            (!!(t === "object" && boject[property])) ||
            t === "unknown";
}

获取元素相对页面的左偏移和上偏移量

//func: 获取元素相对视窗的左偏移和上偏移量
//edition: 20170628
//from: js高程(p321)
//question: 应该差一个边框宽度

function getElementLeft(element){
    var actualLeft = element.offsetLeft,
        current = element.offsetParent;
    while(current !== null){
        actualLeft += current.offsetLeft;
        current  = current.offsetParent;
    }

    return actualLeft;
}
function getElementTop(element){
    var actualTop = element.offsetTop,
        current = element.offsetParent;
    while(current !== null){
        actualTop += current.offsetTop;
        current  = current.offsetParent;
    }

    return actualTop;

}

获得视窗大小

//func: 获取视窗大小
//edition: 20170628
//from: js高程(p323)
function getViewport(){
    if(document.compatMode === "BackCompat"){
        return {
            width: document.body.clientWidth,
            height: document.body.clientHeight
        };
    }else{
        return {
            width: document.documentElement.clientWidth,
            height: document.documentElement.clientHeight
        };
    }
}

获取元素相对视口的位置

//func: 获取元素相对视口的位置
//edition: 20170628
//from: js高程(p325)
function getBoundingClientRect(element){
    var scrollTop = document.documentElement.scrollTop,
        scrollLeft = document.documentElement.scrollLeft;

    if(element.getBoundingClientRect){
        if(typeof auguments.callee.offset !== "number"){
            var temp = document.createElement("div");
            temp.style.cssText = "position: absolute; left: 0; top: 0";
            document.body.appendChild(temp);
            arguments.callee.offset = -temp.getBoundingClientRect().top - scrollTop;
            document.body.removeChild(temp);
            temp = null;
        }
        var rect = element.getBoundingClientRect(),
            offset = arguments.callee.offset;
        return {
            left: rect.left + offset,
            right: rect.right + offset,
            top: rect.top + offset,
            bottom: rect.bottom + offset
        };
    }else{
        var actualTop = getElementTop(element),
            actualLeft = getElementLeft(element);
        return {
            left: actualLeft - scrollLeft,
            rigth: actualLeft + offsetWidth - scrollLeft,
            top: actualTop - scrollTop,
            bottom: actualTop + offsetHeight - scrollTop 
        };
    }
}

相关文章

网友评论

      本文标题:JS codes

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