Adatum
Мар
11
2013
CSS // JavaScript // jQuery // PHP /

Очень простой широкоформатный Слайдер.

11032013
Этот отличный полно экранный Слайдер отлично смотрится как на экране компьютера так и на экране мобильного телефона. Он работает с переходами на CSS и позволяет применить якорь (anchor) для навигации.

Демо Скачать

THE HTML

<div id="cbp-fwslider" class="cbp-fwslider">
    <ul>
        <li><a href="#"><img src="images/1.jpg" alt="img01"/></a></li>
        <li><a href="#"><img src="images/2.jpg" alt="img02"/></a></li>
        <li><a href="#"><img src="images/3.jpg" alt="img03"/></a></li>
        <li><a href="#"><img src="images/4.jpg" alt="img04"/></a></li>
        <li><a href="#"><img src="images/5.jpg" alt="img05"/></a></li>
    </ul>
</div>

THE CSS

.cbp-fwslider {
    position: relative;
    margin: 0 0 10px;
    overflow: hidden;
    padding: 40px 0 60px;
}
 
.cbp-fwslider ul {
    margin: 0;
    padding: 0;
    white-space: nowrap;
    list-style-type: none;
}
 
.cbp-fwslider ul li {
    transform: translateZ(0);
    float: left;
    display: block;
    margin: 0;
    padding: 0;
}
 
.cbp-fwslider ul li > a,
.cbp-fwslider ul li > div {
    display: block;
    text-align: center;
    outline: none;
}
 
.cbp-fwslider ul li > a img {
    border: none;
    display: block;
    margin: 0 auto;
    max-width: 75%;
}
 
.cbp-fwslider nav span {
    position: absolute;
    top: 50%;
    width: 50px;
    height: 100px;
    background: #47a3da;
    color: #fff;
    font-size: 50px;
    text-align: center;
    margin-top: -50px;
    line-height: 100px;
    cursor: pointer;
    font-weight: normal;
}
 
.cbp-fwslider nav span:hover {
    background: #378fc3;
}
 
.cbp-fwslider nav span.cbp-fwnext {
    right: 0px;
}
 
.cbp-fwslider nav span.cbp-fwprev {
    left: 0px;
}
 
.cbp-fwdots {
    position: absolute;
    bottom: 0px;
    white-space: nowrap;
    text-align: center;
    width: 100%;
}
 
.cbp-fwdots span {
    display: inline-block;
    width: 18px;
    height: 18px;
    background: #ddd;
    margin: 4px;
    border-radius: 50%;
    cursor: pointer;
}
 
.cbp-fwdots span:hover {
    background: #999;
}
 
.cbp-fwdots span.cbp-fwcurrent {
    background: #47a3da;
    box-shadow: 0 0 0 2px #47a3da;
    transition: box-shadow 0.2s ease-in-out;
}

THE JAVASCRIPT

;( function( $, window, undefined ) {
 
    'use strict';
 
    // Глобальные
    var Modernizr = window.Modernizr;
 
    $.CBPFWSlider = function( options, element ) {
        this.$el = $( element );
        this._init( options );
    };
 
    // Настройки
    $.CBPFWSlider.defaults = {
        // default transition speed (ms)
        speed : 500,
        // default transition easing
        easing : 'ease'
    };
 
    $.CBPFWSlider.prototype = {
        _init : function( options ) {
           
            this.options = $.extend( true, {}, $.CBPFWSlider.defaults, options );
            this._config();
            this._initEvents();
        },
        _config : function() {
 
            this.$list = this.$el.children( 'ul' );
            this.$items = this.$list.children( 'li' );
            this.itemsCount = this.$items.length;
            this.support = Modernizr.csstransitions && Modernizr.csstransforms;
            this.support3d = Modernizr.csstransforms3d;
            var transProperties = {
                'WebkitTransition' : { transitionEndEvent : 'webkitTransitionEnd', tranformName : '-webkit-transform' },
                'MozTransition' : { transitionEndEvent : 'transitionend', tranformName : '-moz-transform' },
                'OTransition' : { transitionEndEvent : 'oTransitionEnd', tranformName : '-o-transform' },
                'msTransition' : { transitionEndEvent : 'MSTransitionEnd', tranformName : '-ms-transform' },
                'transition' : { transitionEndEvent : 'transitionend', tranformName : 'transform' }
            };
            if( this.support ) {
                this.transEndEventName = transProperties[ Modernizr.prefixed( 'transition' ) ].transitionEndEvent + '.cbpFWSlider';
                this.transformName = transProperties[ Modernizr.prefixed( 'transition' ) ].tranformName;
            }
            this.current = 0;
            this.old = 0;
            this.isAnimating = false;
            this.$list.css( 'width', 100 * this.itemsCount + '%' );
            if( this.support ) {
                this.$list.css( 'transition', this.transformName + ' ' + this.options.speed + 'ms ' + this.options.easing );
            }
            this.$items.css( 'width', 100 / this.itemsCount + '%' );
            if( this.itemsCount > 1 ) {
 
                this.$navPrev = $( '<span class="cbp-fwprev"><</span>' ).hide();
                this.$navNext = $( '<span class="cbp-fwnext">></span>' );
                $( '<nav/>' ).append( this.$navPrev, this.$navNext ).appendTo( this.$el );
                var dots = '';
                for( var i = 0; i < this.itemsCount; ++i ) {
                    var dot = i === this.current ? '<span class="cbp-fwcurrent"></span>' : '<span></span>';
                    dots += dot;
                }
                var navDots = $( '<div class="cbp-fwdots"/>' ).append( dots ).appendTo( this.$el );
                this.$navDots = navDots.children( 'span' );
 
            }
 
        },
        _initEvents : function() {
             
            var self = this;
            if( this.itemsCount > 1 ) {
                this.$navPrev.on( 'click.cbpFWSlider', $.proxy( this._navigate, this, 'previous' ) );
                this.$navNext.on( 'click.cbpFWSlider', $.proxy( this._navigate, this, 'next' ) );
                this.$navDots.on( 'click.cbpFWSlider', function() { self._jump( $( this ).index() ); } );
            }
 
        },
        _navigate : function( direction ) {
 
            // do nothing if the list is currently moving
            if( this.isAnimating ) {
                return false;
            }
 
            this.isAnimating = true;
            // update old and current values
            this.old = this.current;
            if( direction === 'next' && this.current < this.itemsCount - 1 ) {
                ++this.current;
            }
            else if( direction === 'previous' && this.current > 0 ) {
                --this.current;
            }
            // slide
            this._slide();
 
        },
        _slide : function() {
 
            this._toggleNavControls();
            var translateVal = -1 * this.current * 100 / this.itemsCount;
            if( this.support ) {
                this.$list.css( 'transform', this.support3d ? 'translate3d(' + translateVal + '%,0,0)' : 'translate(' + translateVal + '%)' );
            }
            else {
                this.$list.css( 'margin-left', -1 * this.current * 100 + '%' ); 
            }
             
            var transitionendfn = $.proxy( function() {
                this.isAnimating = false;
            }, this );
 
            if( this.support ) {
                this.$list.on( this.transEndEventName, $.proxy( transitionendfn, this ) );
            }
            else {
                transitionendfn.call();
            }
 
        },
        _toggleNavControls : function() {
 
            switch( this.current ) {
                case 0 : this.$navNext.show(); this.$navPrev.hide(); break;
                case this.itemsCount - 1 : this.$navNext.hide(); this.$navPrev.show(); break;
                default : this.$navNext.show(); this.$navPrev.show(); break;
            }
            this.$navDots.eq( this.old ).removeClass( 'cbp-fwcurrent' ).end().eq( this.current ).addClass( 'cbp-fwcurrent' );
 
        },
        _jump : function( position ) {
 
            if( position === this.current || this.isAnimating ) {
                return false;
            }
            this.isAnimating = true;
            this.old = this.current;
            this.current = position;
            this._slide();
 
        },
        destroy : function() {
 
            if( this.itemsCount > 1 ) {
                this.$navPrev.parent().remove();
                this.$navDots.parent().remove();
            }
            this.$list.css( 'width', 'auto' );
            if( this.support ) {
                this.$list.css( 'transition', 'none' );
            }
            this.$items.css( 'width', 'auto' );
 
        }
    };
 
    var logError = function( message ) {
        if ( window.console ) {
            window.console.error( message );
        }
    };
 
    $.fn.cbpFWSlider = function( options ) {
        if ( typeof options === 'string' ) {
            var args = Array.prototype.slice.call( arguments, 1 );
            this.each(function() {
                var instance = $.data( this, 'cbpFWSlider' );
                if ( !instance ) {
                    logError( "cannot call methods on cbpFWSlider prior to initialization; " +
                    "attempted to call method '" + options + "'" );
                    return;
                }
                if ( !$.isFunction( instance[options] ) || options.charAt(0) === "_" ) {
                    logError( "no such method '" + options + "' for cbpFWSlider instance" );
                    return;
                }
                instance[ options ].apply( instance, args );
            });
        } 
        else {
            this.each(function() {  
                var instance = $.data( this, 'cbpFWSlider' );
                if ( instance ) {
                    instance._init();
                }
                else {
                    instance = $.data( this, 'cbpFWSlider', new $.CBPFWSlider( options, this ) );
                }
            });
        }
        return this;
    };
 
} )( jQuery, window );

Похожие статьи

Оставить комментарий

*

  • Просто перепечатали с другого сайта. Ни слова о настройках.