【问题标题】:Javascript/jQuery get true width and position of float affected element?Javascript/jQuery 获得浮动影响元素的真实宽度和位置?
【发布时间】:2013-05-15 01:45:12
【问题描述】:

是否可以获得浮动影响元素的宽度(使用 javascript 或 jQuery)?当文本由于浮动图像而被推倒时,是否可以获得其位置和真实宽度?我附上了一张图片以便更好地解释。

代码示例,

<div>
    <img style="...float: left"/>
    <h1>A title!</h1>
    <p>Text!</p>
    <h1>New header added.</h1>
</div>

Picture

我需要从箭头开始找到宽度,(灰色框是图像)(虚线是根据Firefox检查模式的宽度)。

如果可能,我想避免更改所有元素的显示类型。 谢谢!

【问题讨论】:

  • 您实际上并不需要像“将

    标签放在图像右侧”那样“找到宽度”,这不是您想要的解决方案吗?

  • 是的,这可能是一个解决方案,但我认为这将涉及更改所有元素 css。我宁愿计算宽度,而不必对 html 或 css 进行修改。

标签: javascript jquery html width floating


【解决方案1】:

我参加聚会有点晚了,但我遇到了类似的问题,并想出了一个解决方案,(到目前为止)似乎在这个问题的所有情况下都有效。我喜欢这个解决方案,因为据我所知,它独立于浮动元素工作——你所需要的只是你想要获得其真实宽度/位置的元素,仅此而已。出于速度目的,我使用纯 Javascript 完成了它,但如果您愿意,可以使用 jQuery 和单独的 CSS 样式表轻松简化它。

//Get the rendered bounding box for the content of any HTMLElement "el"
var getLimits = function(el) {
    //Set a universal style for both tester spans; use "!important" to make sure other styles don't mess things up!
    var testerStyle = 'width: 0px!important; overflow: hidden!important; color: transparent!important;';

    //Create a 'tester' span and place it BEFORE the content
    var testerStart = document.createElement('SPAN');
    testerStart.innerHTML = '|';
    var testerFloat = ' float: left!important;';
    testerStart.setAttribute('style', testerStyle + testerFloat);

    //Insert testerStart before the first child of our element
    if (el.firstChild) {
        el.insertBefore(testerStart, el.firstChild);
    } else {
        el.appendChild(testerStart);
    }

    //Create a 'tester' span and place it AFTER the content
    var testerEnd = document.createElement('SPAN');
    testerEnd.innerHTML = '|';
    testerFloat = ' float: right!important;';
    testerEnd.setAttribute('style', testerStyle + testerFloat);
    el.appendChild(testerEnd);

    //Measure the testers
    var limits = {
        top: testerStart.offsetTop,
        bottom: testerEnd.offsetTop + testerEnd.offsetHeight,
        left: testerStart.offsetLeft,
        right: testerEnd.offsetLeft
    }

    //Remove the testers and return
    el.removeChild(testerStart);
    el.removeChild(testerEnd);
    return limits;
};

因此,在您的情况下,代码将是:

var paragraphBoundingBox = getLimits($('div>p').get(0));

需要注意的几点:

1) 如果您使用的是 RTL 语言,则浮动方向会反转

2) 输出对象中的所有四个边缘位置都是相对于 el.offsetParent - 使用this handy 函数可以找到它们相对于文档的位置。

【讨论】:

    【解决方案2】:

    首先,“全宽”就是真正的宽度。

    你可以看这张图,它可以帮助你理解为什么受影响元素的真实宽度和真实位置是firefox告诉你的。

    http://i.stack.imgur.com/mB5Ds.png

    要获得浮动图像向右推动的内联文本的宽度,除了使用全宽减去浮动图像的宽度之外没有其他好方法。

    var w = $('p').width() 
            - $('img').width() 
            - $('img').css('margin-left').replace("px", "")
            - $('img').css('margin-right').replace("px", "")
            - $('img').css('padding-left').replace("px", "")
            - $('img').css('padding-right').replace("px", "")
            - $('img').css('border-left-width').replace("px", "")
            - $('img').css('border-right-width').replace("px", "");
    

    【讨论】:

    • 我想我可能不得不做这样的事情,谢谢菲尼克斯。我最终制作了一个循环,查看所有元素,检测它们是否真的在图像旁边(使用位置和高度),然后使用图像宽度计算我正在寻找的宽度。它似乎工作可靠,谢谢。
    猜你喜欢
    • 2019-03-16
    • 1970-01-01
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    • 2013-12-13
    • 2012-08-08
    相关资源
    最近更新 更多