如何让div浮动并随页面中心滚动?

发布于 2024-08-31 04:25:25 字数 240 浏览 2 评论 0原文

因此,我致力于开发 Facebook FBML 应用程序。我想要的很简单。只需在页面中央显示一个 div,然后随页面滚动即可。即始终位于中心。

用普通的 JS 就很容易了。我只是使用 pageYOffset

但是,在使用 FBJS 的 Facebook 中,我不确定应该使用什么。它没有 getPageYOffset().. 我尝试了 getScrollTop().. 这似乎不是正确的事情。

那么,有人知道怎么做吗?

So, I work on a Facebook FBML App. What I want is simple. Just have a div shows in the center of the page, and scroll with the page. i.e. always in the center.

It would be easy with normal JS. I just use the pageYOffset

However, in Facebook using FBJS, I am not sure what I should use. It doesn't have getPageYOffset().. and I tried getScrollTop().. it doesn't seem the right thing.

So, anyone knows how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请你别敷衍 2024-09-07 04:25:25

你以前在 FB 上见过类似的事情吗?在我看来,FBJS 因缺乏 API 选项而给您带来了问题。

在普通 JS 中实现这一点的一种方法是获取滑块的位置(右侧和底部)以及整个屏幕尺寸,然后根据全帧尺寸和滑块偏移确定可视窗口的中心在哪里。

所有这些变量都应该被定义或者可以通过 javascript 检索,以便您抓取和操作,然后只需更新 div 上的 css 值即可将窗口移动到正确的位置。然后,您可以轮询窗口滑块的状态或监视其事件并在更新时检索其位置以重新计算窗口的中心。

如果 FBJS 无法确定窗口大小或滑块位置,那么您将遇到更困难的问题。

Have you ever seen something like this done on FB before? It sounds to me like FBJS is causing you the problem with its lack of API options.

One way to do it in normal JS is to get the position of the sliders, both Right and bottom, and also the whole screen size, and then determine where the center of the viewable window is based on the full frame size and the slider offsets.

All of those variables should be defined or be retrievable via javascript for you to grab and manipulate, and then it is simply updating the css values on a div to move the window to the correct location. you can then poll the state of the window sliders or monitor their event and retrieve their position as it is updated to recalculate the center of the window.

If FBJS has no facility to determine window size or slider position, then you have a much harder problem on your hands.

╰沐子 2024-09-07 04:25:25

使用 css

#myDiv {
    /* Keep position fixed (scroll invariant) */
    position: fixed;

    /* Center */
    left: 50%;
    top: 50%;

    /* Set width and margin to account for half of width */
    width: 200px;
    margin-left: -100px;

    /* Set height and margin to account for half of height */
    height: 100px;
    margin-top: -50px;
}

前 3 行将使页面左上角居中。
接下来的 4 行设置宽度,然后向后和向上移动 div 以使中心居中。

来自 w3Schools 的有关职位的更多信息。

Use css

#myDiv {
    /* Keep position fixed (scroll invariant) */
    position: fixed;

    /* Center */
    left: 50%;
    top: 50%;

    /* Set width and margin to account for half of width */
    width: 200px;
    margin-left: -100px;

    /* Set height and margin to account for half of height */
    height: 100px;
    margin-top: -50px;
}

The first 3 lines would center the upper left corner on the page.
The next 4 lines set the width, and then shift the div back and up so that the center is centered.

More info from w3Schools about position.

眉目亦如画i 2024-09-07 04:25:25

使用 Facebook 的调用 myDiv.getAbsoluteTop() 怎么样?这不会使对象保持固定,但它至少会从正确的位置开始。

还有另一个想法——你可以生成一张图像,并将该图像设置为背景,我认为你可以保持固定。但这取决于你到底想要什么。

How about using Facebook's call myDiv.getAbsoluteTop()? This won't keep the object fixed, but it will at least start at the proper position.

Just one other idea -- you could generate an image, and set the image as the background, which I think you can keep fixed. It depends what you're after exactly though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文