以间隔扩大div高度
我正在使用一个以图案图像作为背景的 div。该图案是可重复的,但最好以像素间隔扩展。
我想知道是否有一种解决方案可以以固定间隔扩展 div,但仍会在文本填充 div 时扩展。
假设图案的最佳观看间隔为 10px,例如。高度:120px、130px、140px 等 div 存在动态内容,并且当 div 的高度扩展时,高度固定为间隔内的下一个可能的高度。 例如。 div 的原始高度为 132px,应自动转换为 140px。
这可能吗?我添加了 jquery 标签,因为我认为单独使用 CSS 是不可能的,而 javascript/jquery 是我对解决方案的猜测:)
解决方案:
我结合了两个现有答案的解决方案:
$(function(){
var DivSize = function(el) {
$(el).css('height',Math.ceil( $(el).height() / 10 ) * 10+'px');
}
DivSize("#test");
});
这似乎做这份工作!
I'm working with a div that has a pattern-image as background. The pattern is repeatable but is preferred to expand with an interval of pixels.
I would like to know if there is a solution to expand a div with a fixed interval but still expand as text fills the div.
Say the pattern is best viewed with the interval of 10px, eg. height:120px, 130px, 140px etc.
A div is present with dynamic content and as the height of the div expands the height is fixed to the next possible height within the interval.
Eg. the original height of the div is 132px, which should be transformed into 140px automatically.
Is this possible? I've added the jquery tag since I assume this is not possible with CSS alone and javascript/jquery is my guess for a solution :)
SOLUTION:
I combined a solution of the two existing answers:
$(function(){
var DivSize = function(el) {
$(el).css('height',Math.ceil( $(el).height() / 10 ) * 10+'px');
}
DivSize("#test");
});
This seems to do the job!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
现有解决方案的问题在于,它们没有考虑到您在 div 上设置静态高度,无论内容是什么,该高度都不会改变。因此,您需要一个元素来告诉您想要的内容高度,然后包装元素保留背景并根据内部内容的高度变化进行扩展。
这个 jsFiddle 示例应该不言而喻,间隔当然是用于测试/演示,就像 css 一样。
http://jsfiddle.net/sg3s/uHsVg/
示例 JS:
示例 HTML:
此扩展 div不需要是绝对的或任何东西,它就像任何普通的 html 块元素一样,设置了静态高度。
编辑:要使其缩小,只需删除treshhold if语句,我只是将其放在那里,这样它只会在真正的更改上设置高度,而不仅仅是任何更改。 ..
The problem with the existing solutions is that they don't take into consideration that you're setting a static height on a div which will then not change no matter what the content is. So you will need an element to tell you want the content height is, then the wrapping element holds the background and is expanded according to the height changes in the inner content.
This jsFiddle example should speak for itself, the interval is ofcourse for testing / demo as is the css.
http://jsfiddle.net/sg3s/uHsVg/
The example JS:
Example HTML:
This expanding div doesn't need to be absolute or anything, it'll act like any normal html block element with a static height set to it.
Edit: to also make it shrink just remove the treshhold if statement, I just put that there so it would only set the height on a real change and not just any change...
试试这个CSS:
Try this CSS:
如果元素具有填充或边框(如果有,您需要考虑这些)并且尚未经过测试,则此方法不起作用。
它不断检查自动高度是否已更改,将其上限值到最接近的 10 并将其设置为
高度。
This doesn't work if the element has padding or border (if it has, you need to account for those) and hasn't been tested.
It keeps checking if the automatic height has changed, ceils it up to nearest 10 and sets it as
the height.
啊,我明白了,所以你总是希望你的 div 大小是 10 的倍数。
你应该在内容更新时触发或调用一个函数,然后将 div 高度设置为下一个最大倍数,例如:
你的 div 将必须为位置:绝对;或:亲属;让 height() 起作用。
Ahh, I see, so you always want your div size to be a multiple of 10.
You should fire or call a function when the content is updated, then just set the div height to the next greatest multiple, something like:
Your div will beed to be position:absolute; or :relative; for height() to work.