如何获得“可放置” s offset().left 和 offset().top 不使用 jquery,使用原始 javascript
由于某种原因,我无法使用 jquery 。
这是我的代码:
<!DOCTYPE html>
<html>
<head>
</head>
<body >
<style type="text/css">
#draggable
{
position: absolute;
width: 100px;
height: 100px;
padding: 0.5em;
float: left;
margin: 10px;
border: 1px solid #DDDDDD;
color: #333333;
background: #F2F2F2;
cursor: move;
z-index:10;
}
#droppable
{
width: 150px;
height: 150px;
padding: 0.5em;
float: left;
margin: 10px;
border: 1px solid #E78F08;
color: #FFFFFF;
font-weight: bold;
background: #F6A828;
overflow: hidden;
}
</style>
<p>
Eos an invidunt dignissim liberavisse, no paulo alterum vix, mei causae vivendum
te. Discere omittam ea sed. Est ex mutat aeque discere. Omnes tincidunt pro ea.
Mentitum expetenda reprehendunt vis id, duo natum adhuc ut. Malis partiendo id duo,
feugait iudicabit consequuntur at eum.
</p>
<div id="droppable">
<p>
Drop here.
</p>
</div>
<p>
Ea eam audire antiopam, agam consulatu patrioque no nec. Sea simul insolens ne,
vero scribentur duo ut. Te mel dico choro virtute. Mel ex sumo omnes.
</p>
<p>
Eos an invidunt dignissim liberavisse, no paulo alterum vix, mei causae vivendum
te. Discere omittam ea sed. Est ex mutat aeque discere. Omnes tincidunt pro ea.
Mentitum expetenda reprehendunt vis id, duo natum adhuc ut. Malis partiendo id duo,
feugait iudicabit consequuntur at eum.
</p>
<p>
Ea eam audire antiopam, agam consulatu patrioque no nec. Sea simul insolens ne,
vero scribentur duo ut. Te mel dico choro virtute. Mel ex sumo omnes.
</p>
<div id="draggable" >
<p>Drag me to my target</p>
</div>
<script>
function id(id){
return document.getElementById(id)
}
console.log(id('droppable').offsetWidth,id('droppable').offsetTop)
id('draggable').style.left =id('droppable').offsetWidth+'px'
id('draggable').style.top = id('droppable').offsetTop+'px'
</script>
</body>
</html>
这是演示 http://jsfiddle.net/6H5Yj/
那么如何设置'draggable's left 等于'droppable's left, top 等于顶部,
谢谢
for some reason , i can't use jquery .
this is my code:
<!DOCTYPE html>
<html>
<head>
</head>
<body >
<style type="text/css">
#draggable
{
position: absolute;
width: 100px;
height: 100px;
padding: 0.5em;
float: left;
margin: 10px;
border: 1px solid #DDDDDD;
color: #333333;
background: #F2F2F2;
cursor: move;
z-index:10;
}
#droppable
{
width: 150px;
height: 150px;
padding: 0.5em;
float: left;
margin: 10px;
border: 1px solid #E78F08;
color: #FFFFFF;
font-weight: bold;
background: #F6A828;
overflow: hidden;
}
</style>
<p>
Eos an invidunt dignissim liberavisse, no paulo alterum vix, mei causae vivendum
te. Discere omittam ea sed. Est ex mutat aeque discere. Omnes tincidunt pro ea.
Mentitum expetenda reprehendunt vis id, duo natum adhuc ut. Malis partiendo id duo,
feugait iudicabit consequuntur at eum.
</p>
<div id="droppable">
<p>
Drop here.
</p>
</div>
<p>
Ea eam audire antiopam, agam consulatu patrioque no nec. Sea simul insolens ne,
vero scribentur duo ut. Te mel dico choro virtute. Mel ex sumo omnes.
</p>
<p>
Eos an invidunt dignissim liberavisse, no paulo alterum vix, mei causae vivendum
te. Discere omittam ea sed. Est ex mutat aeque discere. Omnes tincidunt pro ea.
Mentitum expetenda reprehendunt vis id, duo natum adhuc ut. Malis partiendo id duo,
feugait iudicabit consequuntur at eum.
</p>
<p>
Ea eam audire antiopam, agam consulatu patrioque no nec. Sea simul insolens ne,
vero scribentur duo ut. Te mel dico choro virtute. Mel ex sumo omnes.
</p>
<div id="draggable" >
<p>Drag me to my target</p>
</div>
<script>
function id(id){
return document.getElementById(id)
}
console.log(id('droppable').offsetWidth,id('droppable').offsetTop)
id('draggable').style.left =id('droppable').offsetWidth+'px'
id('draggable').style.top = id('droppable').offsetTop+'px'
</script>
</body>
</html>
this is the demo http://jsfiddle.net/6H5Yj/
so how to set the 'draggable's left Equal the 'droppable's left, top Equal top,
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)