简体   繁体   English

当项目超过div宽度时,水平滚动/轮播

[英]Horizontal Scroll/Carousel when items exceed div width

在此处输入图片说明

I have a fixed size <div> which contains blocks. 我有一个固定大小的<div> ,其中包含块。 By clicking the + a user is able to add additional boxes (an infinite amount theoretically). 通过单击+ ,用户可以添加其他框(理论上是无限量)。

When the number of "boxes" exceeds the width of the <div> container I would like the user to be able to scroll left and/or right in-order to view all boxes. 当“盒子”的数量超过<div>容器的宽度时,我希望用户能够向左和/或向右滚动以查看所有盒子。

Currently I am looking for a place to start as I have not implemented such a feature before. 目前,我正在寻找一个起点,因为我以前没有实现过这样的功能。

There are JQuery examples out there however I would like to avoid JQuery if at all possible. 有一些JQuery示例,但是我想尽可能避免使用JQuery。

Please assist. 请协助。

Following my comment and suggesting you to use KendoUI ScrollView and seeing that you missed the option itemsPerPage , I will slightly modify KendoUI demo for showing you how to use it. 继我的意见,并建议您使用KendoUI滚动型和看到你错过了选项itemsPerPage ,我会稍微修改KendoUI演示为您展示如何使用它。

When you want to show more than one item per page, you simply define how many you want in the initialization of the widget. 如果要在每页上显示多个项目,则只需在小部件的初始化中定义所需的数量。 Example: 例:

  $("#scrollview").kendoMobileScrollView({
      dataSource:    dataSource,
      contentHeight: 150,
      itemsPerPage:  4,
      enablePager:   false,
      template:      kendo.template($("#template").html())
  });

You might also define a template where you define how to render each page. 您还可以定义一个模板,在其中定义如何呈现每个页面。 Example: 例:

<script id="template" type="text/x-kendo-template">
    <table>
        <tr>
            # for (var i = 0; i < data.length; i++) { #
            <td>
                <img src="#: data[i].image_url #"/>
            </td>
            # } #
        </tr>
    </table>
</script>

When you put all together, you have: http://jsfiddle.net/OnaBai/5M9Vw/ 当您放在一起时,您将拥有: http : //jsfiddle.net/OnaBai/5M9Vw/

I found a fairly straightforward algorithm to handle the left/right scrolling. 我找到了一种相当简单的算法来处理左右滚动。

With a bit of work I should be able to get this working without JQuery. 通过一些工作,我应该能够在没有JQuery的情况下使它工作。

JFiddle example is here . JFiddle示例在这里

HTML 的HTML

<div class="mtabArrowLeft">&laquo;</div>
<div class="menuTabs">
        <div class="img-reel">
            <input class="menutabBTN" name="" type="button" value="a" />
            <input class="menutabBTN" name="" type="button" value="b" />
            <input class="menutabBTN" name="" type="button" value="c" />
            <input class="menutabBTN" name="" type="button" value="d" />
            <input class="menutabBTN" name="" type="button" value="e" />
            <input class="menutabBTN" name="" type="button" value="f"/>
        </div>
</div>
<div class="mtabArrowRight">&raquo;</div>

JavaScript 的JavaScript

$(function() {
            var imageWidth = 71;
            var reelSize = 4;
            var imageSum = $('.img-reel input').size();
            var imageReelWidth = imageWidth * imageSum;
            $('.img-reel').css({'width' : imageReelWidth});

            rotate = function(){
                var trigger = $btn.attr('class');
                var image_reelPosition = (trigger=='mtabArrowLeft') ? -imageWidth : imageWidth;
                var reel_currentPosition = $('.img-reel').css('left').replace('px','');
                var pos = reel_currentPosition-image_reelPosition;
                var maxPos = (imageSum-reelSize)*-imageWidth;
                //console.log('pos='+pos+', max='+maxPos);
                if(pos>=maxPos && pos<=0){
                    $('.img-reel').animate({left:pos},300);
                    $('.mtabArrowLeft,.mtabArrowRight').fadeTo(250,1);
                    //console.log('move');
                    if(pos==maxPos){$('.mtabArrowRight').fadeTo(250,0.2);}
                    else if(pos==0){$('.mtabArrowLeft').fadeTo(250,0.2);}
                }
            };
            if (imageSum > 4) {
                $('.mtabArrowLeft,.mtabArrowRight').click(function(){
                    $btn = $(this);
                    rotate();
                    return false;
                });
            }
            else {
                $('.mtabArrowLeft,.mtabArrowRight').fadeTo(0,0.2).click(function(){return false});
            }
    })

CSS 的CSS

html,body,h1,h2,h3,h4,h5,p,ul,li,form,button { margin:0; padding:0 }
body { margin:20px; font:normal 62.5% tahoma }
p { margin:20px; }

.menuTabs {
    float: left;
    width: 284px;
    overflow:hidden;
    position:relative;
    height:50px;
}

.img-reel { position:absolute; left:0; top:0; height:50px; }

.mtabArrowLeft {
    float: left;
    height: 25px;
    width: 35px;
    margin-left: 15px;
    margin-right: 4px;
     cursor:pointer;
  font-size:20px;
}

.mtabArrowRight {
    float: left;
    height: 25px;
    width: 35px;
    margin-left: 15px;
    margin-right: 15px;
  cursor:pointer;
  font-size:20px;
}

      .mtabArrowLeft, .mtabArrowRight { color:#fff; font-weight:bold; background:red; text-indent:12px; border-radius:5px; -moz-border-radius:5px; -webkit-border-radius:5px; line-height:21px }

.menutabBTN {
    float: left;
    height: 25px;
    width: 65px;
    margin-right: 3px;
    margin-left: 3px;
    padding: 0px;
    border-top-width: 0px;
    border-right-width: 0px;
    border-bottom-width: 0px;
    border-left-width: 0px;
    font-family: Tahoma, Geneva, sans-serif;
    font-size: 12px;
    color: #000;
    text-align: center;
    line-height: 25px;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM