如何使我的<ol>列表水平滚动

时间:2017-06-23 15:02:16

标签: html css

这是fiddle

我正在尝试在<ol>列表

上进行水平滚动

这是我的代码:

&#13;
&#13;
.scroll {
  overflow: auto;
  white-space: nowrap;
}

.card {
  float: left;
  width: 240px;
  list-style-type: none;
}
&#13;
<section class="scroll">
  <ol>
    <li class="card">
      <a href="#">
        <img src="https://image.tmdb.org/t/p/w240_and_h266_bestv2/jYohlUVkLPFdnDryZ8V1HZkJzDt.jpg" />
      </a>
      <p class="castname"><a href="#">Aamir Khan</a></p>
    </li>

    <li class="card">
      <a href="#">
        <img src="https://image.tmdb.org/t/p/w240_and_h266_bestv2/jYohlUVkLPFdnDryZ8V1HZkJzDt.jpg" />
      </a>
      <p class="castname"><a href="#">Aamir Khan</a></p>
    </li>

    <li class="card">
      <a href="#">
        <img src="https://image.tmdb.org/t/p/w240_and_h266_bestv2/jYohlUVkLPFdnDryZ8V1HZkJzDt.jpg" />
      </a>
      <p class="castname"><a href="#">Aamir Khan</a></p>
    </li>

    <li class="card">
      <a href="#">
        <img src="https://image.tmdb.org/t/p/w240_and_h266_bestv2/jYohlUVkLPFdnDryZ8V1HZkJzDt.jpg" />
      </a>
      <p class="castname"><a href="#">Aamir Khan</a></p>
    </li>

    <li class="card">
      <a href="#">
        <img src="https://image.tmdb.org/t/p/w240_and_h266_bestv2/jYohlUVkLPFdnDryZ8V1HZkJzDt.jpg" />
      </a>
      <p class="castname"><a href="#">Aamir Khan</a></p>
    </li>
  </ol>
</section>
&#13;
&#13;
&#13;

我添加了这个,但我不知道我的代码中有什么问题。

overflow: auto;
  white-space: nowrap; 

4 个答案:

答案 0 :(得分:0)

.scroll>ol{ display: flex; }

答案 1 :(得分:0)

删除左边的浮动并替换为display inline-block;

.scroll {
  overflow: auto;
  white-space: nowrap;
}
.card {
    display: inline-block;
	width:240px;
	list-style-type:none;
}
<section class="scroll">
		    <ol>
			    <li class="card">
				    <a href="#">
					
					<img src="https://image.tmdb.org/t/p/w240_and_h266_bestv2/jYohlUVkLPFdnDryZ8V1HZkJzDt.jpg" />
					
					</a>
					
					<p class="castname"><a href="#">Aamir Khan</a></p>
					
				</li>
		
				
				<li class="card">
				    <a href="#">
					
					<img src="https://image.tmdb.org/t/p/w240_and_h266_bestv2/jYohlUVkLPFdnDryZ8V1HZkJzDt.jpg"/>
					
					</a>
					
					<p class="castname"><a href="#">Aamir Khan</a></p>
					
				</li>
				
				<li class="card">
				    <a href="#">
					
					<img src="https://image.tmdb.org/t/p/w240_and_h266_bestv2/jYohlUVkLPFdnDryZ8V1HZkJzDt.jpg"/>
					
					</a>
					
					<p class="castname"><a href="#">Aamir Khan</a></p>
					
				</li>
				
				<li class="card">
				    <a href="#">
					
					<img src="https://image.tmdb.org/t/p/w240_and_h266_bestv2/jYohlUVkLPFdnDryZ8V1HZkJzDt.jpg"/>
					
					</a>
					
					<p class="castname"><a href="#">Aamir Khan</a></p>
					
				</li>
				
				<li class="card">
				    <a href="#">
					
					<img src="https://image.tmdb.org/t/p/w240_and_h266_bestv2/jYohlUVkLPFdnDryZ8V1HZkJzDt.jpg"/>
					
					</a>
					
					<p class="castname"><a href="#">Aamir Khan</a></p>
					
				</li>
		
	
	        </ol>
	
	
	</section>

答案 2 :(得分:0)

white-space:nowrap仅适用于inlineinline-block元素。

.scroll {
  overflow: auto;
  white-space: nowrap;
}
.card {
  display:inline-block;
  width:240px;
  list-style-type:none;
}
<section class="scroll">
  <ol>
    <li class="card">
      <a href="#"><img src="https://image.tmdb.org/t/p/w240_and_h266_bestv2/jYohlUVkLPFdnDryZ8V1HZkJzDt.jpg" /></a>
      <p class="castname"><a href="#">Aamir Khan</a></p>
    </li>
    <li class="card">
      <a href="#"><img src="https://image.tmdb.org/t/p/w240_and_h266_bestv2/jYohlUVkLPFdnDryZ8V1HZkJzDt.jpg" /></a>
      <p class="castname"><a href="#">Aamir Khan</a></p>
    </li>
    <li class="card">
      <a href="#"><img src="https://image.tmdb.org/t/p/w240_and_h266_bestv2/jYohlUVkLPFdnDryZ8V1HZkJzDt.jpg" /></a>
      <p class="castname"><a href="#">Aamir Khan</a></p>
    </li>
    <li class="card">
      <a href="#"><img src="https://image.tmdb.org/t/p/w240_and_h266_bestv2/jYohlUVkLPFdnDryZ8V1HZkJzDt.jpg" /></a>
      <p class="castname"><a href="#">Aamir Khan</a></p>
    </li>
    <li class="card">
      <a href="#"><img src="https://image.tmdb.org/t/p/w240_and_h266_bestv2/jYohlUVkLPFdnDryZ8V1HZkJzDt.jpg" /></a>
      <p class="castname"><a href="#">Aamir Khan</a></p>
    </li>
  </ol>
</section>

答案 3 :(得分:0)

您需要做的只是改变

overflow: auto;

overflow-y: scroll;

然后设置滚动的宽度和高度。这是我的JSFiddle