3

我正在尝试使用 div 和 javascript 创建一个简单的滑块。我设置了一个 div,其中包含六个图像和一个箭头,每次单击时都会将包含图像的容器播放为 528px(每个图像的宽度)。当我到达画廊的开头或结尾时,我希望相应的箭头按钮淡出,这样用户就不会一直按下下一个/上一个。

任何帮助表示赞赏。

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>
4

3 回答 3

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')
于 2014-09-26T00:54:24.423 回答
0

试试woothemes 的flex 滑块,它可以满足您的所有需求。

于 2013-07-04T19:13:17.180 回答
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 的文档。

于 2014-07-25T14:36:14.917 回答