Top Related Projects
π€ Detection of elements in viewport & smooth scrolling with parallax.
Turbolinks makes navigating your web application faster
Versatile and extensible page transition library for server-rendered websites π
The speed of a single-page web application without having to write any JavaScript
A lightweight script to animate scrolling to anchor links.
The most versatile JavaScript typewriter effect library on the planet.
Quick Overview
Barba.js is a small and flexible JavaScript library that helps create fluid and smooth transitions between your website's pages. It reduces the delay between user actions and website responses, making navigation feel faster and more seamless. Barba.js works by preventing the default browser page load and using AJAX to fetch new content.
Pros
- Improves perceived loading times and creates a smoother user experience
- Easy to integrate with existing projects and works with any back-end language
- Highly customizable with hooks and events for fine-grained control
- Lightweight and has no dependencies
Cons
- Requires JavaScript to function, which may impact SEO if not implemented correctly
- Can be complex to set up for more advanced transitions and animations
- May require additional work to handle browser history and deep linking
- Not suitable for all types of websites, especially those with complex server-side rendering
Code Examples
- Basic usage:
import barba from '@barba/core';
barba.init();
This code initializes Barba.js with default settings.
- Custom transition:
import barba from '@barba/core';
barba.init({
transitions: [{
name: 'opacity-transition',
leave(data) {
return gsap.to(data.current.container, {
opacity: 0
});
},
enter(data) {
return gsap.from(data.next.container, {
opacity: 0
});
}
}]
});
This example defines a custom transition using GSAP for animation.
- Hooks usage:
import barba from '@barba/core';
barba.hooks.beforeEnter(() => {
// Do something before entering a new page
});
barba.hooks.after(() => {
// Do something after the transition is complete
});
This code demonstrates how to use Barba.js hooks to execute custom logic during transitions.
Getting Started
To get started with Barba.js:
-
Install the library:
npm install @barba/core
-
Import and initialize Barba in your JavaScript file:
import barba from '@barba/core'; barba.init();
-
Wrap your main content in a
data-barba="wrapper"
element and each page's content in adata-barba="container"
element:<div data-barba="wrapper"> <div data-barba="container"> <!-- Your page content here --> </div> </div>
-
Customize transitions and add hooks as needed for your specific use case.
Competitor Comparisons
π€ Detection of elements in viewport & smooth scrolling with parallax.
Pros of Locomotive Scroll
- Focuses specifically on smooth scrolling and parallax effects
- Provides advanced scroll-based animations and transformations
- Offers easy integration with existing projects
Cons of Locomotive Scroll
- Limited to scroll-based interactions, unlike Barba's page transitions
- May have a steeper learning curve for complex animations
- Potentially heavier performance impact on larger pages
Code Comparison
Locomotive Scroll:
import LocomotiveScroll from 'locomotive-scroll';
const scroll = new LocomotiveScroll({
el: document.querySelector('[data-scroll-container]'),
smooth: true
});
Barba:
import barba from '@barba/core';
barba.init({
transitions: [{
name: 'opacity-transition',
leave(data) { /* ... */ },
enter(data) { /* ... */ }
}]
});
Locomotive Scroll excels in creating smooth, parallax scrolling experiences, while Barba focuses on seamless page transitions. Locomotive Scroll is ideal for single-page websites with complex scroll animations, whereas Barba shines in multi-page applications requiring smooth navigation between pages. The code examples demonstrate the different focus areas: Locomotive Scroll initializes a smooth scrolling container, while Barba sets up page transition animations.
Turbolinks makes navigating your web application faster
Pros of Turbolinks
- Simpler integration, especially for Ruby on Rails applications
- Automatic handling of browser history and URL changes
- Smaller file size and potentially faster initial load times
Cons of Turbolinks
- Less flexible for complex transitions and animations
- Can be more challenging to implement custom behaviors
- May require additional configuration for non-Rails projects
Code Comparison
Turbolinks:
document.addEventListener("turbolinks:load", function() {
// Your JavaScript code here
});
Barba:
barba.init({
transitions: [{
leave(data) {
// Transition logic
},
enter(data) {
// Transition logic
}
}]
});
Key Differences
- Barba offers more granular control over transitions and animations
- Turbolinks focuses on speed and simplicity, while Barba emphasizes customization
- Barba provides a more framework-agnostic approach, making it easier to integrate with various project types
- Turbolinks is tightly integrated with Rails, offering out-of-the-box functionality for Rails applications
- Barba allows for more complex page transitions and state management between views
Both libraries aim to improve the user experience by providing smoother page transitions, but they cater to different use cases and development preferences. Choose based on your project requirements and desired level of customization.
Versatile and extensible page transition library for server-rendered websites π
Pros of swup
- Smaller bundle size (around 9KB gzipped)
- Simpler API and easier to set up for basic use cases
- Built-in support for CSS transitions without additional plugins
Cons of swup
- Less flexible for complex animations and transitions
- Fewer built-in features and plugins compared to Barba
- Limited support for custom caching strategies
Code Comparison
swup:
import Swup from 'swup';
const swup = new Swup();
Barba:
import barba from '@barba/core';
barba.init({
transitions: [{
leave(data) {
// Animation logic
},
enter(data) {
// Animation logic
}
}]
});
Key Differences
- swup focuses on simplicity and ease of use, while Barba offers more advanced features and customization options
- Barba provides a more robust plugin ecosystem and supports complex page transitions
- swup has a smaller learning curve but may be limited for intricate animation requirements
- Barba offers more granular control over the page transition process, including hooks for before, after, and during transitions
Both libraries aim to enhance user experience by providing smooth page transitions in single-page applications, but they cater to different levels of complexity and customization needs.
The speed of a single-page web application without having to write any JavaScript
Pros of Turbo
- Integrated with Ruby on Rails ecosystem, offering seamless integration for Rails developers
- Supports real-time updates through Turbo Streams, enabling live page updates without full reloads
- Includes Turbo Drive for accelerating navigation and form submissions out of the box
Cons of Turbo
- Less flexible for non-Rails projects or custom JavaScript frameworks
- Steeper learning curve for developers not familiar with the Hotwire stack
- Limited animation capabilities compared to Barba's transition system
Code Comparison
Turbo (basic usage):
import { Turbo } from "@hotwired/turbo-rails"
Turbo.start()
Barba (basic usage):
import barba from '@barba/core';
barba.init({
transitions: [{
leave(data) {
// Transition logic
},
enter(data) {
// Transition logic
}
}]
});
Key Differences
- Turbo focuses on enhancing traditional server-rendered applications, while Barba is more suited for creating SPA-like experiences
- Barba offers more granular control over page transitions and animations
- Turbo provides a more comprehensive solution for Rails applications, including form handling and real-time updates
A lightweight script to animate scrolling to anchor links.
Pros of smooth-scroll
- Lightweight and focused solely on smooth scrolling functionality
- Easy to implement with minimal configuration required
- Supports various browsers and fallbacks for older ones
Cons of smooth-scroll
- Limited to scrolling functionality, lacking page transition features
- Doesn't provide advanced animation or transition effects
- May require additional libraries for more complex website interactions
Code Comparison
smooth-scroll:
var scroll = new SmoothScroll('a[href*="#"]', {
speed: 500,
easing: 'easeInOutCubic'
});
Barba:
barba.init({
transitions: [{
leave(data) {
return gsap.to(data.current.container, {
opacity: 0
});
},
enter(data) {
return gsap.from(data.next.container, {
opacity: 0
});
}
}]
});
Summary
smooth-scroll is a lightweight library focused on providing smooth scrolling functionality, making it easy to implement with minimal configuration. However, it lacks the advanced page transition and animation features offered by Barba. Barba, on the other hand, provides a more comprehensive solution for creating fluid and dynamic website transitions, but may have a steeper learning curve and require additional setup. The choice between the two depends on the specific needs of your project and the level of complexity you're aiming for in your website's interactions.
The most versatile JavaScript typewriter effect library on the planet.
Pros of TypeIt
- Focused specifically on creating typewriter effects, offering more specialized features for this purpose
- Lightweight and easy to integrate into existing projects
- Provides a simple API for creating complex typing animations
Cons of TypeIt
- Limited to typewriter animations, less versatile for general page transitions
- Requires JavaScript to function, potentially impacting SEO and accessibility
- May not be suitable for large-scale applications with complex navigation needs
Code Comparison
TypeIt:
new TypeIt("#element", {
strings: "This is a typewriter effect!",
speed: 50,
waitUntilVisible: true
}).go();
Barba:
barba.init({
transitions: [{
leave(data) {
return gsap.to(data.current.container, {opacity: 0});
},
enter(data) {
return gsap.from(data.next.container, {opacity: 0});
}
}]
});
TypeIt excels at creating typewriter effects with a simple API, while Barba offers a more comprehensive solution for page transitions and dynamic content loading. TypeIt is ideal for projects requiring specific typing animations, whereas Barba is better suited for larger applications needing smooth page transitions and enhanced user experience across multiple pages.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
barba.js Γ’ΒΒ
Create badass, fluid and smooth transitions between your websiteΓ’ΒΒs pages.
Intro
Barba.js Γ’ΒΒ aka Barba Γ’ΒΒ is a small (7kb minified and compressed) and easy-to-use library that helps you create fluid and smooth transitions between your website's pages. It makes your website run like a SPA (Single Page Application) and help reduce the delay between your pages, minimize browser HTTP requests and enhance your user's web experience.
Features
Barba is user friendly, smart, extensible and futureproof. The library provides a bunch of useful features that will make your website shine like any other website, ever!
- Simplified API - written in TypeScript and works with
Promises
- Cross-browser support - progressive enhancement for modern browsers
- DOM flexibility - custom markup, namespaces and
data
attribute schema - Hook system - regulars lifecycle methods for
Transitions
andViews
- Transition resolution - rules that let Barba pick the right transition
- Sync mode - indicates whether leave and enter hooks should Γ’ΒΒplay togetherΓ’ΒΒ
- Page related code - custom logic attached to a specific
View
- Modern browser strategies - keep your site run as fast as possible
- Cook like an expert - improve your development workflow
- Built-in utilities - brought to you with a bunch of useful methods
- Plugin system - supplied with useful plugins
- Wide community - more than 980+ amazing developers can help build your website!
Documentation
Here you will find the documentation describing how to use the library.
- Website - official Barba website
- User guide - how to install and use the plugin
- Lessons, courses and videos - for in-depth learning
- Showcase - selected works made with Barba
- Developer API - by developers, for developers
[!NOTE] This guide assumes intermediate knowledge of HTML, CSS, and JavaScript. It is worth mentioning that all code examples use ES6+ syntax. If you are not comfortable with this syntax, we would encourage you to grasp the basics then come back.
In case of emergency, check the "legacy" code example.
Sponsor
If you like this library and want to give some recognition, it is now possible to become a Github sponsor and support this project by sponsoring BarbaJS maintainer on Github. Even if it's a small contribution, you participate in the effort of making open source projects maintained for anyone, and developers to be rewarded for their work/time.
Contribute
If you want to report a bug or request a new feature/improvement, please read the project contributors guidelines before.
Thanks for taking time to contribute to Barba :tada: :+1:
Contributors
License
The project is developed under the MIT license:
- Permissions: This software and derivatives may be used for commercial purposes, you may distribute this software, this software may be modified and you may use and modify the software without distributing it.
- Conditions: Include a copy of the license and copyright notice with the code.
- Limitations: Software is provided without warranty and the software author/license owner cannot be held liable for damages.
Read the full license for more information about your rights.
Top Related Projects
π€ Detection of elements in viewport & smooth scrolling with parallax.
Turbolinks makes navigating your web application faster
Versatile and extensible page transition library for server-rendered websites π
The speed of a single-page web application without having to write any JavaScript
A lightweight script to animate scrolling to anchor links.
The most versatile JavaScript typewriter effect library on the planet.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot