【问题标题】:Continuous background-image pattern over several elements多个元素上的连续背景图像图案
【发布时间】:2016-03-30 09:37:28
【问题描述】:

我需要在多个元素上制作一个连续的背景图案。 我无法控制元素的高度或数量。
这是一个例子:

p{
  margin:0;
  padding:1.5em;
}
.bg{
  background-image:url('http://enjoycss.com/webshots/hB_1.png');
}
<p>some text<br>on several lines</p>
<p class="bg">some text<br>on several lines</p>
<p class="bg">some text<br>on several lines<br><br>and another<br>some text<br>on several lines<br><br>and another</p>
<p class="bg">some text<br>on several lines<br><br>and another</p>
<p>some text<br>on several lines<br><br>and another<br>some text<br>on several lines<br><br>and another</p>
<p class="bg">some text<br>on several lines</p>
<p class="bg">some text<br>on several lines<br><br>and another<br>some text<br>on several lines<br><br>and another</p>
<p class="bg">some text<br>on several lines<br><br>and another</p>

我要的效果用background-attachement:fixed;几乎可以实现,但是我需要背景随着内容滚动。
示例:

p{
  margin:0;
  padding:1.5em;
}
.bg{
  background-image:url('http://enjoycss.com/webshots/hB_1.png');
  background-attachment:fixed;
}
<p>some text<br>on several lines</p>
<p class="bg">some text<br>on several lines</p>
<p class="bg">some text<br>on several lines<br><br>and another<br>some text<br>on several lines<br><br>and another</p>
<p class="bg">some text<br>on several lines<br><br>and another</p>
<p>some text<br>on several lines<br><br>and another<br>some text<br>on several lines<br><br>and another</p>
<p class="bg">some text<br>on several lines</p>
<p class="bg">some text<br>on several lines<br><br>and another<br>some text<br>on several lines<br><br>and another</p>
<p class="bg">some text<br>on several lines<br><br>and another</p>

【问题讨论】:

  • 你不能把它们包装在div 中并为其分配背景吗?
  • @NicklasNygren 不,内容是由所见即所得的编辑器生成的,客户端无法制作容器,他只能更改p元素的类。

标签: javascript jquery css background background-image


【解决方案1】:

Javascript

使用少量的JavaScript,就可以得到你需要的效果。

此方法获取当前高度并添加所有先前的高度以赋予元素其起始背景位置。

var lastHeight = 0;
$('.bg').each(function() {
  $(this).css('background-position', '0 -' + lastHeight + 'px');
  var currentHeight = $(this).outerHeight();
  var newPosition = currentHeight + lastHeight;
  lastHeight = lastHeight + currentHeight;
});
p {
  margin: 0;
  padding: 1.5em;
}
.bg {
  background-image: url('http://enjoycss.com/webshots/hB_1.png');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>some text<br>on several lines</p>
<p class="bg">some text<br>on several lines</p>
<p class="bg">some text<br>on several lines<br><br>and another<br>some text<br>on several lines<br><br>and another</p>
<p class="bg">some text<br>on several lines<br><br>and another</p>
<p>some text<br>on several lines<br><br>and another<br>some 
<p class="bg">some text<br>on several lines</p>
<p class="bg">some text<br>on several lines<br><br>and another<br>some text<br>on several lines<br><br>and another</p>
<p class="bg">some text<br>on several lines<br><br>and another</p>
<p>some text<br>on several lines<br><br>and another<br>some text<br>on several lines<br><br>and another</p>

【讨论】:

    【解决方案2】:

    您可以让段落的最小高度/行高与背景图像大小相协调:

    p {
      margin: 0;
      line-height: 1em;
      background-image: url('http://i.imgur.com/TbQPryV.png');
      background-size: 1em 1em;
      min-height: 1em;
      outline: 1px dashed rgba(0,0,0,0.3);  
    }
    <p>some text<br>on several lines<br><br>and another</p>
    <p>some text<br>on several lines</p>
    <p>some text<br>on several lines<br><br>and another<br>some text<br>on several lines<br><br>and another</p>
    <p>some text<br>on several lines<br><br>and another</p>
    <p>some text<br>on several lines<br><br>and another<br>some text<br>on several lines<br><br>and another</p>

    【讨论】:

    • 不错的方法,但不幸的是我的背景图案高于行高。
    【解决方案3】:

    没有background:fixed,我已经完成了。请检查并让我知道它对你有用吗???

    Demo

    如果我不明白你的意思,请告诉我。

    【讨论】:

    • 感谢答案,但正如我评论的那样,我无法将元素包装在容器中以添加背景
    猜你喜欢
    • 2021-01-01
    • 1970-01-01
    • 2011-09-11
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 1970-01-01
    • 2019-08-18
    相关资源
    最近更新 更多