简体   繁体   English

内容固定时背景滚动

[英]Background scrolls while content is fixed

I am making a website using vue.js and Bootstrap I have made use of bootstrap's jumbotron component at the top of my web page right below my navigation and I would like to know how to make some of the elements scrollable while keeping some elements stationary.我正在使用 vue.js 和 Bootstrap 创建一个网站,我在导航下方的 web 页面顶部使用了 bootstrap 的jumbotron组件,我想知道如何使一些元素可滚动,同时保持一些元素静止。 I will include an example as I am aware this is still confusing at this point.我将举一个例子,因为我知道这在这一点上仍然令人困惑。

Demo演示

This is a demo of just the jumbotron there will be other content on the page beside the jumbotron what I want is the Buick logo (I will have other logos as well) to scroll for a short time in the background behind the other content until the main scroll action of the page takes over. 这是jumbotron的演示,在jumbotron旁边的页面上会有其他内容我想要的是别克标志(我也会有其他标志)在其他内容后面的背景中滚动一小段时间,直到页面的主要滚动动作接管。 I hope what I am looking for is clear now.我希望我现在正在寻找什么是清楚的。 If you need more info I will be happy to provide it.如果您需要更多信息,我将很乐意提供。

这是为了帮助证明我的问题

这是另一张图片

Just to be clear in these pictures just show the jumbotron not the whole page.只是为了清楚地在这些图片中显示 jumbotron 而不是整个页面。 Also fixed position is not a solution to my problem as I want the stationary elements to remain in the jumbotron.还修复了 position 不是我的问题的解决方案,因为我希望固定元素保留在 jumbotron 中。

If I get the problem right, this should do the trick: https://codesandbox.io/embed/serene-rhodes-42224?fontsize=14&hidenavigation=1&theme=dark .如果我的问题正确,这应该可以解决问题: https://codesandbox.io/embed/serene-rhodes-42224?fontsize=14&hidenavigation=1&theme=dark

Briefly what was done:简要说明做了什么:

  1. Make text and logo wrappers direct children of scrolling container.使文本和徽标包装器直接成为滚动容器的子级。 In your case it was.jumbotron, which had only one direct child - native jumbotron.container.在您的情况下,它是.jumbotron,它只有一个直系子 - 本地 jumbotron.container。 So, instead I set the scrolling behavior on.wrapper div instead of.jumbotron.因此,我将滚动行为设置在 .wrapper div 而不是 .jumbotron 上。
  2. Set position sticky on text wrapper.在文本包装上设置 position 粘性。 This will give us the desired behavior: sticks to the top (or any set border) of parent, and scrolls with it when main window scrolls.这将为我们提供所需的行为:坚持父项的顶部(或任何设置的边框),并在主 window 滚动时随之滚动。
  3. Set a height for logo wrapper with position absolute, which basically controls how long the user will have to scroll the jumbotron before the logo appears.使用 position absolute 设置徽标包装的高度,它基本上控制用户在徽标出现之前必须滚动 jumbotron 的时间。

So from what I understand you want to make some elements scrollable whilst others are in fixed positions.因此,据我了解,您希望使某些元素可滚动,而其他元素则处于固定位置。 this can be done using css positioning and overflow.这可以使用 css 定位和溢出来完成。 If you have an element that you want to be unscrollable unless there is enough content in that div to make it scrollable use the following css如果您有一个想要不可滚动的元素,除非该 div 中有足够的内容使其可滚动,否则请使用以下 css

 position: fixed; overflow-y:scroll;

However if you want the element to remain unscrollable (you cant scroll on it no matter what) just use the following css:但是,如果您希望元素保持不可滚动(无论如何都无法滚动),只需使用以下 css:

 overflow: hidden;

You need to use background-attachment property of CSS.您需要使用 CSS 的background-attachment属性。

HTML: HTML:

<div class="fixed">
--- Your Content Here ---
</div>

CSS: CSS:

.fixed {
  background: url('https://cdn.pixabay.com/photo/2016/06/11/23/22/soap-bubbles-1451092_960_720.jpg');
  background-attachment: fixed;
height: 400px;
  width: 50%;
  max-width: 600px;
  margin: 32px auto;
}

Check CodePen here 在此处检查 CodePen

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM