简体   繁体   English

javascript - 如何理解两个不同的跨度是否在同一行?

[英]javascript - how to understand if two different spans are in the same line?

I've got a div styled with CSS's wrap property. 我有一个用CSS的wrap属性设计的div。 It has a fixed width and when content exceeds the maximum allowed it breaks into different lines. 它具有固定的宽度,当内容超过允许的最大值时,它会分成不同的行。 I'm building up an application where I need to understand whether different span elements are in the same line or not. 我正在构建一个应用程序,我需要了解不同的span元素是否在同一行中。 Let me give an example: 让我举个例子:

<div style="width: 300px;" class="wrap">
    <span style="font-size:15px;color:red;">Lorem ipsum dolor sit amet, consectetur adipiscing elit</span>
    <span style="font-size:17px;color:green;"> Vivamus in mauris dui, non posuere</span>
</div> 

A live example as well: jsFiddle 一个现实的例子: jsFiddle

I need to understand 3 cases: 我需要了解3个案例:

  • When the span starts in the same line but ends in a different line. 当跨度从同一行开始但以不同的行结束时。
  • When the span starts in a different line but ends in the same line. 当跨度以不同的行开始但在同一行结束时。
  • When the spans start and end in the same line. 当跨距开始和结束在同一行时。

What I've done till now: 到目前为止我做了什么:

  • If the spans have the same font-size it is easy to estimate with the offsetTop property. 如果跨度具有相同的字体大小,则很容易使用offsetTop属性进行估计。 I tried it and it works properly. 我试过了,它运作正常。
  • However, when spans have different font-sizes, they'll have different offsetTop as well. 但是,当跨度具有不同的字体大小时,它们也将具有不同的offsetTop When the font-size is smaller, the offsetTop increases and the vica-versa, therefore it is hard to estimate using this property. 当font-size较小时, offsetTop增加,反之亦然,因此很难使用此属性进行估算。
  • Finally, equalizing the font-sizes will not work as well because when you change the font-size a span that fits to one line may have to be divided into two lines (eg when font-size increases from 13px to 25px). 最后,均衡字体大小也不会起作用,因为当您更改字体大小时,适合一行的跨度可能必须分为两行(例如,当字体大小从13px增加到25px时)。

So, in short, how would you understand it using pure javascript? 那么,简而言之,您如何使用纯JavaScript来理解它? Here is a jsFiddle to play around. 这是一个可以玩的jsFiddle

Try this: 尝试这个:

<div class="wrap">
    <span id='first'>Lorem elit</span><span id='second'> Vivamus amet, consectetur</span>
</div> ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

<script>
    /* Check if span is broken into two lines or not */
    var firstBefore = document.createElement('span'),
        firstAfter = document.createElement('span'),
        secondBefore = document.createElement('span'),
        secondAfter = document.createElement('span');

    first.insertBefore(firstBefore, first.childNodes[0]);
    first.appendChild(firstAfter);
    second.insertBefore(secondBefore, second.childNodes[0]);
    second.appendChild(secondAfter);

    var firstBroken = (firstBefore.offsetTop === firstAfter.offsetTop) ? false : true,
        secondBroken = (secondBefore.offsetTop === secondAfter.offsetTop) ? false : true;

    /* Since the spans' baselines seem to be aligned by default, lets compared that instead */
    first.offsetBottom = (first.offsetTop + first.offsetHeight);
    second.offsetBottom = (second.offsetTop + second.offsetHeight);

    /* Main logic */
    if (first.offsetBottom === second.offsetBottom) {
        if (!firstBroken && !secondBroken) {
            alert('Begins and ends on the same line');
        } else if (firstBroken) {
            alert('Begins on different lines, but end on the same line');
        }
    } else {
        if (firstBroken) {
            alert('Begins and ends on different lines');
        } else {
            alert('Begins on the same line, but ends on different lines');   
        }
    }​​
</script>

Or play with it here: http://jsfiddle.net/RYEnY/1/ 或者在这里玩它: http//jsfiddle.net/RYEnY/1/

Be sure to run with different lengths of text. 一定要运行不同长度的文本。

It works by first inserting a hidden <span> at the beginning and end of each target <span> you want to detect. 它的工作原理是首先在要检测的每个目标<span>的开头和结尾插入一个隐藏的<span> The code then checks if the target <span> is broken up into two lines by comparing the offsetTop of the hidden spans. 然后,代码通过比较隐藏跨度的offsetTop来检查目标<span>是否被分解为两行。 Then, it grabs the offsets of the baseline of each target span and logically deducts whether the spans begin/end on the same lines or not. 然后,它抓取每个目标跨度的基线的偏移量并逻辑地推断跨度是否在相同的线上开始/结束。

you can do like that (it uses query): 你可以这样做(它使用查询):

var span1 = $('#span1');
var span2 = $('#span2');
var offsetTop1 = getOffsetTop(span1);//you've to count offsetTop there
var offsetTop2 = getOffsetTop(span2);
var height1 = span1.height();
var height2 = span2.height();

if (Math.abs((offsetTop1 + height1) - (offsetTop2 + height2)) < 1) { //you can increase accuracy
    // two spans are in the same line
}

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

相关问题 同一条javascript行中的两个不同的CSS类 - Two different css classes in the same javascript line 如何使用JavaScript将数字添加到不同的跨度? - How to add digits to different spans with JavaScript? 如何将跨度垂直保持在同一行? - How can I keep spans in the same line vertically? 想要合并 javascript 中具有相同键但不同值的两行 - Want to merge two line with same keys but different values in javascript 如何在同一个jQuery函数中获得不同范围的值? - How do I get the value of different spans in the same jQuery function? 如何使用量角器验证两个跨度在不同位置是否相等? - How to use protractor to verify if two spans are equal in different locations? Javascript createRange。 如何只用一个脚本选择不同的跨度? - Javascript createRange. How to select different spans with only one script? 我想将一个活动类添加到一个 div 上的第二个跨度,其中两个跨度与 javascript 具有相同的类 - I want to add an active class to second span on a div with two spans with the same class with javascript 我如何 plot JavaScript 在同一条线上的两个移动点 - How do I plot two moving points on the same line in JavaScript 量角器连接两个具有相同类名的跨度 - Protractor concatenate two spans with same class name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM