...

View Full Version : fixed within div, scrollable with page



rpjd
04-08-2011, 07:00 PM
Can an element such as a button have a fixed position within a scrollable div (although not necessarily visually positioned within the div itself), yet be scrollable with the main page?

Excavator
04-08-2011, 07:44 PM
Hello rpjd,
You can't do that with CSS only. The fixed position is relative to the body of the document only, relatively positioning the containing scrolling element does not have the same affect on fixed positioning as it does absolute positioning.

See here for a description of the positioning property values (http://www.w3schools.com/Css/pr_class_position.asp).

An example that shows what I mean -
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
html, body {
margin: 0;
background: #FC6;
}
#container {
width: 800px;
margin: 30px auto;
background: #999;
overflow: auto;
}
#scroll {
height: 400px;
width: 400px;
margin: 100px auto;
padding: 15px;
background: #fff;
overflow-y: scroll;
position: relative;
}
#button {
height: 50px;
width: 75px;
background: #f00;
position: fixed;
top: 25px;
left: 25px;
}
</style>
</head>
<body>
<div id="container">
<div id="scroll">
<div id="button"></div>
<p>
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna
<br /><br />
aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no
<br /><br />
sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
<br /><br />
nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo
<br /><br />
duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
</p>
<p>
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna
<br /><br />
aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no
<br /><br />
sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam
<br /><br />
nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo
<br /><br />
duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.
</p>
<!--end scroll--></div>
<!--end container--></div>
</body>
</html>

Edit// See these jQuery solutions:

one (http://www.bennadel.com/blog/1810-Creating-A-Sometimes-Fixed-Position-Element-With-jQuery.htm)
two (http://www.vertstudios.com/blog/jquery-sticky-scroller-position-fixed-plugin/)

rpjd
04-08-2011, 08:35 PM
I haven't got a clue about JQuery. Can this be accomplished using javascript at all?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum