如何使用javascript在另一个div的顶部定位一个div?

编程入门 行业动态 更新时间:2024-10-28 18:27:08
本文介绍了如何使用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?

本文发布于:2023-10-08 09:52:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1472276.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   javascript   div

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!