修剪面包屑

发布于 2024-09-25 14:34:33 字数 344 浏览 0 评论 0原文

我目前正在努力解决修剪面包屑导航的问题,这样它就不会破坏我网站的设计。

问题如下:面包屑导航具有固定宽度(假设为 900 像素) - 因此,如果用户导航到其位置导致面包屑大于 900 像素的项目,我必须修剪它以适合设计。

所以,我陷入困境的部分是:我如何决定修剪多少以及修剪哪里?我发现我可以修剪面包屑中间溢出的文本,这样就会导致

一些>导航>>那> ...>>是>太>长

但我如何决定在哪里剪切呢?我怎样才能保护元素的锚点不被修剪?!

我真的很困惑,有没有可以接受的方法来处理此类问题?

I'm currently struggling with the problem of trimming a breadcrumb navigation so it doesn't destroy my site's design.

The problem is the following: The breadcrumb navigation has a fixed width (let's say 900px) - So, if the user navigates to an item whose location results in a breadcrumb that is larger than 900px I'd have to trim it to fit into the design.

So, the part where I'm stuck is this: How can I decide how much to trim and where to trim? I figured out that I could just trim the overflow of text in the middle of the breadcrumb, so it would result in

Some > Navigation > That > ... > is > too > long

But how can I decide where to cut? And how will I be able to preserve the anchors of the elements from being trimmed?!

I'm really stuck on this, is there any accepted way to deal with such issues?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天气好吗我好吗 2024-10-02 14:34:33

保留根元素,删除以下元素,直到面包屑适合。我实际上建议您在 JavaScript 中执行此操作,因为这样您就可以使用方法来计算面包屑的像素宽度,而在 PHP 中,您必须使用固定数量的字符作为断点,这总是会导致不同的长度。无论如何,使用 JS 对于 SEO 等来说会更好,因为链接仍然存在,只是隐藏了。

假设您有一个简单的列表元素作为面包屑:

<ul id="breadcrumb">
    <li><a href="/">Home</a></li>
    <li><a href="/products/">Products</a></li>
    <li><a href="/products/hats/">Hats</a></li>
    <li><a href="/products/hats/large/">Large</a></li>
    <li><a href="/products/hats/large/red/">Red</a></li>
    <li><a href="/products/hats/large/red/round/">Round</a></li>
</ul>

然后您可以像这样修剪长度(使用 jQuery):

if($('#breadcrumb').width() > 900) {
    $('#breadcrumb li:first').after('<li>...</li>');

    while($('#breadcrumb').width() > 900) {    
        $('#breadcrumb li').eq(2).remove();
    }
}

一个简单的示例,可能不起作用,但应该给您一些想法。

顺便说一句,如果你想在 PHP 中进行修剪,如果你想保持简单,你必须在面包屑形成时进行。如果您之后开始修剪,则必须诉诸一些相当复杂的正则表达式或在项目中包含某种 DOM 解析器来保持锚标记完整。

Keep the root element, remove following elements until the breadcrumb fits. I'd actually recommend you to do this in JavaScript as then you'd have methods to calculate the pixel width of the breadcrumb, whereas in PHP you'd have to use a fixed number of characters as the break point which would always result in different lengths. Using JS would be better anyways for SEO and such, as the links would still be there, only hidden.

Assuming you have a simple list element as your breadcrumb:

<ul id="breadcrumb">
    <li><a href="/">Home</a></li>
    <li><a href="/products/">Products</a></li>
    <li><a href="/products/hats/">Hats</a></li>
    <li><a href="/products/hats/large/">Large</a></li>
    <li><a href="/products/hats/large/red/">Red</a></li>
    <li><a href="/products/hats/large/red/round/">Round</a></li>
</ul>

You can then trim the length like this (using jQuery):

if($('#breadcrumb').width() > 900) {
    $('#breadcrumb li:first').after('<li>...</li>');

    while($('#breadcrumb').width() > 900) {    
        $('#breadcrumb li').eq(2).remove();
    }
}

A simple example and probably doesn't work, but should give you some idea.

BTW, if you want to do the trimming in PHP, you have to do it when the breadcrumb is being formed if you want to keep it simple. If you start trimming afterwards, you'd have to resort to some pretty complex regular expressions or to including some kind of DOM parser in your project to keep the anchor tags intact.

悍妇囚夫 2024-10-02 14:34:33

我确实意识到我的解决方案比编程更面向设计,但就我个人而言,我不会修剪面包屑。相反,如果面包屑内容超过 900 像素,我会覆盖一个从完全不透明变为完全透明的 png,并简单地使其看起来像是面包屑的第一部分淡出。然后使用 jQuery,我将根据鼠标在面包屑上的位置向左或向右滚动面包屑(基于距右侧边距左侧的百分比,其中中心为 0% 或 100%)。以同样的方式,当光标更接近左边距时,我会在右侧使用相同透明 png 图像的翻转版本。

这是一个例子,可以更好地说明我的意思:

alt text

希望这有帮助!

I do realize that my solution is more design-oriented than programming, but personally, I wouldn't trim the breadcrumbs. Instead, if the breadcrumbs content exceed 900px, I would overlay a png that goes from fully opaque to full transparency and simply make it look like the first part of the breadcrumbs fades out. Then with jQuery, I would scroll the breadcrumbs either left or right, depending on the mouse position over it (based on percentage from left of right margin, where center is either 0% or 100%). In the same manner, I would then use a flipped version of the same transparent png image on the right side, for when the cursor is closer to the left margin.

Here's an example to better illustrate what I'm saying:

alt text

Hope this helps !

夜光 2024-10-02 14:34:33

假设您不希望面包屑在到达末尾时中断,这就是您要做的。

使用截断太长单词的函数。因此,决定最长的单词应该有多长。在某个点之后,您不再继续显示它,而是在单词末尾执行通常...,然后继续显示面包屑的下一部分。

看看这个页面。

http://www.the-art-of-web.com/php/截断/

Assuming you don't want the breadcrumbs to do a break when the reach the end, here is what you do.

Use a function that truncates words that are too long. So make a decision of how long the longest word should be. After a certain point instead of continuing to display it, you do the usually ... at the end of the word and continue on to the next part of the breadcrumb.

Take a look at this page.

http://www.the-art-of-web.com/php/truncate/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文