Coder Social home page Coder Social logo

swupjs's Introduction

swupjs

WARNING: this repository is deprecated in favour of JS-plugin

Swupjs is an extension of swup, which modifies the module for use with JavaScript animations.

Swupjs only slightly modifies swup, where all the capabilities of swup remain the same, with only one exception - timing and animations are based on JavaScript, not CSS transitions. For more information about functionality and idea of swupjs, refer to swup documentation.

Installation

npm install swupjs

or include the file from the dist folder

<script src="./dist/swupjs.js"></script>

How it works

Swupjs is enabled similarly as swup.

let options = {}
const swupjs = new Swupjs(options)

Animations option

To use your animations for page transitions, you first need to define the animation object.

animations: {
    '*': {
        out: function (next) {
            next()
        },
        in: function (next) {
            next()
        }
    }
}

The example above is the default setup in swupjs and defines two animations, where out is the animation (function) being executed before content replace, and in is animation being executed after the content is replaced. As one may have noticed, one parameter is passed into both functions. Call of next function serves as an indicator, that animation is done - so in a real world next() would be called as a callback of the animation. As you can see, by default no animation is being executed and next() is called right away.

Note: Although the whole purpose of swup is to enable page transitions, this can still enhance your user experience even without the animation as it can shorten your load time drastically when preload and/or cache options are set to true. In most cases, your page change should be immediate without any wait time.

out: function (next) {
    setTimeout(next, 2000)
}

In the example above, next function is called after two seconds, which means that swupjs would wait two seconds (or any time necessary for the load of the new page content), before continuing to the content replace.

Animation object needs to be passed as a part of your options.

let options = {
    animations: {
        '*': {
            out: function (next) {
                next()
            },
            in: function (next) {
                next()
            }
        }
    }
}
const swupjs = new Swupjs(options)

Basic usage with tools like GSAP would look something like the following:

let options = {
    animations: {
        '*': {
            in: function(next){
                document.querySelector('#swup').style.opacity = 0;
                TweenLite.to(document.querySelector('#swup'), .5, {
                    opacity: 1,
                    onComplete: next
                });
            },
            out: function(next){
                document.querySelector('#swup').style.opacity = 1;
                TweenLite.to(document.querySelector('#swup'), .5, {
                    opacity: 0,
                    onComplete: next
                });
            }
        },
    }
}

const swupjs = new Swupjs(options);

Choosing the animation

As one may have noticed, the name of animation object in options is defined as '*', which serves as a fallback or base set of animations used throughout the website. Custom animations can be defined for a transition between any pages, where the name is defined by [starting route]>[final route].

...
'homepage>documentation': {
    out: function (next) {
        next()
    },
    in: function (next) {
        next()
    }
}
...

The animation above would be executed for the transition between homepage (/) and documentation page (/documentation). Notice that for the lack of route, keyword "homepage" is used. Any of the two routes can also be defined by wildcard symbol (homepage>* or *>documentation). The most fitting animation is always chosen.

Custom animation to dynamic pages

Similarly to swup, where data-swup-transition attribute of the clicked link is used for assigning a special class to the html tag, swupjs uses the same attribute for choosing custom animation. In case the attribute is defined on clicked link, swupjs also tests the animation object for the content of the data attribute. So following attribute data-swup-transition="post" would end up in *>post being executed.

...
    '*': {
       ...
    },
    '*>documentation': {
       ...
    },
    '*>post': {
       ...
    }
...

swupjs's People

Contributors

dependabot[bot] avatar gmrchk avatar hirasso avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

swupjs's Issues

Can get Swup to work!!

Its a shame you didn't include a barebones example.

I'm trying to see swup is working by using the example of:

out: function (next) { setTimeout(next, 2000) }

My code is:
let options = { animations: { '*': { out: function (next) { setTimeout(next, 9000) }, in: function (next) { next() } } } } const swupjs = new Swupjs(options)

The result is no waiting 9 sec just loads the second page right away.

Also, I'd like to use anime.js to create the page transitions but honestly don't know how I'd do that.
I've read the documentation a few times now but none the wiser.

Error in Chrome (possibly others) from getDataFromHtml.js

Hi there,

This is a weird one and I'd love to try and figure out the root cause.

A production site has just become unusable for what appears to be only Chrome users on MacOS, which is what I saw the error on, that would have me think that it was a browser update but I have also a report that it affected Firefox on PC. I verified and diagnosed on Chrome Version 73.0.3683.75 (Official Build) (64-bit)

Error

Uncaught TypeError: Cannot read property 'className' of null

Which referenced this file https://github.com/gmrchk/swup/blob/master/src/modules/getDataFromHtml.js

From line 23

pageClass: fakeDom.querySelector('#swupBody').className

Which explicitly needs a Swup id of swupBody. I was using the default of #swup.

Fix, sort of

I managed to fix it by changing my id to swupBody and specifying the elements option in options.

However, I can't use any other ID. If I don't use #swupBody is breaks.

There hasn't been a change to my sites codebase since last week so I'm a little baffled what has occurred.

I see some reference in the docs towards needing to alter the getDataFromHtml.js file but I'm not clear on how it references my use case.

How I'm using Swup

Nothing fancy, using Greensock for some simple animations. If you need any more details please let me know.

Update

Now I've made this change I'm seeing pages in Safari which attempt to load but just puts this in the address bar:

typeerror:%20null%20is%20not%20an%20object%20(evaluating%20'document.body.querySelector('[data-swup="'%20+%20i%20+%20'"]').outerHTML%20=%20page.blocks[i]')

But this may only be loosely related as there are JS errors on some of the pages from an external source, would that cause Swup to totally crash? I have disabled Swup on the production site for the time being.

I very much doubt this the plugins issue by the way! But I'd love some help finding out what is going on.

Many thanks!
Mike

Choosing the animation

Hi, I have another question regarding prioritisation of animations in swupjs.

Say i have two animations defined. One which goes 'info>homepage', and another '*>homepage'.

From the info '/info/' page to the homepage '/', the 'info>homepage' animation should take precedence right? I'm finding that the wildcard * animation is overriding the 'info>hompage' animation in my website and I'm not sure why. It would be good to understand a bit better how this works, as the docs don't provide a lot of info.

Disable double click?

Is there a possibility to ignore double clicks? Maybe ignore clicks when it has class 'is-changing' or something? let me know thanks

Possible to detect which link was clicked?

Great library by the way. I've found it very powerful and reasonably straight forward to implement.

However, I'm integrating it into a wordPress site with a grid of images and getting stuck on an issue that seems more complicated than it needs to be.

I have a grid of images that link to posts. I'd like to animate elements between the pages starting with the element that was clicked. So I need to know which link was clicked so I can pass that as the target into an animation function.

As the event listener 'clickLink' is set to the document I can't use that. I've trying to write a workaround for some time now and running into more complexity than I expected.

So I thought I'd just check to see if I'm missing something and it's possible to just pass the clicked element into the animation function?

Slight flicker on animation out.

Everything is working great! However when I transition out of a page I turn my swup container to opacity 0 before turning it back to 1 when starting my transitioning in. The problem however is most of the times when the animation out finishes it seems as if the transition in starts. (making the content of the old page visible again but at like .1 opacity) But it then resets when the actual transition in starts.

If it's not clear what I mean be sure to ask me more details. It's not that easy to explain.
P.S: I'm using Anime.js

EDIT: The flicker seems to happen around the pageRetrievedFromCache event.

Example of page becoming slightly visible.
image

Delay action when clicking a link

Hi again,

Is there any way of delaying the Swup action when I click a link? I need to do a pre-transition before Swup's transition, so I would need that one to be finished before Swup starts doing it's thing. I could just remove link and have the href in a data attribute and load through loadPage, but can I specify the transition I want there like I do in links with the swup-class attribute?

Thanks!

Accessing the preloaded page from Cache

Hi there, first of all, thank you for your work on swup — It's been a joy to work with so far! I have a question about using the 'preloadPage' functon I was hoping you may be able to enlighten me about.

For a portfolio site I'm building, within a project page I'd like to reveal an image from the next project page (next #swup container) when hovering the 'next project' link (kind of like peeking into the next page). On clicking the link, I would then completely transition to the next project.

Currently, I am using the following to preload the next project into cache on the intial load:

swup.on('contentReplaced', function () {
  var nextProject = $('.next-project-title a').attr('href');
  defaultInit();
  swup.options.elements.forEach((portfolio) => {
      horizontalScroll();
      // once project loaded request and save next project to cache
      swup.preloadPage(nextProject)
  })
});

From here though, I'm struggling to work out how I could access the preloaded object to attach it to the DOM manually — is this achievable with swup? If you could point me in the right direction/advise I would be very grateful!

Best,
Andrew

Choosing animation for all subpages

Hi,
I'm currently working on a project and decided to go with swup for my page transitions. However I've encountered a major setback - I can't define an animation for multiple subpages.
For example:
Having a homepage and a set of products where each product has it's own page under /product/ I'd like to have an animation definition for homepage>product/* which would get used for all the product pages.

Would you consider adding this sort of regexp feature to the animation definitions?

Blacklist a class?

Hi,

First of all, thank you for this amazing library. I'm working on a new project using it and it's going great.

I would have one question, is it possible to blacklist a href by using a class or id on it?

for example <a id="dontswup" href="/test">clickme</a>

I don't want swup to activate when he sees the id "dontswup"

If not how can i solve this ?

reload scripts or functions

Hello,
I noticed that using this fantastic script on some pages do not load some of my functions containing animations.
is there a way (other than to call incoming functions for new pages) to force loading?
thank you

Swup isn't working

Hello hello,

This sounds like a wonderful tool. But for some reason can't figure why this is not working for me. I tried some basic setup as follows :

index.html
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <div>
        <main id="swup">
            <h1>This is homepage</h1>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore
                magna aliqua.
            </p>
            <a href="./about.html">Go to other page</a>
        </main>
    </div>
    <div class="loading">We are loading...</div>

    <script src="./js/libs/swupjs.js"></script>
    <script src="./js/main.js"></script>
</body>

</html>
about.html
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <div>
        <main id="swup">
            <h1>This is about page</h1>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore
                magna aliqua.
            </p>
            <a href="./index.html">Go to other page</a>
        </main>
    </div>
    <div class="loading">We are loading...</div>

    <script src="./js/libs/swupjs.js"></script>
    <script src="./js/main.js"></script>
</body>

</html>
main.js
(function() {
    var options = {
        debugMode: true,
        animations: {
            '*': {
                out: function (next) {
                    console.log("Out Function")
                    setTimeout(function () {
                        console.log("Timeout Out")
                        next()
                    },1000)
                },
                in: function (next) {
                    console.log("Timeout In")
                    next()
                }
            }
        }
    }
    var swupjs = new Swupjs(options)
 })();

But nothing happens. Clicking on the links redirects right away. Nothing show up in the console. But the debug mode shows that Swup is enabled. What am I doing wrong?

Wait until content is fully loaded to start transition 'in'

The title says it all. Is there a way to not start the transition 'in' until the content is fully loaded?
Like, can I wait for the 'swup:PageView' event inside the 'in'.

Right now I find myself triggering the transition in just to watch the page finalize the loading when the fade ends.

Thanks! Lovin SwupJs even more than the css version ;) GJ
Works like charm with GreenSock

MAMP vs Real Hosting

Hi!
on MAMP your animations works fine! (homepage > pages)

animations: {
    'homepage*>': {
        // this is used for transition from homepage to any other page
    },
    '': {
        // this is used for any other transition, between the subpages OR from subpage to homepage
    },
}

but NOT online :((((
works only *> for all pages
this is the error
www.website.com/Cannot%20tween%20a%20null%20target.

UPDATE:
I understand what it depends on:
I basically put the site in a subfolder and not in the root root. Is there a way (especially when we have sites that reside in subfolders such as www.mysite.com/site), to solve the problem?

unfortunately I need to have a different animation for each page both incoming and outgoing.
if I work only with default states (*>) is there a way, even using only the if else at least on the entry pages?

thank you for precious help

"Object doesn't support this action" error in IE11

Hi there! Got this absolutely marvellous library working amazingly well on a site, used the original version to start but moved to the JS version to get a bit more control over the animations.

In IE11 (Version 11.0.20 on Windows 10 via VirtualBox) I'm getting this error when creating a new instance of Swupjs - new Swupjs(), at first, given where the error is I thought it might be something to do with the custom events I am using, but deleting those didn't affect anything, it's just happens on init. The error is:

Object doesn't support this action which, according to IE dev tools is coming from this line:

// trigger event on document with prefix "swup:"
var event = new CustomEvent('swup:' + eventName, { detail: eventName });

My options are just using the defaults.

Not sure if it's relevant but all my scripts are compiled using Babel and I have the babel polyfill also.

Hopefully that's enough to go on! If there is any more information I can give you please let me know.

Many thanks,
Mike

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.