简单的画廊滑块

时间:2012-11-11 07:02:52

标签: slider

我正在尝试使用div和javascript创建一个简单的滑块。我设置了一个带有六个图像和一个箭头的div,每次单击时都会对包含图像528px(每个图像的宽度)的容器进行电影拍摄。当我到达画廊的开头或结尾时,我希望相应的箭头按钮淡出,以便用户不会一直按下/ prev。

感谢任何帮助。

JAVASCRIPT

$("#btn-gallery-next").click(function(){
$("div#gallery li").not(this).removeClass('clicked');
$("div#gallery-slide").animate({left:"-=528px"});
if($("div#gallery-slide").position().left < -3168)
{
    $("#btn-gallery-next").fadeOut();
}
else {
    $("#btn-gallery-next").fadeIn();
}

});

$("#btn-gallery-prev").click(function(){
$("div#gallery li").not(this).removeClass('clicked');
$("div#gallery-slide").animate({left:"+=528px"});
if($("div#gallery-slide").position().left > 0)
{
    $("#btn-gallery-prev").fadeOut();
}
else {
    $("#btn-gallery-prev").fadeIn();
}

});

HTML

<div id="gallery-slide">
      <img class="gallery-img" src="_/img/gallery/img1.jpg" alt="" />
      <img class="gallery-img" src="_/img/gallery/img2.jpg" alt="" />
      <img class="gallery-img" src="_/img/gallery/img3.jpg" alt="" />
      <img class="gallery-img" src="_/img/gallery/img4.jpg" alt="" />
      <img class="gallery-img" src="_/img/gallery/img5.jpg" alt="" />
      <img class="gallery-img" src="_/img/gallery/img6.jpg" alt="" />
</div>

3 个答案:

答案 0 :(得分:0)

从woothemes尝试flex slider,它可以满足您的所有需求。

答案 1 :(得分:0)

为什么不使用像Owl Slider这样的滑块库?它有很多选项和配置。集成到任何项目中都非常简单。

  • 示例#1 www.midwestgathering.com/#galleries
  • 示例#2 www.owlgraphic.com/owlcarousel/demos/images.html

另一个选项是jcarousel。基本滑块显示的示例使左下一个按钮处于非活动状态,直到用户向右滑动,然后一旦用户到达图库的末尾,右下一个按钮变为非活动状态:

<强> JS

(function($) {
$(function() {
    $('.jcarousel').jcarousel();

    $('.jcarousel-control-prev')
        .on('jcarouselcontrol:active', function() {
            $(this).removeClass('inactive');
        })
        .on('jcarouselcontrol:inactive', function() {
            $(this).addClass('inactive');
        })
        .jcarouselControl({
            target: '-=1'
        });

    $('.jcarousel-control-next')
        .on('jcarouselcontrol:active', function() {
            $(this).removeClass('inactive');
        })
        .on('jcarouselcontrol:inactive', function() {
            $(this).addClass('inactive');
        })
        .jcarouselControl({
            target: '+=1'
        });

    $('.jcarousel-pagination')
        .on('jcarouselpagination:active', 'a', function() {
            $(this).addClass('active');
        })
        .on('jcarouselpagination:inactive', 'a', function() {
            $(this).removeClass('active');
        })
        .jcarouselPagination();
});
})(jQuery);

<强> CSS

.jcarousel-wrapper {
    margin: 20px auto;
    position: relative;
    border: 10px solid #fff;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;
    -webkit-box-shadow: 0 0 2px #999;
    -moz-box-shadow: 0 0 2px #999;
    box-shadow: 0 0 2px #999;
  }


.jcarousel-wrapper .photo-credits {
    position: absolute;
    right: 15px;
    bottom: 0;
    font-size: 13px;
    color: #fff;
    text-shadow: 0 0 1px rgba(0, 0, 0, 0.85);
    opacity: .66;
}

.jcarousel-wrapper .photo-credits a {
    color: #fff;
}

/** Carousel **/

.jcarousel {
    position: relative;
    overflow: hidden;
    width: 600px;
    height: 400px;
}

.jcarousel ul {
    width: 20000em;
    position: relative;
    list-style: none;
    margin: 0;
    padding: 0;
}

.jcarousel li {
    float: left;
}

/** Carousel Controls **/

.jcarousel-control-prev,
.jcarousel-control-next {
    position: absolute;
    top: 200px;
    width: 30px;
    height: 30px;
    text-align: center;
    background: #4E443C;
    color: #fff;
    text-decoration: none;
    text-shadow: 0 0 1px #000;
    font: 24px/27px Arial, sans-serif;
    -webkit-border-radius: 30px;
    -moz-border-radius: 30px;
    border-radius: 30px;
    -webkit-box-shadow: 0 0 2px #999;
    -moz-box-shadow: 0 0 2px #999;
    box-shadow: 0 0 2px #999;
}

.jcarousel-control-prev {
    left: -50px;
}

.jcarousel-control-next {
    right: -50px;
}

.jcarousel-control-prev:hover span,
.jcarousel-control-next:hover span {
    display: block;
}

.jcarousel-control-prev.inactive,
.jcarousel-control-next.inactive {
    opacity: .5;
    cursor: default;
}

/** Carousel Pagination **/

.jcarousel-pagination {
    position: absolute;
    bottom: 0;
    left: 15px;
}

.jcarousel-pagination a {
    text-decoration: none;
    display: inline-block;

    font-size: 11px;
    line-height: 14px;
    min-width: 14px;

    background: #fff;
    color: #4E443C;
    border-radius: 14px;
    padding: 3px;
    text-align: center;

    margin-right: 2px;

    opacity: .75;
}

.jcarousel-pagination a.active {
    background: #4E443C;
    color: #fff;
    opacity: 1;
    text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.75);
}

您可以在www.sorgalla.com/jcarousel/docs找到jcarousel的文档./。

答案 2 :(得分:0)

请参阅演示 - http://codepen.io/vsync/pen/waKju?editors=011

的JavaScript

/**
* Slider - loops over images
* SEP 2014
* By - Yair Even-Or
*/
var Slider = function(elm, prev, next){
    var that = this;
    this.locked = false;
    this.slider = elm;
    this.children = this.slider.children;
    this.itemWidth = this.children[0].clientWidth;

    this.preloadImages();

    next && next.addEventListener('click', function(){ that.move('next') });
    prev && prev.addEventListener('click', function(){ that.move('prev') });
}

Slider.prototype = { 
    move : function(dir){
        var that = this, 
            itemToMove;

        if( this.locked ){ 
            this.locked.removeAttribute('style');
            this.slider.appendChild(this.locked);
            clearTimeout(this.timer);
            moveToEnd();
        }

        // do nothing if there are no items
        if( this.children.length < 2 )
            return false;

        itemToMove = this.children[0];
        this.locked = itemToMove;

        if( dir == 'next' )
            itemToMove.style.marginLeft = -this.itemWidth + 'px';
        else{
            itemToMove = this.children[this.children.length-1];
            itemToMove.style.marginLeft = -this.itemWidth + 'px';
            this.slider.insertBefore(itemToMove, this.children[0]);
            setTimeout(function(){ 
                itemToMove.removeAttribute('style');
            },50);
            this.preloadImages();
            this.locked = false;
        }

        // move the child to the end of the items' list
        if( dir == 'next' )
            this.timer = setTimeout(moveToEnd, 420);

        function moveToEnd(el){
            itemToMove = el || itemToMove;
            if( !itemToMove ) return;
            itemToMove.removeAttribute('style');
            that.slider.appendChild(itemToMove);
            that.locked = false;
            that.preloadImages();
        }
    },
    preloadImages : function(){
      this.lazyload(this.children[1].getElementsByTagName('img')[0] );
      this.lazyload(this.children[this.children.length-1].getElementsByTagName('img')[0] );
    },
    // lazy load image
    lazyload : function(img){
        var lazy = img.getAttribute('data-src');
        if( lazy ){
            img.src = lazy;
            img.removeAttribute('data-src');
        }
    }
}




// HOW TO USE /////////////////
var sliderElm = document.querySelector('.content'),
    next = document.querySelector('.next'),
    prev = document.querySelector('.prev'),
    slider = new Slider(sliderElm, prev, next);

HTML(JADE语法)

.slider
    a.arrow.next
    a.arrow.prev
    ul.content
        li
            img(src='image1.jpg')
        li
            img(src='image2.jpg')
        li
            img(src='image3.jpg')
        li
            img(src='image4.jpg')