jQuery的简单的水平滑块

诺汉

你好呀,

我尝试使用Jquery scrollLeft()方法创建非常简单的滑块我找到了一些答案,我在这里尝试了这个..但是没有用....我还是jquery的初学者,不知道为什么。

的HTML

   <div class="gallery-slider ">
       <div class="images-preview">
           <img alt="" class="img-responsive" src="http://placehold.it/300">
           <img alt="" class="img-responsive" src="http://placehold.it/300">
           <img alt="" class="img-responsive" src="http://placehold.it/300">
           <img alt="" class="img-responsive" src="http://placehold.it/300">
           <img alt="" class="img-responsive" src="http://placehold.it/300">
           <img alt="" class="img-responsive" src="http://placehold.it/300">
           <img alt="" class="img-responsive" src="http://placehold.it/300">
           <img alt="" class="img-responsive" src="http://placehold.it/300">
           <img alt="" class="img-responsive" src="http://placehold.it/300">
           <img alt="" class="img-responsive" src="http://placehold.it/300">
           <img alt="" class="img-responsive" src="http://placehold.it/300">
           <img alt="" class="img-responsive" src="http://placehold.it/300">
       </div>
       <div class="controls">
           <div class="right-arrow"><i class="fa fa-angle-left fa-3x"></i></div>
           <div class="left-arrow"><i class="fa fa-angle-right fa-3x"></i></div>
       </div>
   </div>

的CSS

    .gallery-slider {
    position: relative;
}

.gallery-slider .images-preview {
    margin: auto;
    height: 230px;
    overflow: hidden;
}

.gallery-slider .images-preview img {
    display: inline-block;
    overflow: visible;
    width: 410px;
    margin: 10px 17px;
}
.gallery-slider .images-preview img, .controls {
        height: 200px;
    width: 26%;
}
/* Controls */
.controls {
    position: absolute;
    top: 10px;
    width: 100%;
}


.right-arrow, .left-arrow {
    display: inline-block;

    padding: 62px;
    background-color: rgba(255, 255, 255, 0.76);
    position: absolute;
    height: 100%;
    cursor: pointer;
}
.right-arrow i, .left-arrow i{
        margin: 23px 20px;
}
.left-arrow {
    right: 0px;
}

.right-arrow {
    left: 0px;
    text-align: right;
}

jQuery的

  $(".left-arrow").click(function () { 
      var leftPos = $('.images-preview img').scrollLeft();
      $(".images-preview img").animate({scrollLeft: leftPos - 100}, 1000);
    });

    $(".right-arrow").click(function () { 
      var leftPos = $('.images-preview img').scrollLeft();
      $(".images-preview img").animate({scrollLeft: leftPos + 100}, 1000);
    });

所以有帮助吗?

提前致谢

在这里摆弄

更新:我也需要它scrollleft():0在滚动结束时返回

斯坦利1943

您要的是简单但充满问题的东西。这些问题使事情变得复杂。

  1. 我已经将图片预览位置设为绝对。它允许您通过控制向左(css)进行滚动。无法使scrollLeft正常工作。不知道为什么。如果有人这样做,我很想知道。
  2. 需要计算图像预览中的img数量。允许您添加或删除图像。
  3. 添加了var active以防止单击速度太快。

javascript:

var target = $('.images-preview');

//get the total number of images
var total = $('.images-preview img').length;
//calculate the width of the image-preview
var width = total * 300 + total * 40;
var c = 1;
// 80 is to center the image-preview 
var originalLeft = 80;
// 300 is the image size, 40 is the total margin (this is how many px image-preview
// would have to move left for one image
var totalImg = 300 + 40;
// startToEnd is the total width when you click left(arrow-right) on first image
var startToEnd = width -originalLeft -340;
var a = '';
//need this to prevent multiple clicks
var active = false;

//put in the width at page rendering
$(document)function(){
    target.css('width', width);
});

 $(".left-arrow").click(function () { 
    if (active === false){
        if (c === total){
            a = originalLeft;
            c = 1;
        }else{
            a = '-='+totalImg;
            c++;
        }
        //turn the active to true to prevent another animation from activating
        active = true;
        target.animate(
            {left: a},
            {duration:500,
            //turn the active off after animation is complete
            complete: function(){
                active = false;
            }
        });
    }
 });
 $(".right-arrow").click(function () { 
    if (active === false){
        if (c === 1){
            a = '-'+startToEnd;
            c = total;
        }else{
            a = '+='+totalImg;

            c--;
        }
        active = true;
        target.animate(
            {left: a},
            {duration:500,
            complete: function(){
                active = false;
            }
        });
    }
 });

CSS:

.gallery-slider{
    width:500px;
    height:300px;
    position:relative;
    overflow:hidden;
}
.images-preview{
    width:300px;
    height:300px;
    position:absolute;
    left:80px;
}
.images-preview img{
    width:300px;
    height:300px;
    position:relative;
    float:left;
    margin:0 20px;
}
.control{
    width:100%;
    height:100%;
    position:relative;
}
.right-arrow, .left-arrow{
    position:absolute;
    padding:0 26px;
}
.right-arrow i, .left-arrow i{
    line-height:300px;
}
.right-arrow{
    left:0;   
}
.left-arrow{
    right:0;
}

这是演示:https : //jsfiddle.net/ood26n7b/1/

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

jQuery UI:水平滑块向右移得太远

来自分类Dev

将jQuery水平滑块更改为垂直

来自分类Dev

Rgd:双水平滑块

来自分类Dev

水平滑块上的标记

来自分类Dev

全页水平滑块

来自分类Dev

jQuery滑块:水平图像滑块

来自分类Dev

使用jQuery为文本列表创建水平滑块式滚动条

来自分类Dev

水平JQuery UI滑块

来自分类Dev

jQuery简单滑块

来自分类Dev

简单的jQuery图像滑块

来自分类Dev

jQuery水平滑动效果

来自分类Dev

带有单击控件的嵌套水平滑块

来自分类Dev

水平滑动菜单类型图像滑块

来自分类Dev

自动滑动水平滑块也可在单击时滑动

来自分类Dev

如何实现水平jQuery滑块效果

来自分类Dev

jQuery滑块下降如何使其水平

来自分类Dev

如何实现水平jQuery滑块效果

来自分类Dev

jQuery滑块下降如何使其水平

来自分类Dev

用5个步骤使jquery Ui滑块平滑

来自分类Dev

根据浏览器的宽度设置JQuery的平滑滑块值

来自分类Dev

jQuery click元素上的if语句(简单滑块)

来自分类Dev

麻烦让简单的jquery滑块正常工作

来自分类Dev

简单的JQuery滑块无法正常工作

来自分类Dev

jQuery简单ul li内容滑块

来自分类Dev

FullPage.js带有水平滑块的正常滚动

来自分类Dev

我可以给水平滑块css3边框半径吗?

来自分类Dev

溢出-y:在IE中自动中断水平滑块的布局

来自分类Dev

平板电脑上出现空白且宽度较小的水平滑块

来自分类Dev

jQuery在相邻div上的水平滑动标题