Summary of JavaScript common scripts (3)

  • 2020-05-10 17:43:05
  • OfStack

Extending string concatenation through arrays can lead to performance problems


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

Source code: https: / / gist github. com/hehongwei44 / fe71f10e4d2d9295aeab

Page viewport scrollbar position of the auxiliary function


/* Determines the height and width of the current page */
function pageHeight() {
    return document.body.scrollHeight;
}
function pageWidth() {
    return document.body.scrollWidth;
}
/* Determine the horizontal and vertical positions of the scroll bar */
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;
}
/* Determines the height and width of the browser viewport */
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;
}

Source code: https: / / gist github. b9b7061d4defadb com hehongwei44/62907

A function that adjusts the transparency of an element


/* A function that adjusts the transparency of an element */
function setOpacity(elem, level) {
    //IE Processing transparency
    if (elem.filters) {
        elem.style.filters = 'alpha(opacity=' + level + ')';
    } else {
        elem.style.opacity = level / 100;
    }
}

Source code: https: / / gist github. cd3b8439aff6a3c com hehongwei44/87839

Gets several generic functions for mouse position


/* Two general functions , Used to get the current position of the mouse relative to the entire page */
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;
}
/* Two functions that get the position of the mouse relative to the current element */
function getElementX(e) {
    return (e && e.layerX) || window.event.offsetX;
}
function getElementY(e) {
    return (e && e.layerY) || window.event.offsetY;
}

Source code: https: / / gist github. bd42baa491ef8 com hehongwei44/2732365

Use the cssdisplay attribute to switch 1 set of functions for element visibility


/**
 * use display To hide the functions of the elements
 * */
function hide(elem) {
    var curDisplay = getStyle(elem, 'display');     if (curDisplay != 'none') {
        elem.$oldDisplay = curDisplay;
    }
    elem.style.display = 'none';
}
/**
 * use display To display the functions of the elements
 * */
function show(elem) {
    elem.style.display = elem.$oldDisplay || '';
}

Source code: https: / / gist github. com/hehongwei44 / b4192af8227d756bfda6

Style-related generic functions


/**
 * Gets the specified element (elem) Style properties (name)
 * */
function getStyle(elem, name) {
    // If there is style[] In the , So it's already set ( And it's current )
    if (elem.style[name]) {
        return elem.style[name];
    }
    // Otherwise, , test IE The method of
    else if (elem.currentStyle) {
        return elem.currentStyle[name];
    }
    // or W3C The method of
    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);
    }
    // Otherwise, , The user is using a different browser
    else {
        return null;
    }
}

Source code: https: / / gist github. Nine abf63536accd0f2eeb7 com hehongwei44 /

Gets the current height and width of the element


/**
 * Gets the true height of the element
 * Rely on the getStyle See the function above.
 * */
function getHeight(elem) {
    return parseInt(getStyle(elem, 'height'));
}
/**
 * Gets the true width of the element
 * Rely on the getStyle See the function above
 * */
function getWidth(elem) {
    return parseInt(getStyle(elem, 'width'));
}

Source code: https: / / gist github. com/hehongwei44 / b524ff25991d99625eb2

These are the common javascript scripts Shared in this article. I hope you enjoy them.


Related articles: