栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

JavaScript常用脚本汇总(三)

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JavaScript常用脚本汇总(三)

通过数组,拓展字符串拼接容易导致性能的问题

复制代码 代码如下:
function StringBuffer() {
    this.__strings__ = new Array();
}
StringBuffer.prototype.append = function (str) {
    this.__strings__.push(str);
    return this;
}
StringBuffer.prototype.toString = function () {
    return this.__strings__.join("");
}
var buffer = new StringBuffer();
buffer.append("Hello ").append("javascript");
var result = buffer.toString();
alert(result);    //Hello javascript

代码来源:https://gist.github.com/hehongwei44/fe71f10e4d2d9295aeab

页面 视口 滚动条的位置的辅助函数

复制代码 代码如下:

function pageHeight() {
    return document.body.scrollHeight;
}
function pageWidth() {
    return document.body.scrollWidth;
}

function scrollX() {
    var de = document.documentElement;
    return self.pageXOffset || (de && de.scrollLeft) || document.body.scrollLeft;
}
function scrollY() {
    var de = document.documentElement;
    return self.pageYOffset || (de && de.scrollTop) || document.body.scrollTop;
}

function windowHeight() {
    var de = document.documentElement;
    return self.innerHeight || (de && de.clientHeight) || document.body.clientHeight;
}
function windowWidth() {
    var de = document.documentElement;
    return self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
}

代码来源:https://gist.github.com/hehongwei44/62907b9b7061d4defadb

调节元素透明度的函数

复制代码 代码如下:

function setOpacity(elem, level) {
    //IE处理透明度
    if (elem.filters) {
        elem.style.filters = 'alpha(opacity=' + level + ')';
    } else {
        elem.style.opacity = level / 100;
    }
}

代码来源:https://gist.github.com/hehongwei44/87839cd3b8439aff6a3c

获取鼠标位置的几个通用的函数

复制代码 代码如下:

function getX(e) {
    e = e || window.event;
    return e.pageX || e.clientX + document.body.scrollLeft;
}
function getY(e) {
    e = e || window.event;
    return e.pageY || e.clientY + document.body.scrollTop;
}

function getElementX(e) {
    return (e && e.layerX) || window.event.offsetX;
}
function getElementY(e) {
    return (e && e.layerY) || window.event.offsetY;
}

代码来源:https://gist.github.com/hehongwei44/2732365bd42baa491ef8

使用cssdisplay属性来切换元素可见性的一组函数

复制代码 代码如下:

function hide(elem) {
    var curDisplay = getStyle(elem, 'display');

    if (curDisplay != 'none') {
        elem.$oldDisplay = curDisplay;
    }
    elem.style.display = 'none';
}

function show(elem) {
    elem.style.display = elem.$oldDisplay || '';
}

代码来源:https://gist.github.com/hehongwei44/b4192af8227d756bfda6

样式相关的通用函数

复制代码 代码如下:

function getStyle(elem, name) {
    //如果存在于style[]中,那么它已被设置了(并且是当前的)
    if (elem.style[name]) {
        return elem.style[name];
    }
    //否则,测试IE的方法
    else if (elem.currentStyle) {
        return elem.currentStyle[name];
    }
    //或者W3C的方法
    else if(document.defaultView && document.defaultView.getComputedStyle){
        name = name.replace(/(A-Z)/g, "-$1");
        name = name.toLowerCase();
        var s = document.defaultView.getComputedStyle(elem, "");
        return s && s.getPropertyValue(name);
    }
    //否则,用户使用的是其他浏览器
    else {
        return null;
    }
}

代码来源:https://gist.github.com/hehongwei44/9abf63536accd0f2eeb7

获取元素当前的高度和宽度

复制代码 代码如下:

function getHeight(elem) {
    return parseInt(getStyle(elem, 'height'));
}

function getWidth(elem) {
    return parseInt(getStyle(elem, 'width'));
}

代码来源:https://gist.github.com/hehongwei44/b524ff25991d99625eb2

以上就是本文分享的javascript常用脚本了,希望大家能够喜欢。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/103639.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号