困难的CSS定位问题

发布于 2024-11-10 00:27:39 字数 618 浏览 4 评论 0原文

我有一个 CSS 布局,如附图所示。 在此处输入图像描述
我想实现以下行为

  1. 当标题的一部分可见时,位置(滚动时)如图所示。
  2. 当 header 不可见时(我们向下滚动超过 header 长度), left、right 和 img 的位置应该是固定的,唯一可滚动的部分应该是页面内容。

到目前为止 fiddle 链接

Liam 建议 链接,但 javascript 有错误 Uncaught TypeError: Property '$' of object [object DOMWindow]不是一个函数。也许它与mootools有关(我没有使用它)。如果没有 mootol,这个功能是否可行?

你能帮我定位这个风格吗?

谢谢

I have a CSS layout as in the attached picture.
enter image description here
I'd like to achieve the following behaviour

  1. When part of the header is visible the positions (when scrolling) are as in the picture.
  2. When header is invisible (we're scrolling down more then header length), the positions of left, right and img should be fixed and the only scrollable part should be page content.

So far fiddle link

Liam suggested this link but javascript has error Uncaught TypeError: Property '$' of object [object DOMWindow] is not a function . Maybe it's related to the mootools (I'm not using it). Is this functionality possible without mootols?

Could you help me with positioning style of this?

thank you

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

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

发布评论

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

评论(2

日记撕了你也走了 2024-11-17 00:27:39

测试过这个,效果很好

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript"
  src="https://ajax.googleapis.com/ajax/libs/mootools/1.3.2/mootools-yui-compressed.js">
</script>
<script type="text/javascript">
//<![CDATA[
window.onscroll = function()
{
    if( window.XMLHttpRequest ) {
        if (document.documentElement.scrollTop > 221 || self.pageYOffset > 221) {
            $('rightsidebar').style.position = 'fixed';
            $('rightsidebar').style.top = '0';
            $('leftsidebar').style.position = 'fixed';
            $('leftsidebar').style.top = '0';
        } else if (document.documentElement.scrollTop < 221 || self.pageYOffset < 221) {
            $('rightsidebar').style.position = 'absolute';
            $('rightsidebar').style.top = '221px';
            $('leftsidebar').style.position = 'absolute';
            $('leftsidebar').style.top = '221px';
        }
    }
}
//]]>
</script>
<style type="text/css">
/*<![CDATA[*/
body {margin:0;
}
#header {background:blue; height:221px;}
#rightsidebar {
        position:absolute;
        right: 0;
        top: 221px;
        width: 150px;
        color: #FFFFFF;
        background:red;
}

#leftsidebar {
        position:absolute;
        left: 0;
        top: 221px;
        width: 150px;
        color: #FFFFFF;
}
#topleft {background:green;}
#image {background:red;}
#footer {height:100px; background:yellow;}
/*]]>*/
</style>
<title></title>
</head>
<body>
<div id="header">header</div>
<div id="leftsidebar">
<div id="topleft">lkjlk</div>
<div id="image">IMAGE</div>
</div>
<div id="content"><br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br /></div>
<div id="rightsidebar">lkjlk</div>
<div id="footer">Footer</div>
</body>
</html>

Tested this, works fine

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
  "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript"
  src="https://ajax.googleapis.com/ajax/libs/mootools/1.3.2/mootools-yui-compressed.js">
</script>
<script type="text/javascript">
//<![CDATA[
window.onscroll = function()
{
    if( window.XMLHttpRequest ) {
        if (document.documentElement.scrollTop > 221 || self.pageYOffset > 221) {
            $('rightsidebar').style.position = 'fixed';
            $('rightsidebar').style.top = '0';
            $('leftsidebar').style.position = 'fixed';
            $('leftsidebar').style.top = '0';
        } else if (document.documentElement.scrollTop < 221 || self.pageYOffset < 221) {
            $('rightsidebar').style.position = 'absolute';
            $('rightsidebar').style.top = '221px';
            $('leftsidebar').style.position = 'absolute';
            $('leftsidebar').style.top = '221px';
        }
    }
}
//]]>
</script>
<style type="text/css">
/*<![CDATA[*/
body {margin:0;
}
#header {background:blue; height:221px;}
#rightsidebar {
        position:absolute;
        right: 0;
        top: 221px;
        width: 150px;
        color: #FFFFFF;
        background:red;
}

#leftsidebar {
        position:absolute;
        left: 0;
        top: 221px;
        width: 150px;
        color: #FFFFFF;
}
#topleft {background:green;}
#image {background:red;}
#footer {height:100px; background:yellow;}
/*]]>*/
</style>
<title></title>
</head>
<body>
<div id="header">header</div>
<div id="leftsidebar">
<div id="topleft">lkjlk</div>
<div id="image">IMAGE</div>
</div>
<div id="content"><br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br /></div>
<div id="rightsidebar">lkjlk</div>
<div id="footer">Footer</div>
</body>
</html>
星軌x 2024-11-17 00:27:39

http://jsfiddle.net/juSvJ/
这应该有帮助。

除非屏幕真的很小,否则无论如何它都应该可以工作。

http://jsfiddle.net/juSvJ/
that should help.

Unless one's screen is really small, it should work no matter.

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