简体   繁体   English

移动设备中div的可滑动水平滚动

[英]Swipeable horizontal scrolling in div on mobile

So I have a page where I have a main image frame, and below it I have thumbnails. 因此,我有一个页面,其中有一个主图像框架,在它下面有缩略图。 When the user clicks on a thumbnail, the main image frame changes the image to the image shown in the clicked thumbnail. 当用户单击缩略图时,主图像框会将图像更改为单击的缩略图中显示的图像。 Like a carousel. 像旋转木马。 See the image below. 参见下图。

Now, my problem is, if the user will add 20 thumbnails, I don't wanna show all. 现在,我的问题是,如果用户将添加20个缩略图,我不想全部显示。 I want the thumbnails to always show just the 3 and be swipeable on mobile, so you can scroll horizontally and browse the thumbnails. 我希望缩略图始终只显示3,并且可以在移动设备上滑动,因此您可以水平滚动并浏览缩略图。

So let's say my code is: 所以说我的代码是:

<div class="main-image-frame">
   <img src="img.jpg">
</div>
<div class="thumbnail-container">
  <div class="thumbnail>
    <img src="tnail.jpeg">
  </div>
  <div class="thumbnail>
    <img src="tnail.jpeg">
  </div>
  <div class="thumbnail>
    <img src="tnail.jpeg">
  </div>
  <div class="thumbnail>
    <img src="tnail.jpeg">
  </div>
  <div class="thumbnail>
    <img src="tnail.jpeg">
  </div>
  <div class="thumbnail>
    <img src="tnail.jpeg">
  </div>
</div>

How do I make it horizontally scrollable and swipeable in mobile? 如何在移动设备中使其水平滚动和滑动? I guess a combination of CSS and js but I have no idea how to start 我猜是CSS和js的结合,但是我不知道如何开始

可滚动的缩略图

No need to use JavaScript. 无需使用JavaScript。 It can be done using CSS. 可以使用CSS来完成。

Set the container to a fixed width, and give it two important properties: 将容器设置为固定宽度,并为其指定两个重要属性:

overflow-x:scroll;
white-space:nowrap;

Then for each thumbnail, have the following: 然后,对于每个缩略图,请执行以下操作:

display:inline-block;

 .container { background-color:red; height:100px; width:calc(100% - 40px); margin:10px; overflow-x:scroll; padding:10px; white-space: nowrap; } .thumbnail { background-color:blue; display:inline-block; height:100%; width:30%; } .thumbnail img { height:100%; width:100%; } 
 <div class="container"> <div class="thumbnail"> <img src="http://www.bobbyberberyan.com/wp-content/uploads/2012/03/HTML5CSS3Logos.svg"></img> </div> <div class="thumbnail"> <img src="http://www.bobbyberberyan.com/wp-content/uploads/2012/03/HTML5CSS3Logos.svg"></img> </div> <div class="thumbnail"> <img src="http://www.bobbyberberyan.com/wp-content/uploads/2012/03/HTML5CSS3Logos.svg"></img> </div> <div class="thumbnail"> <img src="http://www.bobbyberberyan.com/wp-content/uploads/2012/03/HTML5CSS3Logos.svg"></img> </div> <div class="thumbnail"> <img src="http://www.bobbyberberyan.com/wp-content/uploads/2012/03/HTML5CSS3Logos.svg"></img> </div> <div class="thumbnail"> <img src="http://www.bobbyberberyan.com/wp-content/uploads/2012/03/HTML5CSS3Logos.svg"></img> </div> <div class="thumbnail"> <img src="http://www.bobbyberberyan.com/wp-content/uploads/2012/03/HTML5CSS3Logos.svg"></img> </div> </div> 

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

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