1

我无法使用 Bootstrap 3 实现 Elastislide Image Gallery。

我正在使用的 Elastislide HTML、CSS 和 JQuery 代码在这里:http ://bit.ly/1o01x3x 。

我正在使用 W3Schools 的 Bootstrap 教程和示例:http: //bit.ly/1sJZYs4

通过在 StackOverflow 上搜索其他类似问题,我了解到该问题与 box-sizing 有关。Bootstrap 使用 box-sizing: border-box 但 Elastislide 使用 box-sizing: content-box。

我曾尝试使用这样的建议:http: //bit.ly/1ECEinM但没有成功,所以我的问题是:我应该如何/在哪里应用该代码以使 Elastislide 与 Bootstrap 3 一起使用?

画廊.php:

<!DOCTYPE html>
<html lang="en">
<head>

<title>Test</title>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="IE=Edge;chrome=1">

<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/demo.css" />
<link rel="stylesheet" type="text/css" href="css/style.css" />
<link rel="stylesheet" type="text/css" href="css/elastislide.css" />
<link href='http://fonts.googleapis.com/css?family=PT+Sans+Narrow&v1' rel='stylesheet' type='text/css' />
<link href='http://fonts.googleapis.com/css?family=Pacifico' rel='stylesheet' type='text/css' />

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

<noscript>
<style>
    .es-carousel ul{
        display:block;
    }
</style>
</noscript>

<script id="img-wrapper-tmpl" type="text/x-jquery-tmpl">    
    <div class="rg-image-wrapper">
        {{if itemsCount > 1}}
            <div class="rg-image-nav">
                <a href="#" class="rg-image-nav-prev">Previous Image</a>
                <a href="#" class="rg-image-nav-next">Next Image</a>
            </div>
        {{/if}}
        <div class="rg-image"></div>
        <div class="rg-loading"></div>
        <div class="rg-caption-wrapper">
            <div class="rg-caption" style="display:none;">
                <p></p>
            </div>
        </div>
    </div>
</script>

</head>
<body>

<div class="container">
    <div class="row">
        <div class="col-sm-8">
            <div class="content">
                <h1>Responsive Image Gallery <span>A jQuery image gallery with a thumbnail carousel</span></h1>
                <div id="rg-gallery" class="rg-gallery">
                    <div class="rg-thumbs">
                        <!-- Elastislide Carousel Thumbnail Viewer -->
                        <div class="es-carousel-wrapper">
                            <div class="es-nav">
                                <span class="es-nav-prev">Previous</span>
                                <span class="es-nav-next">Next</span>
                            </div>
                            <div class="es-carousel">
                                <ul>
                                    <li><a href="#"><img src="images/thumbs/1.jpg" data-large="images/1.jpg" alt="image01" data-description="From off a hill whose concave womb reworded" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/2.jpg" data-large="images/2.jpg" alt="image02" data-description="A plaintful story from a sistering vale" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/3.jpg" data-large="images/3.jpg" alt="image03" data-description="A plaintful story from a sistering vale" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/4.jpg" data-large="images/4.jpg" alt="image04" data-description="My spirits to attend this double voice accorded" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/5.jpg" data-large="images/5.jpg" alt="image05" data-description="And down I laid to list the sad-tuned tale" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/6.jpg" data-large="images/6.jpg" alt="image06" data-description="Ere long espied a fickle maid full pale" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/7.jpg" data-large="images/7.jpg" alt="image07" data-description="Tearing of papers, breaking rings a-twain" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/8.jpg" data-large="images/8.jpg" alt="image08" data-description="Storming her world with sorrow's wind and rain" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/9.jpg" data-large="images/9.jpg" alt="image09" data-description="Upon her head a platted hive of straw" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/10.jpg" data-large="images/10.jpg" alt="image10" data-description="Which fortified her visage from the sun" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/11.jpg" data-large="images/11.jpg" alt="image11" data-description="Whereon the thought might think sometime it saw" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/12.jpg" data-large="images/12.jpg" alt="image12" data-description="The carcass of beauty spent and done" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/13.jpg" data-large="images/13.jpg" alt="image13" data-description="Time had not scythed all that youth begun" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/14.jpg" data-large="images/14.jpg" alt="image14" data-description="Nor youth all quit; but, spite of heaven's fell rage" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/15.jpg" data-large="images/15.jpg" alt="image15" data-description="Some beauty peep'd through lattice of sear'd age" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/16.jpg" data-large="images/16.jpg" alt="image16" data-description="Oft did she heave her napkin to her eyne" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/17.jpg" data-large="images/17.jpg" alt="image17" data-description="Which on it had conceited characters" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/18.jpg" data-large="images/18.jpg" alt="image18" data-description="Laundering the silken figures in the brine" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/19.jpg" data-large="images/19.jpg" alt="image19" data-description="That season'd woe had pelleted in tears" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/20.jpg" data-large="images/20.jpg" alt="image20" data-description="And often reading what contents it bears" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/21.jpg" data-large="images/21.jpg" alt="image21" data-description="As often shrieking undistinguish'd woe" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/22.jpg" data-large="images/22.jpg" alt="image22" data-description="In clamours of all size, both high and low" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/23.jpg" data-large="images/23.jpg" alt="image23" data-description="Sometimes her levell'd eyes their carriage ride" /></a></li>
                                    <li><a href="#"><img src="images/thumbs/24.jpg" data-large="images/24.jpg" alt="image24" data-description="As they did battery to the spheres intend" /></a></li>
                                </ul>
                            </div>
                        </div>
                        <!-- End Elastislide Carousel Thumbnail Viewer -->
                    </div><!-- rg-thumbs -->
                </div><!-- rg-gallery -->
                <p class="sub">Want more Shakespeare? <a href="http://www.opensourceshakespeare.org/" target="_blank">http://www.opensourceshakespeare.org/</a></p>
            </div><!-- content -->
        </div>
        <div class="col-sm-4"><!-- nothing here yet --></div>
    </div>
</div>

</body>
</html>

elastislide.css:

/* Elastislide Style */
.es-carousel-wrapper{
    background: #101010;
    padding:10px 27px;
    -moz-border-radius: 10px;
    -webkit-border-radius: 10px;
    border-radius: 10px;
    -moz-box-shadow:0px 1px 3px rgba(0, 0, 0, 0.9);
    -webkit-box-shadow:0px 1px 3px rgba(0, 0, 0, 0.9);
    box-shadow:0px 1px 3px rgba(0, 0, 0, 0.9);
    position:relative;
    margin-bottom:20px;
}
.es-carousel{
    overflow:hidden;
    background:#000;
}
.es-carousel ul{
    display:none;
}
.es-carousel ul li{
    height:100%;
    float:left;
    display:block;
}
.es-carousel ul li a{
    display:block;
    border-style:solid;
    border-color:#222;
    opacity:0.8;
    -webkit-touch-callout:none;
    /* option */
    -webkit-transition: all 0.2s ease-in-out;
    -moz-transition: all 0.2s ease-in-out;
    -o-transition: all 0.2s ease-in-out;
    -ms-transition: all 0.2s ease-in-out;
    transition: all 0.2s ease-in-out;
}
.es-carousel ul li.selected a{
    border-color:#fff;
    opacity:1.0;
}
.es-carousel ul li a img{
    display:block;
    border:none;
    max-height:100%;
    max-width:100%;
}
.es-nav span{
    position:absolute;
    top:50%;
    left:8px;
    background:transparent url(../lib_images/img-gallery/nav_thumbs.png) no-repeat top left;
    width:14px;
    height:26px;
    margin-top:-13px;
    text-indent:-9000px;
    cursor:pointer;
    opacity:0.8;
}
.es-nav span.es-nav-next{
    right:8px;
    left:auto;
    background-position:top right;
}
.es-nav span:hover{
    opacity:1.0;
}

rg-gallery.css:

.rg-image-wrapper{
    position:relative;
    padding:20px 30px;
    background:transparent url(../lib_images/img-gallery/black.png) repeat top left;
    -moz-border-radius: 10px;
    -webkit-border-radius: 10px;
    border-radius: 10px;
    min-height:20px;

  -webkit-box-sizing: content-box;
     -moz-box-sizing: content-box;
          box-sizing: content-box;
}
.rg-image{
    position:relative;
    text-align:center;
    line-height:0px;

  -webkit-box-sizing: content-box;
     -moz-box-sizing: content-box;
          box-sizing: content-box;
}
.rg-image img{
    max-height:100%;
    max-width:100%;

  -webkit-box-sizing: content-box;
     -moz-box-sizing: content-box;
          box-sizing: content-box;
}
.rg-image-nav a{
    position:absolute;
    top:0px;
    left:0px;
    background:#000 url(../lib_images/img-gallery/nav.png) no-repeat -20% 50%;
    width:28px;
    height:100%;
    text-indent:-9000px;
    cursor:pointer;
    opacity:0.3;
    outline:none;
    -moz-border-radius: 10px 0px 0px 10px;
    -webkit-border-radius: 10px 0px 0px 10px;
    border-radius: 10px 0px 0px 10px;

  -webkit-box-sizing: content-box;
     -moz-box-sizing: content-box;
          box-sizing: content-box;
}
.rg-image-nav a.rg-image-nav-next{
    right:0px;
    left:auto;
    background-position:115% 50%;
    -moz-border-radius: 0px 10px 10px 0px;
    -webkit-border-radius: 0px 10px 10px 0px;
    border-radius: 0px 10px 10px 0px;

  -webkit-box-sizing: content-box;
     -moz-box-sizing: content-box;
          box-sizing: content-box;
}
.rg-image-nav a:hover{
    opacity:0.8;

  -webkit-box-sizing: content-box;
     -moz-box-sizing: content-box;
          box-sizing: content-box;
}
.rg-caption {
    text-align:center;
    margin-top:15px;
    position:relative;

  -webkit-box-sizing: content-box;
     -moz-box-sizing: content-box;
          box-sizing: content-box;
}
.rg-caption p{
    font-size:11px;
    letter-spacing:2px;
    font-family: 'Trebuchet MS', 'Myriad Pro', Arial, sans-serif;
    line-height:16px;
    padding:0 15px;
    text-transform:uppercase;

  -webkit-box-sizing: content-box;
     -moz-box-sizing: content-box;
          box-sizing: content-box;
}
.rg-view{
    height:30px;

  -webkit-box-sizing: content-box;
     -moz-box-sizing: content-box;
          box-sizing: content-box;
}
.rg-view a{
    display:block;
    float:right;
    width:16px;
    height:16px;
    margin-right:3px;
    background:#464646 url(../lib_images/img-gallery/views.png) no-repeat top left;
    border:3px solid #464646;
    opacity:0.8;

  -webkit-box-sizing: content-box;
     -moz-box-sizing: content-box;
          box-sizing: content-box;
}
.rg-view a:hover{
    opacity:1.0;

  -webkit-box-sizing: content-box;
     -moz-box-sizing: content-box;
          box-sizing: content-box;
}
.rg-view a.rg-view-full{
    background-position:0px 0px;

  -webkit-box-sizing: content-box;
     -moz-box-sizing: content-box;
          box-sizing: content-box;
}
.rg-view a.rg-view-selected{
    background-color:#6f6f6f;
    border-color:#6f6f6f;

  -webkit-box-sizing: content-box;
     -moz-box-sizing: content-box;
          box-sizing: content-box;
}
.rg-view a.rg-view-thumbs{
    background-position:0px -16px;

  -webkit-box-sizing: content-box;
     -moz-box-sizing: content-box;
          box-sizing: content-box;
}
.rg-loading{
    width:46px;
    height:46px;
    position:absolute;
    top:50%;
    left:50%;
    background:#000 url(../lib_images/img-gallery/ajax-loader.gif) no-repeat center center;
    margin:-23px 0px 0px -23px;
    z-index:100;
    -moz-border-radius: 10px;
    -webkit-border-radius: 10px;
    border-radius: 10px;
    opacity:0.7;

  -webkit-box-sizing: content-box;
     -moz-box-sizing: content-box;
          box-sizing: content-box;
}

画廊.js:

$(function() {
    // ======================= imagesLoaded Plugin ===============================
    // https://github.com/desandro/imagesloaded

    // $('#my-container').imagesLoaded(myFunction)
    // execute a callback when all images have loaded.
    // needed because .load() doesn't work on cached images

    // callback function gets image collection as argument
    //  this is the container

    // original: mit license. paul irish. 2010.
    // contributors: Oren Solomianik, David DeSandro, Yiannis Chatzikonstantinou

    $.fn.imagesLoaded       = function( callback ) {
    var $images = this.find('img'),
        len     = $images.length,
        _this   = this,
        blank   = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==';

    function triggerCallback() {
        callback.call( _this, $images );
    }

    function imgLoaded() {
        if ( --len <= 0 && this.src !== blank ){
            setTimeout( triggerCallback );
            $images.off( 'load error', imgLoaded );
        }
    }

    if ( !len ) {
        triggerCallback();
    }

    $images.on( 'load error',  imgLoaded ).each( function() {
        // cached images don't fire load sometimes, so we reset src.
        if (this.complete || this.complete === undefined){
            var src = this.src;
            // webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
            // data uri bypasses webkit log warning (thx doug jones)
            this.src = blank;
            this.src = src;
        }
    });

    return this;
    };

    // gallery container
    var $rgGallery          = $('#rg-gallery'),
    // carousel container
    $esCarousel         = $rgGallery.find('div.es-carousel-wrapper'),
    // the carousel items
    $items              = $esCarousel.find('ul > li'),
    // total number of items
    itemsCount          = $items.length;

    Gallery             = (function() {
            // index of the current item
        var current         = 0, 
            // mode : carousel || fullview
            mode            = 'carousel',
            // control if one image is being loaded
            anim            = false,
            init            = function() {

                // (not necessary) preloading the images here...
                $items.add('<img src="lib_images/img-gallery/ajax-loader.gif"/><img src="lib_images/img-gallery/black.png"/>').imagesLoaded( function() {
                    // add options
                    _addViewModes();

                    // add large image wrapper
                    _addImageWrapper();

                    // show first image
                    _showImage( $items.eq( current ) );

                });

                // initialize the carousel
                if( mode === 'carousel' )
                    _initCarousel();

            },
            _initCarousel   = function() {

                // we are using the elastislide plugin:
                // http://tympanus.net/codrops/2011/09/12/elastislide-responsive-carousel/
                $esCarousel.show().elastislide({
                    imageW  : 65,
                    onClick : function( $item ) {
                        if( anim ) return false;
                        anim    = true;
                        // on click show image
                        _showImage($item);
                        // change current
                        current = $item.index();
                    }
                });

                // set elastislide's current to current
                $esCarousel.elastislide( 'setCurrent', current );

            },
            _addViewModes   = function() {

                // top right buttons: hide / show carousel

                var $viewfull   = $('<a href="#" class="rg-view-full"></a>'),
                    $viewthumbs = $('<a href="#" class="rg-view-thumbs rg-view-selected"></a>');

                $rgGallery.prepend( $('<div class="rg-view"/>').append( $viewfull ).append( $viewthumbs ) );

                $viewfull.on('click.rgGallery', function( event ) {
                        if( mode === 'carousel' )
                            $esCarousel.elastislide( 'destroy' );
                        $esCarousel.hide();
                    $viewfull.addClass('rg-view-selected');
                    $viewthumbs.removeClass('rg-view-selected');
                    mode    = 'fullview';
                    return false;
                });

                $viewthumbs.on('click.rgGallery', function( event ) {
                    _initCarousel();
                    $viewthumbs.addClass('rg-view-selected');
                    $viewfull.removeClass('rg-view-selected');
                    mode    = 'carousel';
                    return false;
                });

                if( mode === 'fullview' )
                    $viewfull.trigger('click');

            },
            _addImageWrapper= function() {

                // adds the structure for the large image and the navigation buttons (if total items > 1)
                // also initializes the navigation events

                $('#img-wrapper-tmpl').tmpl( {itemsCount : itemsCount} ).appendTo( $rgGallery );

                if( itemsCount > 1 ) {
                    // addNavigation
                    var $navPrev        = $rgGallery.find('a.rg-image-nav-prev'),
                        $navNext        = $rgGallery.find('a.rg-image-nav-next'),
                        $imgWrapper     = $rgGallery.find('div.rg-image');

                    $navPrev.on('click.rgGallery', function( event ) {
                        _navigate( 'left' );
                        return false;
                    }); 

                    $navNext.on('click.rgGallery', function( event ) {
                        _navigate( 'right' );
                        return false;
                    });

                    // add touchwipe events on the large image wrapper
                    $imgWrapper.touchwipe({
                        wipeLeft            : function() {
                            _navigate( 'right' );
                        },
                        wipeRight           : function() {
                            _navigate( 'left' );
                        },
                        preventDefaultEvents: false
                    });

                    $(document).on('keyup.rgGallery', function( event ) {
                        if (event.keyCode == 39)
                            _navigate( 'right' );
                        else if (event.keyCode == 37)
                            _navigate( 'left' );    
                    });

                }

            },
            _navigate       = function( dir ) {

                // navigate through the large images

                if( anim ) return false;
                anim    = true;

                if( dir === 'right' ) {
                    if( current + 1 >= itemsCount )
                        current = 0;
                    else
                        ++current;
                }
                else if( dir === 'left' ) {
                    if( current - 1 < 0 )
                        current = itemsCount - 1;
                    else
                        --current;
                }

                _showImage( $items.eq( current ) );

            },
            _showImage      = function( $item ) {

                // shows the large image that is associated to the $item

                var $loader = $rgGallery.find('div.rg-loading').show();

                $items.removeClass('selected');
                $item.addClass('selected');

                var $thumb      = $item.find('img'),
                    largesrc    = $thumb.data('large'),
                    title       = $thumb.data('description');

                $('<img/>').load( function() {

                    $rgGallery.find('div.rg-image').empty().append('<img src="' + largesrc + '"/>');

                    if( title )
                        $rgGallery.find('div.rg-caption').show().children('p').empty().text( title );

                    $loader.hide();

                    if( mode === 'carousel' ) {
                        $esCarousel.elastislide( 'reload' );
                        $esCarousel.elastislide( 'setCurrent', current );
                    }

                    anim    = false;

                }).attr( 'src', largesrc );

            },
            addItems        = function( $new ) {

                $esCarousel.find('ul').append($new);
                $items      = $items.add( $($new) );
                itemsCount  = $items.length; 
                $esCarousel.elastislide( 'add', $new );

            };

        return { 
            init        : init,
            addItems    : addItems
        };

    })();

    Gallery.init();

    /*
    Example to add more items to the gallery:

    var $new  = $('<li><a href="#"><img src="images/thumbs/1.jpg" data-large="images/1.jpg" alt="image01" data-description="From off a hill whose concave womb reworded" /></a></li>');
    Gallery.addItems( $new );
    */
});

还有 jquery.easing.1.3.js、jquery.elastislide.js 和 jquery.tmpl.min.js。不知道我是否也必须粘贴该代码?

4

0 回答 0