动态调整 div 大小/重新定位以实现多列查看
设置
我有一个网站,可以绘制 RSS 提要并将其显示在页面上。 目前,我在包含每个 feed 的 div 上使用百分比,以便多个 feed 可以彼此相邻出现。
但是,我只有两个相邻的窗口,如果窗口大小调整,屏幕上可能会出现一些难看的空白区域。
我希望
能够做的,但还没有找到一种方法,是将所有提要线性地放入页面中,并且具有:
- 一个“预构建”多列视图,其中提要将“平衡”它们本身进入列,
这使我发现:
- 列的数量根据当前屏幕的宽度而变化\
这类似于文字处理应用程序处理列式布局的方式。
问题
我想我需要实现某种形式的 AJAXy 快乐,但目前对 Javascript 知之甚少。
有没有办法用 仅 CSS/HTML/PHP 来做到这一点?
如果不是,我应该如何解决这个问题?
最终解决方案:(
#rss
{min-width: 10em;
max-width: 25em;
min-height: 15em;
max-height: 25em;
font-size: .97em;
float: left;
}
Setup
I have a website that draws RSS feeds and displays them on the page. Currently, I use percentages on the divs that contain each feed, so that multiples can appear next to each other.
However, I only have two next to each other, and if the window resizes, there can be some ugly empty space on the screen.
Desire
What I'd like to be able to do, but have not figured out a way yet, is to put all the feeds linearly into the page, and have:
- a 'pre-built' multicolumn view where the feeds would "balance" themselves into the columns
which leads me to:
- the number of columns change depending on how wide the screen is currently\
This is akin to how word processing applications handle columnar layouts.
Question
I presume that I will need to implement some form of AJAXy happiness, but currently know very little about Javascript.
Is there a way to do this with just CSS/HTML/PHP?
If not, how should I go about solving this?
final solution:
(based on @warpr's and @joh6nn's answers)
#rss
{min-width: 10em;
max-width: 25em;
min-height: 15em;
max-height: 25em;
font-size: .97em;
float: left;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
仅使用 CSS/HTML 可能无法获得您想要的结果,但您可以在某种程度上接近。
我在相册中使用的一个技巧是:
由于每个 div 具有相同的尺寸,因此当它们向左浮动时,它们将形成一个网格,其列数恰好适合您的浏览器。
除非您实际上修复了 div 的高度并使用 CSS 来剪辑内容,否则您将需要 javascript 来执行第 2 步,我所做的是:
这相当容易实现,但显然不是最佳的。 我期待阅读这里发布的任何更好的解决方案:)
You probably cannot get what you want with just CSS/HTML, but you can get somewhat close.
A trick I used for a photo album is this:
Because each div has the same dimensions, when they're floated left they will form a grid with exactly the number of columns that will fit in your browser.
Unless you actually fix the height of the divs and use CSS to clip the content, you will need javascript for step 2, what I did was:
This is fairly easy to implement, but is obviously not optimal. I look forward to reading any better solutions posted here :)
你也许可以用列表来做到这一点; 我从未尝试过,所以我不确定。
如果您使列表项显示:内联,则列表将变为水平而不是垂直。 从那里开始,如果您将列表填充到包含元素中并摆弄填充和边距,您可能能够使列表进行线扭曲,如文本:再次,我从未尝试过,所以我不这样做知道。
如果这项技术有效,我将非常有兴趣了解它。
you might be able to do this with lists; i've never tried it, so i'm not sure.
if you make list items display:inline, the list becomes horizontal instead of vertical. from there, if you stuff the list into a containing element and fiddle with the padding and margins, you may be able to get the list to line-warp, like text: again, i've never tried that, so i don't know.
if this technique works, i'd be very interested to hear about it.
我能想到的唯一方法是动态 CSS 和 javascript 的混合。 每次添加列(提要)时,请使用 javascript 重写每个 div 的宽度(以百分比表示)。
jQuery 在这里会派上用场。
如果我错了,请随时纠正我。 我的 jQuery 有点不稳定。
当然,如果您不使用 AJAX,您可以完全用 PHP 实现相同的解决方案。
The only way I can think of is a mixture of dynamic CSS and javascript. Every time a column (feed) is added, use the javascript to rewrite the width (in percentage) of each div.
jQuery would come in handy here.
Someone feel free to correct me if I'm wrong. My jQuery is a little wobbly.
Of course, if you're not using AJAX, you could implement the same solution entirely in PHP.
您还可以使用此 jQuery javascript(您将需要 jQuery 库)。
这将动态设置列宽。
为此,您的列应该具有 'feed' 类 编辑
:
您应该将您的 div 设置为如下所示的样式:
You could also use this jQuery javascript (you will need the jQuery library).
Which would set the column width dynamically.
For this to work your columns should have the class 'feed'
EDIT:
You should style your divs something like this: