Skip to content

Instantly share code, notes, and snippets.

@hehongwei44
Created July 27, 2014 05:49
Show Gist options
  • Select an option

  • Save hehongwei44/7f0eca7c0a0ae19adc9f to your computer and use it in GitHub Desktop.

Select an option

Save hehongwei44/7f0eca7c0a0ae19adc9f to your computer and use it in GitHub Desktop.
一些元素位置设置的通用方法
/*获取元素的CSS位置的辅助函数*/
/**
* 查找元素的左端位置,
* 代码依赖:getStyle来自 https://gist.github.com/hehongwei44/9abf63536accd0f2eeb7
* */
function posX(elem) {
return parseInt(getStyle(elem, "left"));
}
/**
* 查找元素的顶端位置
* */
function posY(elem) {
return parseInt(getStyle(elem, "top"));
}
/*设置元素x和y位置(与当前位置无关)的一对函数*/
/**
* 设置元素水平的函数
* */
function setX(elem, pos) {
elem.style.left = pos + "px";
}
/**
* 设置元素垂直位置的函数
* */
function setY(elem, pos) {
elem.style.top = pos + "px";
}
/**
* 在元素的水平位置上增加像素距离的函数
* */
function addX(elem, pos) {
setX(posX(elem) + pos);
}
/**
* 在元素的垂直位置上增加像素距离的函数
* */
function addY(elem, pos) {
setY(posY(elem) + pos);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment