欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

JavaScript常用脚本汇总(三)_javascript技巧

程序员文章站 2022-04-27 17:21:50
...
通过数组,拓展字符串拼接容易导致性能的问题

复制代码 代码如下:

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属性来切换元素可见性的一组函数

复制代码 代码如下:

/**
* 使用display来隐藏元素的函数
* */
function hide(elem) {
var curDisplay = getStyle(elem, 'display');

if (curDisplay != 'none') {
elem.$oldDisplay = curDisplay;
}
elem.style.display = 'none';
}
/**
* 使用display来显示元素的函数
* */
function show(elem) {
elem.style.display = elem.$oldDisplay || '';
}

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

样式相关的通用函数

复制代码 代码如下:

/**
* 获取指定元素(elem)的样式属性(name)
* */
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

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

复制代码 代码如下:

/**
* 获取元素的真实高度
* 依赖的getStyle见上面的函数。
* */
function getHeight(elem) {
return parseInt(getStyle(elem, 'height'));
}
/**
* 获取元素的真实宽度
* 依赖的getStyle见上面的函数
* */
function getWidth(elem) {
return parseInt(getStyle(elem, 'width'));
}

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

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