Through arrays, expanding string splicing can easily lead to performance problems
The code copy is as follows:
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
Code source: https://gist.github.com/hehongwei44/fe71f10e4d2d9295aeab
Helper function of the position of the page viewport scroll bar
The code copy is as follows:
/*Two functions to determine the current page height and width*/
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;
}
/*Two functions to determine 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;
}
Code source: https://gist.github.com/hehongwei44/62907b9b7061d4dfadb
Functions that adjust element transparency
The code copy is as follows:
/* Function to adjust element transparency*/
function setOpacity(elem, level) {
//IE processing transparency
if (elem.filters) {
elem.style.filters = 'alpha(opacity=' + level + ')';
} else {
elem.style.opacity = level / 100;
}
}
Code source: https://gist.github.com/hehongwei44/87839cd3b8439aff6a3c
Get several common functions for mouse position
The code copy is as follows:
/*Two common functions are 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 mouse position 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;
}
Code source: https://gist.github.com/hehongwei44/2732365bd42baa491ef8
A set of functions that use the cssdisplay attribute to switch element visibility
The code copy is as follows:
/**
* Functions that use display to hide elements
* */
function hide(elem) {
var curDisplay = getStyle(elem, 'display');
if (curDisplay != 'none') {
elem.$oldDisplay = curDisplay;
}
elem.style.display = 'none';
}
/**
* Use display to display the function of the element
* */
function show(elem) {
elem.style.display = elem.$oldDisplay || '';
}
Code source: https://gist.github.com/hehongwei44/b4192af8227d756bfda6
Style-related general functions
The code copy is as follows:
/**
* Get the style attribute (name) of the specified element (elem)
* */
function getStyle(elem, name) {
//If it exists in style[], then it has been set (and is currently)
if (elem.style[name]) {
return elem.style[name];
}
// Otherwise, test IE method
else if (elem.currentStyle) {
return elem.currentStyle[name];
}
//or W3C method
else if(document.defaultView && document.defaultView.getComputedStyle){
name = name.replace(/(AZ)/g, "-$1");
name = name.toLowerCase();
var s = document.defaultView.getComputedStyle(elem, "");
return s && s.getPropertyValue(name);
}
// Otherwise, the user is using another browser
else {
return null;
}
}
Code source: https://gist.github.com/hehongwei44/9abf63536accd0f2eeb7
Get the current height and width of the element
The code copy is as follows:
/**
* Get the true height of the element
* See the function above for the dependency getStyle.
* */
function getHeight(elem) {
return parseInt(getStyle(elem, 'height'));
}
/**
* Get the true width of the element
* See the function above for dependency getStyle
* */
function getWidth(elem) {
return parseInt(getStyle(elem, 'width'));
}
Code source: https://gist.github.com/hehongwei44/b524ff25991d99625eb2
The above are the commonly used javascript scripts shared in this article. I hope you like them.