Streaming live at 10am (PST)

Reinitialize Webflow with Custom Code

Hey guys,

please forgive my “duplicate post” (Reinitialize Webflow IX2) but I’m raising this question here to maybe get help from a broader and “newer” audience. I guess my problem as also seen in the linked thread is a big one for most people and appereantly Webflow.ready(): does not seem to work anymore.

No matter the custom code library one uses this might be of interest to a brought audience how to make sure Weblow Interactions can still be triggered and work after for instance using something like barba.js v2.

barba.init({
  sync: true,
  
  transitions: [{
	
	async leave(data) {
      // ON LEAVE DESTROY WEBFLOW ???
	  Webflow.destroy();

      const done = this.async();

	  pageTransition();
      await delay(300);
      done();
	  console.log('leave');
    },
	
	async enter(data) {
        // ON ENTER INITIALIZE WEBFLOW ???
		var Webflow = Webflow || [];
        Webflow.push(function() {
            $('html').attr('data-wf-page', '5e8363a55808c6fd7f381e9b');
            window.Webflow && window.Webflow.destroy();
            window.Webflow && window.Webflow.ready();
            window.Webflow && window.Webflow.require('ix2').init();
            document.dispatchEvent(new Event('readystatechange'));
        });

        contentAnimation();
	  	console.log('enter');
    },

    async once(data) {
    	contentAnimation();
		console.log('once');
    }
	
  }]
});

In my case I’m linking to a site that would have scroll interactions on the page, but when placing the code above I have no chance to making it work. I tried a bunch of solutions layed out here in the forum but no chance.

Can anyone help here as this would be crucial for my project.

Thanks,
Matt


Here is my public share link: LINK
(how to access public share link)

1 Like

Are you trying to trigger a Webflow interaction to start and stop via your own code?

Hey,

I’m trying to start the interactions. I have custom page-transitions on the page with barbjs and then after the page transition I want to fire/trigger the webflow interactions, yes.

So to your specific question: YES.

Actually almost simpler then trigger A webflow interaction, I want to make sure all webflow interactions simply run normally.

OK, so do you have something to hook into when your barbjs transition has finished? If so then you can use some disgusting hackery in your code to trigger a Webflow interaction to start when you want it to.

In Webflow, create a random div block. Now set it to display none, and give it an ID. Something clear like trigger is good. Next go to this div block’s interaction settings and set your interaction to trigger when the div is clicked on.

Then in your code, do something like this.

<script>
    const trigger = document.getElementById('trigger');

    // barbjs hook, transistion has finsihed...
    trigger.click();
</script>

It pains me that there is no proper way to trigger or hook into Webflow interaction events and I am suggesting you do something like this. But it’s the only way…

Thank you, this is somehow super weird that something like that is only supported via “hackery”. But thanks anyway.

Any idea how I for instance could solve custom actions like on scroll reveal animations, etc.? I have the problem that I load the new page and no scroll reveal interactions work as if the wefblow scripts are not loaded in.

And I have one more general Webflow understanding problem:

If I work with something like barbajs and basically use the plugin to link from page to page via ajax webflow apperantly never knows on what page I am.

I have a console.log statement in my barba leave code for instance and I do the following:

I’m on /work and click on /about. On /work the console.log statement shows that it’s coming from work:249, but on /about it also shows that it’s coming from work:249 … so Webflow does not know on which page I’m on if there is no hardrefresh it seems.

You can try this for your main question.

@dram Thanks, but this is barbajs v.1 … I already tried what the sample has. The sample uses

Webflow.destroy();
Webflow.ready();
Webflow.require('ix2').init(); 

Barba v2. has a different syntax and works differently: See

barba.init({
    transitions: [{
      once(data) {
            console.log('once');
      },
  
      // basic style
      leave(data) {
            // create your stunning leave animation here
            console.log('leave');
            Webflow.destroy();
      },
  
      // basic style
      enter(data) {
            // create your stunning leave animation here
            console.log('enter');
            Webflow.destroy();
            Webflow.ready();
            Webflow.require('ix2').init();
      }
    }]
  });

This does not work, no matter how I try. No matter where I put the destroy, ready and init functions it never works. Also doesn’t matter if I use different event-hooks of barba, there is also “afterEnter” and “after” which is fired way later, but still no effect.

Damn. That’s too bad.

Yeah, but does no one “know” a solution, or is there no solution?
Again, I guess this is not connected to barba js, but generally a “problem” I suspect.

Probably could ask someone from WF like @danro who had some input in the previous topic.

Webflow IX2 does not provide any way to hook into the event life cycle or to trigger an animation to start via custom code. At least not that I’ve ever found, or that is documented anywhere. You could try to unminify and unobfuscate their code to see if you can find a solution. Please report back if you get anywhere with that as I’d love to be able to solve this too :joy:

The only option I have ever been able to use is the spoofed click on a div trigger hack.

I would say that what you want to do is not going to work with Webflow.

This seems like somewhat of the same problem, but someone in another forum topic seems to be able to reintialize PJAX (wich is part of the same library as barba?). Not sure if this is any help, but perhaps that solution might also work for barba.js?

Seamless pagination with "pjax" @mtmt @jasondark anyone able to confirm this perhaps?

How are you getting the ‘data-wf-page’ attribute? I may be wrong here, but it seems like each page within your Webflow project has a unique ‘data-wf-page’ attribute, and that reinitializing interactions uses that attribute to determine which interactions and elements to target. As a result, I believe that you need to grab the ‘data-wf-page’ attribute from the destination page, then set the current page data-wf-page attribute to match the destination one. I was able to do something like that using the pjax library, and seem to be having success with it so far.

All of this is very interesting, but it seems that many people like myself use webflow to avoid heavy level javascript and coding.

Say for example, i use WF interactions and want to add the interactions to my wordpress site because webflow interactions are so great and easy – does anyone have a simple solution/tutorial on what to do?

Would be great if webflow had some advanced tutorials specific to solutions such as this and others for people who export code. does not even need to be video just a place on github would be fine.

or even codepen.

sg

Hi @mtmt :wave: sorry I’m late to the party.

After looking over the Barba.js docs here: https://barba.js.org/docs/getstarted/custom-code/

It seems that you should be able to do something like this:

barba.init({
  views: [{
    enter(data) {
      // Update page ID to the next page
      const pageId = ''; // may need to query data.next.html for data-wf-page attribute
      document.documentElement.setAttribute('data-wf-page', pageId);
      
      // Reinitialize webflow modules
      Webflow.destroy();
      Webflow.ready();
      Webflow.require('ix2').init();
    }
  }]
});

Keep in mind that the data-wf-page is required for IX2 to be able to target certain elements on the page. You may be able to bypass this requirement if you strictly use class targets in IX2 settings.