简体   繁体   English

如何使用javascript将div放在另一个div的顶部?

[英]How to position a div at the top of another div with javascript?

The first div is the '#icon-selection-menu' bar, it's idle position is absolute with top:0px and left:0px . 第一个div是'#icon-selection-menu'栏,它的空闲位置绝对top:0pxleft:0px So it appears at he top left corner inside the content div. 所以它出现在内容div里面的左上角。

Deep in the content div children I got other divs which are actually kind of '.emoticon-button'. 在内容div孩子的深处,我得到了其他div,实际上是'.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. 按下这样的按钮,我想将第一个div放在按钮上方,将其底部边框调整到按钮的顶部边框。

How can I get top and left values to set $('#icon-selection-menu').top and $('#icon-selection-menu').left ? 我怎样才能获得顶部左侧值来设置$('#icon-selection-menu').top$('#icon-selection-menu').left

jQuery 1 provides .offset() to get the position of any element relative to the document. jQuery 1提供.offset()来获取任何元素相对于文档的位置。 Since #icon-selection-menu is already positioned relative to the document, you can use this: 由于#icon-selection-menu已经相对于文档定位,您可以使用:

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

$('#icon-selection-menu') will be placed at the top-left corner of $('.emoticon-button') . $('#icon-selection-menu')将放置在$('.emoticon-button')左上角。

(1) jQuery assumed due to the use of $ in the question. (1)jQuery假设由于在问题中使用$

You can get the top and left position of a div using offsetTop and offsetLeft 您可以使用offsetTop和offsetLeft获取div的顶部和左侧位置

Example:` 例如:`

$('#div-id').offset().top;
$('#div-id').offset().left;

Javascript has a built-in version of what @bfavaretto mentioned. Javascript有@bfavaretto提到的内置版本。 It is a bit longer than the Jquery version, but people like me who don't use Jquery might need it. 它比Jquery版本长一点,但像我这样不使用Jquery的人可能需要它。

var iconselect = document.getElementById("icon-selection-menu");
var emoticonbtn = document.getElementById("emoticon-button");
var oTop = emoticonbtn.offsetTop;
var oLeft = emoticonbtn.offsetLeft;
iconselect.style.top = oTop;
iconselect.style.left = oLeft;
iconselect.style.position = "absolute";

You can, of course, add units to this system such as px or other things. 当然,您可以在此系统中添加单位,例如px或其他内容。 Just note that what I did above was just an example and is for two seperate elements with IDs, not classes. 请注意,我上面所做的只是一个例子,是两个带有ID的单独元素,而不是类。 The first two lines of the code will vary according to your code. 代码的前两行将根据您的代码而有所不同。 The element iconselect is what I am trying to align, and the element emoticonbtn is the button you press to make iconselect appear. 元素iconselect是我想要对齐的元素,元素emoticonbtn是你按下按钮以显示iconselect The most important parts of the code summarized: 代码中最重要的部分总结如下:

elementtomove.offsetTop; //distance from top of screen
elementtomove.offsetLeft; //distance from left of screen

Hope this helps the people who are unwilling to use JQUERY! 希望这能帮助那些不愿意使用JQUERY的人!

you can get the position of the element by using the below jquery 你可以使用下面的jquery获取元素的位置

var position=$('#icon-selection-menu').position();
var left=position.left;
var right=position.right

and on click of that button you can position it above by using 点击该按钮,您可以使用以上位置

$("#ID").live("click",function(){
    $('.emoticon-button').animate({left:left,right:right});
});

Get '.emoticon-button' position(left,top). 获取'.emoticon-button'位置(左,上)。 Then apply the same to '#icon-selection-menu'. 然后将其应用于'#icon-selection-menu'。 There would some adjustment on top and left to align with emoticon. 顶部和左侧会有一些调整以与表情符号对齐。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM