Heim > Web-Frontend > js-Tutorial > Hauptteil

JavaScript与Div对层定位和移动获得坐标的实现代码_javascript技巧

WBOY
Freigeben: 2016-05-16 18:20:05
Original
913 Leute haben es durchsucht
1:移动图层 获得点的x轴y轴坐标,从而进行绝对定位(注意:竖拉框会影响 x 轴 y 轴坐标值)
复制代码 代码如下:

var x,y,z,down=false,obj
function init(){
obj=event.srcElement //事件触发对象
obj.setCapture() //设置属于当前对象的鼠标捕捉
z=obj.style.zIndex //获取对象的z轴坐标值
//设置对象的z轴坐标值为100,确保当前层显示在最前面
obj.style.zIndex=100
x=event.offsetX //获取鼠标指针位置相对于触发事件的对象的X坐标
y=event.offsetY //获取鼠标指针位置相对于触发事件的对象的Y坐标
down=true //布尔值,判断鼠标是否已按下,true为按下,false为未按下
}
function moveit(){
//判断鼠标已被按下且onmouseover和onmousedown事件发生在同一对象上
if(down&&event.srcElement==obj){
with(obj.style){
/*设置对象的X坐标值为文档在X轴方向上的滚动距离加上当前鼠标指针相当于文档对象的X坐标值减鼠标按下时指针位置相对于触发事件的对象的X坐标*/
// posLeft=event.x-x;
posLeft=document.body.scrollLeft+event.x-x;
/*设置对象的Y坐标值为文档在Y轴方向上的滚动距离加上当前鼠标指针相当于文档对象的Y坐标值减鼠标按下时指针位置相对于触发事件的对象的Y坐标*/
// posTop=event.y-y;
posTop=document.body.scrollTop+event.y-y;
window.status="posLeft="+posLeft+",posTop="+posTop;
window.status=window.status+"clientX="+event.clientX+"clientY="+event.clientY+"scrollLeft="+document.body.scrollLeft+"scrollTop="+document.body.scrollTop+",event.y="+event.y+",event.x"+event.x;
}
}
}
function stopdrag(){
//onmouseup事件触发时说明鼠标已经松开,所以设置down变量值为false
down=false
obj.style.zIndex=z //还原对象的Z轴坐标值
obj.releaseCapture() //释放当前对象的鼠标捕捉
//alert("X:"+obj.style.left+";Y:"+obj.style.top);
}


2:为了使图层在不同分辨率浏览器上显示同样的效果,我们需要对图层定位设置

第一步:对最外层 进行相对定位

复制代码 代码如下:




第二步:在相对里面进行决对定位(这样图层就不会随着分辨率的改变而变形)
复制代码 代码如下:



JavaScript与Div对层定位和移动获得坐标的实现代码_javascript技巧ALT="" />

省略

省略

省略

省略

省略

省略

省略

省略

省略


//===================(层移动方法调用)=============================
复制代码 代码如下:

onmouseup=stopdrag()
style="position:absolute;left:60;top:190;width:5;height:5;z-index:99;;border:1px solid #000000;">
D


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!