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

蓝咒 提交于 2019-12-08 15:47:08

问题


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.

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.

How can I get top and left values to set $('#icon-selection-menu').top and $('#icon-selection-menu').left ?


回答1:


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') will be placed at the top-left corner of $('.emoticon-button').

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




回答2:


You can get the top and left position of a div using offsetTop and offsetLeft

Example:`

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



回答3:


Javascript has a built-in version of what @bfavaretto mentioned. It is a bit longer than the Jquery version, but people like me who don't use Jquery might need it.

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. Just note that what I did above was just an example and is for two seperate elements with IDs, not classes. 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. 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!




回答4:


Get '.emoticon-button' position(left,top). Then apply the same to '#icon-selection-menu'. There would some adjustment on top and left to align with emoticon.




回答5:


you can get the position of the element by using the below 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});
});


来源:https://stackoverflow.com/questions/15104538/how-to-position-a-div-at-the-top-of-another-div-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!