html - 背景图像垂直滚动但粘在容器的一侧

标签 html css image css-position

我有一个宽度为 1080 像素的标准容器。我想要一个 float 图像出现在容器的右侧,但是当您向下滚动时,图像也会向下滚动。我还希望 float 图像即使在调整窗口浏览器大小时也能粘在容器的右侧。

我试过将父 div 放在相对位置,将 float 图像放在绝对位置,图像将停留在父 div 的右侧,但是当我向下滚动时,它会保持相同的位置,而不是向下滚动。

这是我到目前为止使用的代码 -

CSS -

.santa-wrapper{
position: relative;
width: 1200px;
margin: 0 auto;
}

.santa{
background-image: url("images/santa-head.png");
background-repeat: no-repeat;
position: absolute;
top: 60%;
right: -20px;
width: 180px;
height: 200px;
z-index: 1;
}

.santa-body{
background-image: url("images/santa-body.png");
background-repeat: no-repeat;   
position: absolute;
top: 60%;
right: -20px;
width: 180px;
height: 200px;
z-index: -1;
}

HTML -

<div class="santa-wrapper">
<div class="santa"></div>
<div class="santa-body"></div>
</div>

最佳答案

要让背景图像固定在右下角,您需要将这段代码添加到您的 CSS 中。

background-position: right bottom;

要使 div 向下滚动,我认为您需要包含一些 Javascript。

关于html - 背景图像垂直滚动但粘在容器的一侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33740291/

相关文章:

html - Ebay 模板 - 图像缩略图

html - 如何使用 TailwindCSS 设置每个单元格具有特定宽度和高度的网格样式

image - Matlab图片中的方框和轴坐标

jquery - 清除sideDown jquery的队列

css - 以导航为中心的响应式 Logo

需要 HTML/CSS 堆叠下划线

Android 应用程序——我们可以在运行时重用图像吗?

html - 单击时无法更改字体超棒的图标颜色

html - 基于宽度兄弟图像宽度的 CSS 剪辑字符串

java - Android 图像转 Base64