js封装公共的方法

图片压缩

百亿站点

// 1.封装a-b随机数
// parseInt(Math.random()*9000+1000)  
// 0-9000,不包含9000  ==》1000-10000,不包含10000.==》1000-9999整数
// parseInt(Math.random()*(9999-1000+1)+1000)

// 获取数字
function getRandomNum(a, b) {
    return parseInt(Math.random() * (b - a + 1) + a);
}

// 获取颜色
function getRandomColor() {
    var r = getRandomNum(0, 255);
    var g = getRandomNum(0, 255);
    var b = getRandomNum(0, 255);
    return "rgb(" + r + "," + g + "," + b + ")";
}

// 获取id
function getid(id) {
    return document.getElementById(id);
}

// 下拉菜单
function downlist(clickid, blockid) {
    // clickid 点击的节点
    // blockid 要改变的节点
    var getclickid = document.getElementById(clickid);
    var getblockid = document.getElementById(blockid);
    var show = false;
    getclickid.onclick = function () {
        getblockid.style.display = show ? "block" : "none";
        show = !show;
    }
}

// onmouseover  鼠标移到目标上;鼠标移进时
function onmouseover(clickid, blockid, list1_li, list2_ul) {
    var getclickid = document.getElementById(clickid);
    var getblockid = document.getElementById(blockid);
    getclickid.onmouseover = function () {
        getblockid.style.display = "block";
    }
}

// onmouseout 鼠标移开时 鼠标离开时 当鼠标移开 
function onmouseout(clickid, blockid) {
    var getclickid = document.getElementById(clickid);
    var getblockid = document.getElementById(blockid);
    getclickid.onmouseout = function () {
        getblockid.style.display = "none";
    }
}


// onmouseout 鼠标移进是显示,离开时隐藏,用于下拉导航
function onmouseoverlist(list1_li, list2_ul) {
    for (var i = 0; i < list2_ul.length; i++) {
        list2_ul[i].num = [i];
    }
    for (var i = 0; i < list1_li.length; i++) {
        list1_li[i].num = [i];

        list1_li[i].onmouseover = function () {

            list2_ul[this.num].style.display = "block";
        }
        list1_li[i].onmouseout = function () {
            list2_ul[this.num].style.display = "none";
        }
    }
}

**

更多面试题和答案,请微信查看

**
js封装公共的方法_第1张图片

## 在小程序中查看

js封装公共的方法_第2张图片

你可能感兴趣的:(js)