通过数组,拓展字符串拼接容易导致性能的问题
代码如下:
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;
新闻热点
疑难解答
图片精选