简体   繁体   English

当孩子不可见时,如何使固定高度和宽度的 div 水平滚动?

[英]How to make a div of fixed height and width scroll horizontally when childs are not more visible?

I need to create a sort of cart, where I store elements created by the user.我需要创建一种购物车,在其中存储用户创建的元素。 I've created a simple scheme to help you understand my needs:我创建了一个简单的方案来帮助您了解我的需求:方案

.buttons are two div containing an image (an arrow), where I'll bind an onclick event to scroll elements ( #scroller ). .buttons是两个包含图像(箭头)的 div,我将在其中将onclick事件绑定到滚动元素( #scroller )。 The layout has fixed sizes, thus the exact length of ul#scroller (the element that will contain items and that needs to be scrolled) is 900px.布局具有固定大小,因此ul#scroller (将包含项目并需要滚动的元素)的确切长度为 900 像素。 I think the size of any #scroller child <li> will be ~100px.我认为任何#scroller<li>的大小将是〜100px。

There's a button (not present in the scheme) that allows the user to store in #scroller an item.有一个按钮(方案中不存在)允许用户在#scroller中存储项目。

Actually when there are too many items the next will go on the bottom (beginning a new line).实际上,当项目太多时,下一个将 go 在底部(开始新行)。 Instead I'd like that the new elements go ahead on the same line, but hidden (without hit #button_right ).相反,我希望新元素 go 在同一行前面,但隐藏(没有点击#button_right )。

I was thinking to do this with javascript, storing elements in an array, and keeping visible only the first 9 (x 100px), then by clicking on the arrow (let's say, the right one) hide the first item and show the 10th.我想用 javascript 来做到这一点,将元素存储在一个数组中,并且只保持前 9 个(x 100px)可见,然后通过单击箭头(比如说,右边的那个)隐藏第一个项目并显示第 10 个。

Do you think this is a good solution?你认为这是一个好的解决方案吗?

If not, what do you suggest?如果没有,你有什么建议? What CSS rules could help me to do it?什么 CSS 规则可以帮助我做到这一点?

Thanks in advance.提前致谢。

you need to create an extra div with a very long width, and put it inside #scroller and make #scroller have an overflow: hidden so it doesn't show the scrollbar.您需要创建一个宽度很长的额外 div,并将其放在#scroller并使#scroller具有overflow: hidden ,因此它不会显示滚动条。

like this:像这样:

html: html:

<div id="scroller">
    <div id="inner">

       (your items)

    </div>
</div>

css: css:

#scroller {
    width: 900px;
    overflow: hidden;
}

#inner {
    width: 90000px;
}

PS now the items won't go to another line but you need to code the buttons so they scroll the content depending on the number of items, depending on their width behing the same it can be more simple or not. PS 现在项目不会 go 到另一,但您需要对按钮进行编码,以便它们根据项目的数量滚动内容,这取决于它们的宽度是否相同,它可能更简单。

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

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