如何使用水平滚动div内容div

时间:2014-06-23 05:38:51

标签: jquery css horizontal-scrolling

我想知道如何向我的div添加水平滚动。

HTML:

<div class="scroll-item">
     <div class="item"></div>
     <div class="item"></div>
     <div class="item"></div>
     <div class="item"></div>
</div>

CSS:

.scroll-item{
    background:#F2F2F2;
    padding: 10px;
    width: 300px;
    overflow-x: scroll;
    border:1px solid red;
}

.item{
    border-left:1px solid #fff;
    border-right:1px solid #ccc;
    height:200px;
    display:inline !important;
    position:relative; 
    float: left;
}

这是我想要实现的目标:
Horizontal Scroll

3 个答案:

答案 0 :(得分:2)

Demo

.scroll-item {
    width: 300px;
    height: 110px;
    overflow-x: scroll;
    overflow-y: hidden;
    background: #585453;
    white-space: nowrap;
    float: left;
    padding:10px;
}
.item {
    width: 109px;
    height: 81px;
    display: inline-block;
    border-top: none;
    /*float: left;*/
    position: relative;
    cursor: pointer;
    background:#fff;
}

答案 1 :(得分:2)

<强> Demo

CSS

.scroll-item {
    background:#F2F2F2;
    padding: 10px;
    width: 300px;
    overflow-x: auto; /* for horizontal scroll */
    overflow-y: hidden; /* no vertical scroll */
    white-space: nowrap; /* no wraping in second line */
    border:1px solid red;
}
.item {
    border-left:1px solid #fff;
    border-right:1px solid #ccc;
    height:50px;
    position:relative;
    background: url(http://tcrf.net/images/4/47/Placeholder.png);
    width: 50px;
    display: inline-block; /* display in one line */
}

答案 2 :(得分:0)

以下是 DEMO

.scroll-item{
   background:#F2F2F2;
   padding: 10px;
   width: 300px;
   overflow-x: scroll;
   border:1px solid red;
    white-space: nowrap;
 }

 .item{
   background:url('http://lorempixel.com/50/50/');
   height:200px;
   width:200px;
   display: inline-block;
   padding:0 5px;
   }