如何使用javascript在另一个div的顶部定位一个div? [英] How to position a div at the top of another div with javascript?

查看:340
本文介绍了如何使用javascript在另一个div的顶部定位一个div?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一个div是'#icon-selection-menu' bar,它的空闲位置是 absolute with top:0px left:0px 。所以它出现在内容div的左上角。

The first div is the '#icon-selection-menu' bar, it's idle position is absolute with top:0px and left:0px. So it appears at he top left corner inside the content div.

在内容div的孩子,我得到其他div,其实是一种'.moticon-button'。他们的位置在他们的父母之间是相对的。在这样的按钮点击我想要定位的第一个div的按钮上方,调整它的底部边框到按钮的顶部边框。

Deep in the content div children I got other divs which are actually kind of '.emoticon-button'. Their position is relative inside their parent. On such button click I'd like to position the first div just above the button, adjusting it's bottom border to the button's top border.

如何获得顶部值设置 $('#icon-selection-menu' $('#icon-selection-menu')。left

推荐答案

1 提供 .offset() / a>获取相对于文档的任何元素的位置。由于#icon-selection-menu 已经相对于文档定位,您可以使用:

jQuery1 provides .offset() to get the position of any element relative to the document. Since #icon-selection-menu is already positioned relative to the document, you can use this:

var destination = $('.emoticon-button').offset();
$('#icon-selection-menu').css({top: destination.top, left: destination.left});

$('#icon-selection-menu')将放置在 $('。emoticon-button')的左上角。

(1)由于在问题中使用 $ ,jQuery假定。

(1) jQuery assumed due to the use of $ in the question.

这篇关于如何使用javascript在另一个div的顶部定位一个div?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆