js 控制div是位置 各位帮帮忙!js怎么控制div显示的位置 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 说详细点,div和一般的标签一样 拿到dom后操作就是了 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript"> function $(id){ return document.getElementById ? document.getElementById(id) : null; } function move(){ with ($("div1")) { style.left = offsetLeft + 10 + "px"; style.top = offsetTop + 10 + "px"; } } function show(){ with ($("div1").style) { if (display == "none") { display = "block"; $("btn2").value = "show"; } else { display = "none"; $("btn2").value = "hide"; } } } </script> </head> <body> <input id="btn1" type="button" value="move" onclick="move();"> <input id="btn2" type="button" value="hide" onclick="show();"><hr> <div id="div1" style="position:absolute;background:blue;left:100px;top:100px;width:200px;height:100px;"> <div> </body></html> 控制div的位置其实就是控制元素的位置我这里有几个函数 你可以参考下function getStyle(elem,name){//获取元素的样式 if(elem.style[name]) return elem.style[name]; else if(elem.currentStyle) return elem.currentStyle[name]; 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;}function pageX(elem){//相对整个文档的x的位置 return elem.offsetParent? elem.offsetLeft+pageX(elem.offsetParent): elem.offsetLeft; }function pageY(elem){//相对整个文档的y的位置 return elem.offsetParent? elem.offsetTop+pageY(elem.offsetParent): elem.offsetTop;}function parentX(elem){//元素相对父元素的位置 return elem.parentNode==elem.offsetParent? elem.offsetLeft: pageX(elem)-pageX(elem.parentNode);}function parentY(elem){//元素相对父元素的位置 return elem.parentNode==elem.offsetParent? elem.offsetTop: pageY(elem)-pageY(elem.parentNode);}function postX(elem){//获取css位置 return parseInt(getStyle(elem,"left"));}function postY(elem){ return parseInt(getStyle(elem,"top"));}function setX(elem,pos){//设置css位置 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){ setX(posY(elem)+pos);} function getHeight(elem){//获取元素当前的高度和宽度 return parseInt(getStyle(elem,'height'));}function getWidth(elem){ return parseInt(getStyle(elem,'width'));} 先把基础打好吧,看看dom之类的 就是把一个frame中的div 点击弹出 展现在浏览器的中心 不是当前frame的页面中心 行不行 咨询关于页面html元素的数量问题. Javascript版 《植物大战僵尸》 试玩~~~ 求正则啊。。 extjs 怎么实现在一个页面点击另一个页面的按钮,在线等 Window.location.href 如何连接到同一工程另个目录下的页面? 光标焦点判定的问题,谢谢! 太笨了,这该怎么弄!!! 如何在asp页面中 浮动出一个框来,询问是否要将本站加为首页。类似于sina前段时间的显示 急问如何使js外的变量传到js中来?? 为什么不能写<script /> js传值问题 extjs 链接时网页打开方式
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
function $(id){
return document.getElementById ? document.getElementById(id) : null;
}
function move(){
with ($("div1")) {
style.left = offsetLeft + 10 + "px";
style.top = offsetTop + 10 + "px";
}
}
function show(){
with ($("div1").style) {
if (display == "none") {
display = "block";
$("btn2").value = "show";
}
else {
display = "none";
$("btn2").value = "hide";
}
}
}
</script>
</head>
<body>
<input id="btn1" type="button" value="move" onclick="move();">
<input id="btn2" type="button" value="hide" onclick="show();"><hr>
<div id="div1" style="position:absolute;background:blue;left:100px;top:100px;width:200px;height:100px;">
<div>
</body>
</html>
其实就是控制元素的位置
我这里有几个函数 你可以参考下function getStyle(elem,name){//获取元素的样式
if(elem.style[name])
return elem.style[name];
else if(elem.currentStyle)
return elem.currentStyle[name];
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;
}function pageX(elem){//相对整个文档的x的位置
return elem.offsetParent?
elem.offsetLeft+pageX(elem.offsetParent):
elem.offsetLeft;
}function pageY(elem){//相对整个文档的y的位置
return elem.offsetParent?
elem.offsetTop+pageY(elem.offsetParent):
elem.offsetTop;
}function parentX(elem){//元素相对父元素的位置
return elem.parentNode==elem.offsetParent?
elem.offsetLeft:
pageX(elem)-pageX(elem.parentNode);
}
function parentY(elem){//元素相对父元素的位置
return elem.parentNode==elem.offsetParent?
elem.offsetTop:
pageY(elem)-pageY(elem.parentNode);
}function postX(elem){//获取css位置
return parseInt(getStyle(elem,"left"));
}
function postY(elem){
return parseInt(getStyle(elem,"top"));
}function setX(elem,pos){//设置css位置
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){
setX(posY(elem)+pos);
} function getHeight(elem){//获取元素当前的高度和宽度
return parseInt(getStyle(elem,'height'));
}function getWidth(elem){
return parseInt(getStyle(elem,'width'));
}