bgfade

JavaScript Library to switch display of DOM elements with fade.

Link

Demo

Normal

  • 1
  • 2
  • 3

HTML

<ul id="normal" class="bg">
  <li><span>1</span></li>
  <li><span>2</span></li>
  <li><span>3</span></li>
</ul>

CSS

.bg {
  list-style: none;
  margin: 0 auto;
  padding-top: 62.5%;
  width: 100%;
}

.bg li {
  background-color: crimson;
  height: 100%;
  left: 0;
  top: 0;
  width: 100%;
}

.bg li:first-of-type {
  background-color: royalblue;
}

.bg li:last-of-type {
  background-color: darkturquoise;
}

.bg li span {
  color: #fff;
  font-size: 5rem;
  left: 50%;
  position: absolute;
  top: 50%;
  transform: translate(-50%, -50%);
}

JavaScript

import bgfade from 'bgfade';

window.addEventListener('load', () => {
  bgfade('normal');
});

Control

  • 1
  • 2
  • 3

HTML

<ul id="control" class="bg">
  <li><span>1</span></li>
  <li><span>2</span></li>
  <li><span>3</span></li>
</ul>
<button id="start">start</button>
<button id="stop">stop</button>

CSS

.bg {
  list-style: none;
  margin: 0 auto;
  padding-top: 62.5%;
  width: 100%;
}

.bg li {
  background-color: crimson;
  height: 100%;
  left: 0;
  top: 0;
  width: 100%;
}

.bg li:first-of-type {
  background-color: royalblue;
}

.bg li:last-of-type {
  background-color: darkturquoise;
}

.bg li span {
  color: #fff;
  font-size: 5rem;
  left: 50%;
  position: absolute;
  top: 50%;
  transform: translate(-50%, -50%);
}

#control {
  margin-bottom: 20px;
}

JavaScript

import bgfade from 'bgfade';

window.addEventListener('load', () => {
  const bg = bgfade('control');

  // click event
  document.getElementById('start').addEventListener('click', () => {
    bg.start();
  });
  document.getElementById('stop').addEventListener('click', () => {
    bg.stop();
  });
});

Customize

HTML

<ul id="customize" class="bg">
  <li><span></span></li>
  <li><span></span></li>
  <li><span></span></li>
</ul>

CSS (requires the shared .bg base styles shown above)

/* Include the shared .bg base styles from the previous CSS example, then apply these overrides. */
#customize {
  overflow: hidden;
}

#customize li span {
  background: url('/assets/images/bgfade-sample-image-01.jpg') no-repeat center;
  background-size: cover;
  display: block;
  height: 100%;
  left: 0;
  position: absolute;
  top: 0;
  transform: scale(1);
  transition: transform 8s;
  width: 100%;
}

#customize li:first-of-type span {
  background-image: url('/assets/images/bgfade-sample-image-02.jpg');
}

#customize li:last-of-type span {
  background-image: url('/assets/images/bgfade-sample-image-03.jpg');
}

#customize li.bg-active span {
  transform: scale(1.2);
}

JavaScript

import bgfade from 'bgfade';

window.addEventListener('load', () => {
  bgfade('customize', {
    speed: 4,
    duration: 5
  });
});