Coder Social home page Coder Social logo

uritzo / scroll-frame Goto Github PK

View Code? Open in Web Editor NEW

This project forked from artsy/scroll-frame

0.0 2.0 0.0 280 KB

Retain your scroll position between pages using an iframe. Especially helpful for infinite scrolling views.

License: MIT License

HTML 36.36% JavaScript 63.64%

scroll-frame's Introduction

scrollFrame

Retain your scroll position between pages using an iframe. Especially helpful for infinite scrolling views.

Example

Insert scroll-frame-head.js into your <head> tag across all views.

<html>
    <head>
        <script src='scroll-frame-head.js'></script>
    </head>
    <body>
        <h1>My detail page</h1>
    </body>
</html>

Then insert scroll-frame.js into the pages where you have an infinite scrolling list that needs to retain scroll position.

<html>
    <head>
        <script src='scroll-frame-head.js'></script>
    </head>
    <body>
        <!-- Some infinite scrolling list action about to go down here -->
        <ul id='my-infinite-scrolling-list'></ul>
        <script src='scroll-frame.js'></script>
    </body>
</html>

Finally use the scrollFrame function to indicate what links should retain their scroll position when clicked.

scrollFrame('#my-infinite-scrolling-list a');

For a working example check out the example folder which you can visit a live demo of here or run yourself by cloning the project and running npm run example.

How it Works

scrollFrame will hijack the user's click for elements that match the query selector you pass in and instead of reloading the page it will append a modal-like iframe that sits on top of your viewport and points to the element's href. It then uses HTML5 history APIs to make the back-button function as expected.

Caveats

  • scrollFrame will only open the next immediate page in an iframe (solving the simple use case of opening a detail page from an infinite scrolling list and then clicking back without losing your position). After clicking on a link inside the iframe the page refreshes to avoid going down a rabbit hole of stacked iframe modals and messy state.

  • Because scrollFrame uses HTML5 history APIs it does not work with older browsers and will simply not do anything when included. This should gracefully degrade as it'll just mean older browsers won't retain their scroll position.

Additionally

Scroll frame will add the scroll-frame-loading class to the <body> so you can set a loading state while the iframe is loading the page. As an example you may want to do something like body.scroll-frame-loading #scroll-frame-spinner { display: block }.

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.