简体   繁体   English

检测无序列表中的换行符

[英]Detecting line-breaks in an unordered list

Concerning horizontal, unordered lists that utilize :before or :after insertion of dividers between list items. 关于使用:before或:after在列表项之间插入分隔符的水平无序列表。

I had resigned myself to spot checking at predetermined intervals whether or not a list's children had broken into another line, and was somewhat satisfied that at least the list looked right at my tweak points. 我已经辞职自己发现以一定间隔列表的子项是否已断成另一条线检查,而且比较满意的,至少在名单上我的调整点向右看。

Bit of a short-sighted solution , however, as problems arose whenever my client wanted to change the content of any given child. 但是,这是一个短视的解决方案 ,因为只要我的客户想要更改任何给定孩子的内容,就会出现问题。 Pretty tedious work :\\ The problem was further compounded when it was decided to switch to a different divider symbol, as each page that contained one of these lists had custom CSS embedded that would also need to be changed. 相当繁琐的工作:\\当决定切换到不同的分隔符时,问题变得更加复杂,因为包含这些列表之一的每个页面都嵌入了自定义CSS,因此也需要进行更改。 Long story short, I had pretty much given up trying to keep it clean. 长话短说,我几乎放弃了保持清洁的努力。

EDIT: 编辑:

To be clear, the issue is that there is no clear way in CSS (that I'm aware of) to detect a line-break in a list. 明确地说,问题在于(我知道)CSS中没有明确的方法来检测列表中的换行符。 If you insert dividers between list items, and the list breaks into a new line, you're left with the last item on the first line with a divider hanging out on the end. 如果在列表项之间插入分隔线,并且列表分成新行,则第一行的最后一项将留在最后,而分隔线则悬在末端。 Unsightly. 不雅观。 The only other way I've been able to tackle it before is with tweak points in CSS, but that is less than an ideal fix, because if the list changes at all, you have to go back and re-check the tweak points. 我以前能够解决的唯一其他方法是使用CSS中的调整点,但这并不是理想的解决方法,因为如果列表完全改变,则必须返回并重新检查调整点。

EDIT2: EDIT2:

Attaching image to help clarify intention 附上图片以帮助阐明意图

在此处输入图片说明

I've come up with a solution today that I'm pretty satisfied with, so I thought I'd share it for posterity's sake, in case anyone else out there is also struggling with a similar problem. 今天,我想出了一个令我非常满意的解决方案,所以我想为后代而分享它,以防万一其他人也遇到类似的问题。 I'm also very interested in solutions anyone else may have come up with, especially if the solution is CSS-only. 我对其他任何人都想出的解决方案也非常感兴趣,尤其是如果解决方案仅适用于CSS。

CSS: CSS:

.classname {
    padding-left:0;
}
.classname.centered {
    text-align:center;
}
.classname.left {
    text-align:left;
}
.classname li {
    display:inline;
    margin:0;
    padding:0;
    white-space: nowrap;    
}
.classname li:not(.last-on-line):not(:last-of-type):after {
    content:"|";
    margin:0 0.25em 0 0.45em;
}

.classname.centered li:first-of-type,
.classname.centered li.first-on-line {
    padding-left:0.8875em;
}

.classname.centered li:last-of-type,
.classname.centered li.last-on-line,
.classname.left li:last-of-type,
.classname.left li.last-on-line {
    padding-right:0.8875em;
}

JS: JS:

function determineListLineBreak() {
    //..
    var matchingDisplayTypes = ['inline','inline-block'];
    //..
    $('ul').each(function() {
        //..
        var listParent = this;
        //..
        if ($(this).hasClass('classname') == true && $(this).find('li').length > 1) {
            //..
            if ($.inArray($(this).find('li').css('display'), matchingDisplayTypes) > -1) {
                //..
                var listItems = $(this).find('li'),
                    listItemCount = $(this).find('li').length,
                    listItemLoopCount = 0;
                //..
                $(listItems).each(function() {
                    //..
                    listItemLoopCount = listItemLoopCount + 1;
                    //..
                    $(this).removeClass('first-on-line').removeClass('last-on-line');
                    //..
                    if (listItemLoopCount == listItemCount) {
                        //..
                        $(listItems).each(function() {
                            //..                    
                            var listItem = this;
                            //..
                            var itemToCompare,
                                compareDirection;
                            //..
                            if ($(listItem).next().length) {                        
                                itemToCompare = $(listItem).next(),
                                compareDirection = 'next';
                            }
                            else {
                                itemToCompare = $(listItem).prev(),
                                compareDirection = 'prev';
                            }
                            //..
                            if ($(listItem).offset().top != $(itemToCompare).offset().top) {
                                //..
                                if (compareDirection == 'next') {
                                    $(listItem).addClass('last-on-line');
                                    $(itemToCompare).addClass('first-on-line');
                                }
                                else {
                                    $(listItem).addClass('first-on-line');
                                    $(itemToCompare).addClass('last-on-line');
                                }
                            }
                        });
                    }
                });
            }
        }
    });
}

$(document).ready(function() {
    determineListLineBreak();
});

$(window).resize(function() {
    setTimeout(determineListLineBreak, 0)
});

HTML: HTML:

<ul class="classname left">
    <li>This is a list item</li>
    <li>This is another list item</li>
    <li>An item?</li>
    <li>Yes.</li>
    <li>Are we done?</li>
    <li>This is getting boring</li>
</ul>
<ul class="classname centered">
    <li>This is a list item</li>
    <li>This is another list item</li>
    <li>An item?</li>
    <li>Yes.</li>
    <li>Are we done?</li>
    <li>This is getting boring</li>
</ul>

Explanation for those that need it: 对那些需要它的人的解释:

All I did was measure the distance to the top of the page from each list item, and then compare the measurements of a list item and the list item immediately before it. 我所做的只是测量到每个列表项到页面顶部的距离,然后比较列表项和紧接其之前的列表项的测量值。 If the distance is different, it adds a class to the list item before the line-break (I also included the addition of a class to the list item that starts the new line, for those who might need that). 如果距离不同,则会在换行之前在列表项中添加一个类(对于可能需要的人,我还包括在列表项中添加一个类以开始新行)。 CSS takes care of the rest. CSS负责其余的工作。

Thanks for reading, hope this helps you! 感谢您的阅读,希望对您有所帮助!

EDIT: 编辑:

So I found out that as you get close to breaking the line, you sometimes see the item on the edge taking on both classes, with the item that should no longer be last having the "last class" as well. 因此我发现,当您接近打破界限时,有时您会看到边缘的项目同时属于这两个类别,并且该项目也不应再最后具有“最后一个类别”。 I think this has to do with the space that the divider takes up is "disappearing", allowing what would have gone to the next line to come back "up". 我认为这与分隔符占用的空间“消失”有关,从而使本应转到下一行的内容又可以“返回”。

The workaround I'm employing now seems a bit much. 我现在使用的解决方法似乎有点多。 I feel like this should be easier and I'm just missing it. 我觉得这应该更容易些,我只是想念它。 I wasn't paying close enough attention the first time I checked it over (wasn't resizing slow enough), but now the added JS/CSS seems to have it nearly perfect. 第一次检查时,我没有给予足够的关注(调整大小的速度不够慢),但是现在添加的JS / CSS似乎已经非常完善。 I can still break it if I really try though, so I could use some help, or if anyone has a better solution, I'd appreciate it. 如果我确实尝试过,我仍然可以打破它,以便可以使用一些帮助,或者如果有人有更好的解决方案,我将不胜感激。

EDIT2: EDIT2:

So, turns out this can be broken too, I'll have to keep working on it.. This gets you close if you aren't super picky. 因此,事实证明这也可能被破坏,我将不得不继续努力。如果您不是超级挑剔的人,这将使您接近。 Good enough for me for now 现在对我来说足够好

EDIT3: EDIT3:

I've edited the code more, to fix some things and to include left-aligned orientation. 我对代码进行了更多编辑,以修复某些问题并包括左对齐的方向。

Fiddle http://jsfiddle.net/u5uzg02w/ 小提琴 http://jsfiddle.net/u5uzg02w/

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

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