你的图像的大小是多少?如果它非常小,一个简单的解决方案是在 photoshop/paint 中做到这一点。生成的图像会大 4 倍,但对于小图像(读取小于 25ko),我认为这是迄今为止最简单的方法。至少我没有看到任何简单的 CSS 解决方案来解决您的问题。
如果您的图片很大并且您知道它的高度,您最终可以采用以下方法:http: //jsfiddle.net/7GPTy/
这个想法是在z-index:-1
ofheight == image_height
和绝对位置创建几个具有不同左值的 div ......我认为这是一个粗略但可行的解决方案
var mybody = document.getElementById('body');
var bodyHeight = mybody.offsetHeight;
var imgHeight = 49; // you could get the size by opening the file
var offsetLines = 4; // this is the number of offset you asked, but you can change that!
var nbOfLines = Math.ceil(bodyHeight/imgHeight);
for(var i=0; i<nbOfLines; i++){
var newBgDiv = document.createElement('div');
newBgDiv.className = 'backgroundImg imgOffset_'+(i%offsetLines);
newBgDiv.style.top = (i*imgHeight) + 'px';
newBgDiv.style.height = imgHeight + 'px';
newBgDiv.style.width = '100%';
mybody.appendChild(newBgDiv);
}
to go through the js code quickly, I basically get the height of the page and divide it by the height of the picture to know how many divs i have to create to generate the entire background. Once done, you simply define the number of different row offset you want (offsetLines
) and you generate your divs.
In my example, the offset depends on the size of the window (try to resize horizontally your window, you'll see that the offset changes). You can of course fix it to a defined number of pixels !