Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding elements on('flatdoc:ready') causes scroll bar to desync #111

Open
miranda1000 opened this issue Dec 20, 2022 · 0 comments
Open

Adding elements on('flatdoc:ready') causes scroll bar to desync #111

miranda1000 opened this issue Dec 20, 2022 · 0 comments

Comments

@miranda1000
Copy link

miranda1000 commented Dec 20, 2022

Adding elements on('flatdoc:ready') causes scroll bar to desync (it shows like you're below where you really are).

Note: The elements that I'm adding are not <h1>, <h2>...

How to reproduce

  1. Add elements on('flatdoc:ready'):
    $(document).on('flatdoc:ready', function() {
        fetch(somewhere, { method: 'get', mode: 'no-cors', referrerPolicy: 'no-referrer' })
            .then(res => res.text()) // toString
            .then(html => new DOMParser().parseFromString(html, "text/html"))
            .then(element => {
                const target = document.getElementById(where_to_add);
                target.parentNode.insertBefore(element, target.nextSibling); // insert below target
            });
    }
    

How to solve

I've found a solution, but I don't know if it can have consequences.

  1. Once the element has been added (below the last then), add:
    .then(_ => {
        $("h2, h3").scrollagent(function(cid, pid, currentElement, previousElement) {
            if (pid) {
                $("[href='#"+pid+"']").removeClass('active');
            }
            if (cid) {
                $("[href='#"+cid+"']").addClass('active');
            }
        });
    });
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant