Full Width Fading Background Slideshow with jQuery and CSS

File Size: 7.54 KB
Views Total: 4822
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   
Full Width Fading Background Slideshow with jQuery and CSS

A simple lightweight jQuery & CSS based background image slideshow plugin with support of next/prev arrows navigation and cross-fade transitions, perfect for a full width banner rotator for your website.

How to use it:

1. Insert your images using data-image attribute into a DIV element.

<div class="slider-wrapper">
  <div class="slide" data-image="1.jpg"></div>
  <div class="slide" data-image="2.jpg"></div>
  <div class="slide" data-image="3.jpg"></div>
</div>

2. Create controls to navigate through these images.

<div class="slider-nav">
  <button class="slider-previous">Previous</button>
  <button class="slider-next">Next</button>
</div>

3. And them wrap them into a container element. The whole markup structure should be like this:

<div class="slider" id="main-slider">
  <div class="slider-wrapper">
    <div class="slide" data-image="1.jpg"></div>
    <div class="slide" data-image="2.jpg"></div>
    <div class="slide" data-image="3.jpg"></div>
  </div>
  <div class="slider-nav">
    <button class="slider-previous">Previous</button>
    <button class="slider-next">Next</button>
  </div>
</div>

4. The required CSS to style the slideshow.

html,
body {
  margin: 0;
  padding: 0;
}

.slider {
  width: 100%;
  overflow: hidden;
  position: relative;
  height: 400px;
}

.slider-wrapper {
  width: 100%;
  height: 100%;
  position: relative;
}

.slide {
  float: left;
  position: absolute;
  width: 100%;
  height: 100%;
  background-repeat: no-repeat;
  background-size: cover;
  opacity: 0;
}

.slider-wrapper > .slide:first-child { opacity: 1; }

.slider-nav {
  height: 64px;
  width: 100%;
  position: absolute;
  top: 50%;
  left: 0;
  margin-top: -32px;
  z-index: 1000;
  display: none;
}

.slider-nav button {
  border: none;
  display: block;
  width: 64px;
  height: 64px;
  cursor: pointer;
  text-indent: -9999em;
  background-color: transparent;
  background-repeat: no-repeat;
}

.slider-nav button:focus { outline-style: none; }

.slider-nav button.slider-previous {
  float: left;
  background-image: url('left.png');
  display: none;
  margin-left: 1em;
}

.slider-nav button.slider-next {
  float: right;
  background-image: url('right.png');
  margin-right: 1em;
}

5. Include the latest version of jQuery library at the end of your webpage.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 

6. The jQuery script to active the slideshow.

(function( $ ) {
  $.fn.slideshow = function( options ) {
    options = $.extend({
      wrapper: ".slider-wrapper",
      previous: ".slider-previous",
      next: ".slider-next",
      slides: ".slide",
      nav: ".slider-nav",
      speed: 500, // fade animation speed
      easing: "swing" // swing or linear
      
    }, options);
    
    var slideTo = function( slide, element ) {
      var $currentSlide = $( options.slides, element ).eq( slide );
      
      $currentSlide.
      animate({
        opacity: 1
      }, options.speed, options.easing ).
      siblings( options.slides ).
      css( "opacity", 0 );  
      
    };
    
    return this.each(function() {
      var $element = $( this ),
        $previous = $( options.previous, $element ),
        $next = $( options.next, $element ),
        index = 0,
        total = $( options.slides ).length;
        
        $( options.slides, $element ).each(function() {
          var $slide = $( this );
          var image = $slide.data( "image" );
          $slide.css( "backgroundImage", "url(" + image + ")" );
        });
        
        
        $element.hover(function() {
          $( options.nav, $element ).stop( true, true ).show();  
        }, function() {
          $( options.nav, $element ).stop( true, true ).hide();  
          
        });
        
      $next.on( "click", function() {
        index++;
        $previous.show();
        
        if( index == total - 1 ) {
          index = total - 1;
          $next.hide();  
        }
        
        slideTo( index, $element );  
        
      });
      
      $previous.on( "click", function() {
        index--;
        $next.show();
        
        if( index == 0 ) {
          index = 0;
          $previous.hide();  
        }
        
        slideTo( index, $element );  
        
      });

      
    });
  };
  
  $(function() {
    $( "#main-slider" ).slideshow();
    
  });
  
})( jQuery );

This awesome jQuery plugin is developed by gabrieleromanato. For more Advanced Usages, please check the demo page or visit the official website.