简体   繁体   English

使用Javascript动态调整HTML元素的位置

[英]Dynamically adjust positioning of an HTML element using Javascript

I have a script that will increase and decrease the size of a period. 我有一个脚本,可以增加和减少周期的大小。 The end result is to have a period that will increase and decrease in size while remaining stationary. 最终结果是要有一个大小会增加和减小而又保持不变的周期。

The current problem I am having is when the size is increased (or decreased), the element will move down and up the page (respectively). 我当前遇到的问题是,当大小增加(或减小)时,元素将分别在页面中上下移动。

To counter this, I have attempted to adjust the padding as the font size is increased. 为了解决这个问题,我试图随着字体大小的增加来调整填充。 This adjustment doesn't work, and the element continues to move. 该调整无效,并且元素继续移动。

Here is my Javascript: 这是我的Javascript:

window.dotFluxOut = true;
var i = 1;
var pad = 50;

var dots = window.setInterval(function () {
    var wait = document.getElementById("wait");

    if (window.dotFluxOut) {
        wait.style.fontSize = i + "px";
        wait.style.padding = (pad - i) + "px";
        i++;
    } else {
        wait.style.fontSize = i + "px";
        wait.style.padding = (pad - i) + "px";
        i--;
    }

    if (parseInt(wait.style.fontSize.replace("px", "")) > 180) {
        window.dotFluxOut = false;
    } else if (parseInt(wait.style.fontSize.replace("px", "")) < 3) {
        window.dotFluxOut = true;
    }

}, 5);

Here is my HTML: 这是我的HTML:

<p id="wait" align="center" style="font-size: 160px">.</p>

EDIT: 编辑:

If you would like to see it running: JSFiddle 如果您希望它运行: JSFiddle

您需要将点容器的行高设置为等于其高度。

I believe the problem is that the line-height is automatically changing with with font-size as is the margin associated with the 我相信问题在于,行高会随着字体大小而自动更改,与

tag. 标签。

If you initially set to line height to be the slightly bigger than the maximum font size this should help resolve the issue. 如果最初将行高设置为略大于最大字体大小,这应该有助于解决此问题。

eg 例如

<p id="wait" align="center" style="font-size: 160px; line-height:200px; margin:0">.</p>

The second problem is that the white space above and below the period/full-stop character grows with the change in font-size ie to allow for the tall characters (bdfhijklt) and the characters with tails (gjpqy). 第二个问题是,句号/句号上方和下方的空白会随着字体大小的变化而增加,即允许使用高字符(bdfhijklt)和带尾字符(gjpqy)。

The best I have come up with is: 我想出的最好的是:

<p align="center" style="margin:0"><span id="wait" style="font-size:160px">.</span><span style="font-size:200px">&nbsp;</span></p>

Hope it helps. 希望能帮助到你。

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

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