gpt4 book ai didi

javascript - 多个元素上的连续背景图像图案

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:31:38 26 4
gpt4 key购买 nike

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

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>

最佳答案

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>

关于javascript - 多个元素上的连续背景图像图案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36304934/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com