无缝连接可重复和不可重复的背景
我有一个 700x300 的背景,在主要内容 div 内无缝重复。现在我想在 content-div 的底部附加一个 div,其中包含不可重复的不同背景图像,与其上方的可重复背景无缝连接。本质上,不可重复图像看起来就像可重复图像的末端部分。
由于图案的性质,除非背景图像的完整 300px 高度在 content-div 背景的最后一次重复中可见,否则下面的 div 中的背景将不会无缝连接。基本上,在所有情况下我都需要内容 div 的高度为 300px 的倍数。解决此类问题的好方法是什么?
我尝试在加载页面时调整内容 div 的大小,但这仅在内容 div 不包含任何调整大小的动态内容时才有效,这不是我的情况:
function adjustContentHeight()
{
// Setting content div's height to nearest upper multiple of column backgrounds height,
// forcing it not to be cut-off when repeated.
var contentBgHeight = 300;
var contentHeight = $("#content").height();
var adjustedHeight = Math.ceil(contentHeight / contentBgHeight);
$("#content").height(adjustedHeight * contentBgHeight);
}
$(document).ready(adjustContentHeight);
我正在寻找一种方法响应 div 调整大小事件,但 似乎没有是这样的事情。另外,请假设我无法访问控制 content-div 中内容大小调整的 JS,尽管这可能是解决问题的一种方法。
我正在考虑的另一个可能的解决方案是根据内容 div 的高度将底部 div 中的背景图像偏移一定量。同样,缺少的部分似乎是响应调整大小事件的能力。
I have a 700x300 background repeating seamlessly inside of the main content-div. Now I'd like to attach a div at the bottom of the content-div, containing a different background image that isn't repeatable, connecting seamlessly with the repeatable background above it. Essentially, the non-repeatable image will look like the end piece of the repeatable image.
Due to the nature of the pattern, unless the full 300px height of the background image is visible in the last repeat of the content-div's backround, the background in the div below won't seamlessly connect. Basically, I need the content div's height to be a multiple of 300px under all circumstances. What's a good approach to this sort of problem?
I've tried resizing the content-div on loading the page, but this only works as long as the content div doesn't contain any resizing, dynamic content, which is not my case:
function adjustContentHeight()
{
// Setting content div's height to nearest upper multiple of column backgrounds height,
// forcing it not to be cut-off when repeated.
var contentBgHeight = 300;
var contentHeight = $("#content").height();
var adjustedHeight = Math.ceil(contentHeight / contentBgHeight);
$("#content").height(adjustedHeight * contentBgHeight);
}
$(document).ready(adjustContentHeight);
What I'm looking for there is a way to respond to a div resizing event, but there doesn't seem to be such a thing. Also, please assume I have no access to the JS controlling the resizing of content in the content-div, though this is potentially a way of solving the problem.
Another potential solution I was thinking off was to offset the background image in the bottom div by a certain amount depending on the height of the content-div. Again, the missing piece seems to be the ability to respond to a resize event.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
另一种方法是根据内容 DIV 的大小计算底部和顶部 DIV 的
background-position
样式。您可以使用负位置将一个的底部与另一个的顶部对齐。另一种方法是使用分层 DIV 方法,其中顶部、内容和底部都是包含背景的父 DIV 的子项。
这些方法的好处是,它不会仅仅为了管理背景而改变内容 DIV 的自然呈现。
示例:http://bin.googlecode.com/svn/ trunk/css/repeating-bg-content.html
Another approach is to calculate the
background-position
style for the bottom and top DIVs based on the size of the content DIV. You can use negative positions to align the bottom of one to the top of another.Yet another approach is to use a layered DIV approach in which the top, content and bottom are all children of a parent DIV that contains the background.
The benefit of these approaches is that it doesn't change the natural rendering of the content DIV simply for managing the background.
Example: http://bin.googlecode.com/svn/trunk/css/repeating-bg-content.html
在你的CSS中设置
background-position:fixed
可以帮助吗?然后您的底部 div 可以移动,但其背景图像相对于页面顶部将保持固定。随着更多的重复图像被显示,更多的底部图像将被隐藏。这将属于“根据内容 div 的高度将底部 div 中的背景图像偏移一定量”的标题,而不是“在所有情况下内容 div 的高度都是 300px 的倍数”的标题。
Could setting
background-position: fixed
in your css help? Then your bottom div could move, but its background image would remain fixed in relation to the top of the page. As more of your repeating image was revealed, so more of your bottom image would be hidden.This would comes under the heading of "offset the background image in the bottom div by a certain amount depending on the height of the content-div", rather than "the content div's height to be a multiple of 300px under all circumstances".
您可以尝试向 div 添加 事件监听器:
You could try adding an event listener to the div:
如果顶部没有任何内容需要匹配,请将重复图像放置在 div 的底部(这样溢出就会溢出顶部)。像这样:
div#repeating {background:transparent url('/path/to/image')repeat left Bottom; }
If nothing needs to match up at the top, position the repeating image at the bottom of the div (so the overflow will spill over the top). Like this:
div#repeating { background: transparent url('/path/to/image') repeat left bottom; }