Skip to content

Target the posts counter in categories using CSS and jQuery

Let's Build It
  • For those who have delved as deep into styling NodeBB as I have, this is for you 🙂

    It’s relatively easy to achieve this look when a topic is updated (note the different coloured posts card)

    84655243-fa6c-4007-9588-944e4a4800e1-image.png

    Here, we simply target the below class

    body:not(.user-guest) li.unread .stats-postcount
    

    However, it’s not so simple an affair to achieve the same look in the category view as the two DIVs are identical and have no unique classes

    c4ee50a6-5f48-4cd3-a103-3a15cb9c6461-image.png

    0fe2c02c-1b7d-451c-a3c4-bbcb7ce21115-image.png

    To work around this, we can use a very simple function that adds an ID to the “posts” DIV as shown below (this is placed in /admin/appearance/customise#custom-js)

    $(document).ready(function() {
            $(window).on('action:ajaxify.end', function(data) {
        // Select the div using its class
        $('.card.card-header.border-0.p-2.overflow-hidden.rounded-1.d-flex.flex-column.align-items-center').each(function() {
            // Check if the div contains the word "Posts"
            if ($(this).text().trim().includes("Posts")) {
                // Assign the id 'catpost' to the div
                $(this).attr('id', 'updated-posts');
            }
        });
    });
    });
    

    This code will first look for the classes .card.card-header.border-0.p-2.overflow-hidden.rounded-1.d-flex.flex-column.align-items-center in the DOM, and if there, it will add the ID of updated-posts, meaning you can then target with the same CSS you are using in the recent view. See below

    c4e1119b-89dd-4cc0-b433-862d83c73782-image.png

    You’ll see that the ID has been added as required, and this means we can now adjust the original CSS so it looks like the below

    body:not(.user-guest) li.unread .stats-postcount, body:not(.user-guest) .unread #updated-posts
    

    NOTE: body:not(.user-guest) is being used so that the effect is not being applied to those who are not logged in as that wouldn’t make any sense - your mileage here may vary though.

    6d1ea244-a15d-449f-b674-db9e7063f1b4-image.png

    Doesn’t that look better? For the life of me, I can’t understand why these two elements do not have ID’s set already, but there you go.

    Enjoy.

  • Very great 😉


Related Topics
  • Threaded post support for NodeBB

    Let's Build It
    146
    50 Votes
    146 Posts
    20k Views

    Updated git for above change

    https://github.com/phenomlab/nodebb-harmony-threading/commit/14a4e277521d83d219065ffb14154fd5f5cfac69

  • Setup OGProxy for use in NodeBB

    Moved Let's Build It
    110
    21 Votes
    110 Posts
    10k Views

    @crazycells said in Setup OGProxy for use in NodeBB:

    are they cached for each user separately?

    No. It’s a shared cache

    @crazycells said in Setup OGProxy for use in NodeBB:

    additionally, this is also handling youtube videos etc, right?

    No. This is handled by nodebb-plugin-ns-embed

  • Footer bar add center text

    Solved Customisation
    41
    8 Votes
    41 Posts
    4k Views

    @phenomlab said in Footer bar add center text:

    div#console-nav-tab

    Ah ok test with bottom: 0px !important; idem

  • hover link effect

    Solved Customisation
    18
    6 Votes
    18 Posts
    910 Views

    @DownPW Looking at the underlying code, class start is being added on hover by jQuery in this function

    document.querySelectorAll(".button-gradient, .button-transparent").forEach((button) => { const style = getComputedStyle(button); const lines = document.createElement("div"); lines.classList.add("lines"); const groupTop = document.createElement("div"); const groupBottom = document.createElement("div"); const svg = createSVG( button.offsetWidth, button.offsetHeight, parseInt(style.borderRadius, 10) ); groupTop.appendChild(svg); groupTop.appendChild(svg.cloneNode(true)); groupTop.appendChild(svg.cloneNode(true)); groupTop.appendChild(svg.cloneNode(true)); groupBottom.appendChild(svg.cloneNode(true)); groupBottom.appendChild(svg.cloneNode(true)); groupBottom.appendChild(svg.cloneNode(true)); groupBottom.appendChild(svg.cloneNode(true)); lines.appendChild(groupTop); lines.appendChild(groupBottom); button.appendChild(lines); button.addEventListener("pointerenter", () => { button.classList.add("start"); }); svg.addEventListener("animationend", () => { button.classList.remove("start"); }); }); })

    The CSS for start is below

    .button-gradient.start .lines svg, .button-transparent.start .lines svg { animation: stroke 0.3s linear; }

    And this is the corresponding keyframe

    @keyframes stroke { 30%, 55% { opacity: 1; } 100% { stroke-dashoffset: 5; opacity: 0; } }

    It’s using both CSS and SVG, so might not be a simple affair to replicate without the SVG files.

  • Changing the look of recent cards

    Announcements
    1
    2 Votes
    1 Posts
    259 Views
    No one has replied
  • New message CSS problem

    Unsolved Customisation
    11
    2 Votes
    11 Posts
    662 Views

    @DownPW hi. Sorry for digging up an old post, but I’m going through items still unresolved and was looking to get an understanding of where you are currently with this?

  • 11 Votes
    23 Posts
    2k Views

    @DownPW No issues. Thanks to the Google Chrome “bug”, it does have some restrictions, but works fine other than that. I have an odd issue where it doesn’t work on Firefox mobile, but works fine on Firefox desktop.

  • [NODEBB] CSS Style Sheets SelectBox

    Locked Solved Customisation
    112
    24 Votes
    112 Posts
    14k Views

    @DownPW as discussed in PM

    Seems to have been solved with the new JS code that you added allowing the version CSS file change!!

    Cache problem therefore with the JS of the Switcher theme

    Based on this, I will close this thread and reference
    https://sudonix.com/topic/207/nodebb-help-for-my-custom-css/27