jquery怎么给一个DIV设置坐标

如题所述

jquery给一个DIV设置坐标也就是设置left和top的距离。
具体举例如下:
1、定义页面中的div
<div id="div1" >测试设置坐标</div>
2、编写jquerry脚本设置div的postion属性
// 获取div1对象
var d = document.getElementById('div1');
// 设置position属性为绝对absolute
d.style.position = "absolute";
// 设置left像素为100px
d.style.left = '100px';
//设置top像素为200px
d.style.top ='200px';
或者也可以定义坐标传入以下函数:
//定义函数placeDiv,两个入参:x_pos和y_pos
function placeDiv(x_pos, y_pos) {
//获得div1这个dom对象
var d = document.getElementById('div1');
// 设置position属性为绝对absolute
d.style.position = "absolute";
//设置left像素为x_pos px
d.style.left = x_pos+'px';
//设置top像素为y_pos px
d.style.top = y_pos+'px';
}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答