简体   繁体   English

如何根据绝对定位的元素高度动态更改边距

[英]How do I dynamically change margin depending on an absolutely positioned elements height

I am trying to dynamically change the margin-top of an element depending on the height of another absolutely positioned image.我正在尝试根据另一个绝对定位图像的高度动态更改元素的边距顶部。

Before anyone asks, it has to be absolutely positioned because of other reasons so I can't just make it relative.在任何人问之前,由于其他原因,它必须绝对定位,所以我不能只是相对。

I am able to change the margin only when I type in an absolute value such as "200px".只有当我输入诸如“200px”之类的绝对值时,我才能更改边距。 However, when I try to use a variable to dynamacally change the margin-top on every window rezise, it simply don't give the element any margin at all.但是,当我尝试使用变量动态更改每个 window 调整的边距顶部时,它根本不给元素任何边距。

Any help would be helpful.任何帮助都会有所帮助。 Thank you in advance!先感谢您!

var element;
var textIntro;
var newMargin;

function setMargin() {
    element = document.getElementById("cover1");
    newMargin = element.style.height;
    document.getElementById("TextIntro").style.marginTop = newMargin;
}

Hope it will help you this Js:希望它会帮助你这个Js:

setMargin();

function setMargin() {
    element = document.getElementById("cover");
    newMargin = element.offsetHeight;
    document.getElementById("cover1").style.top = newMargin +"px";
}

DEMO演示

 var element; var textIntro; var newMargin; setMargin(); function setMargin() { element = document.getElementById("cover"); newMargin = element.offsetHeight; document.getElementById("cover1").style.top = newMargin +"px"; }
 body{ margin: 0; } body > div { position:relative; padding:20px; height:500px; width: 90vw; background-color:red; } body > div > div { position:absolute; display:block; width:calc(100% - 80px); /* Padding parent + this object */ margin-top: 20px; padding: 0 20px 0 20px; } body > div > #cover{ background-color:green; height: 100px; top: 0; } body > div > #cover1{ background-color:blue; height:200px; } body > div > #cover2{ background-color:orange; }
 <div> <div id="cover"></div> <div id="cover1"></div> </div>

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

相关问题 如何使用 JavaScript 更改动态创建的元素的高度? - How do I change the height of dynamically created elements using JavaScript? 在页面上添加边距,也包括绝对/固定位置的元素 - Adding a margin to a page, including absolutely/fixed positioned elements too 如何允许用户移动绝对定位的div? - How do I allow the user to move an absolutely positioned div? 如何在html中创建一个绝对定位的div,该div会将其他元素像苹果的Pages一样推到一边? - How do I create a div in html which is absolutely positioned, which will push other elements to the side like in Apple's Pages? ElementFromPoint是否可用于绝对定位的元素? - ElementFromPoint for absolutely positioned elements? Javascript根据多个绝对定位的子元素的高度调整容器的大小 - Javascript to resize container based on height of multiple absolutely positioned child elements 绝对定位元素上的Mouseenter事件 - Mouseenter events on absolutely positioned elements 如何以编程方式判断两个绝对定位的元素是否重叠? - How to programmatically tell if two absolutely positioned elements overlap? 当容器中有绝对位置的元素时,如何扩展容器? - How to extend container when it has absolutely positioned elements in it? 如何计算绝对放置元素的容器的大小? - How to calculate the size of the container in which the elements are positioned absolutely?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM