问题
I have a CSS layout as in the attached picture.
I'd like to achieve the following behaviour
- When part of the header is visible the positions (when scrolling) are as in the picture.
- 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
回答1:
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>
回答2:
http://jsfiddle.net/juSvJ/ that should help.
Unless one's screen is really small, it should work no matter.
来源:https://stackoverflow.com/questions/6138007/difficult-css-positioning-problem