Current location: Hot Scripts Forums » General Web Coding » JavaScript » make element follow page scroll ?! how-to


make element follow page scroll ?! how-to

Reply
  #1 (permalink)  
Old 03-12-09, 06:10 AM
k-knudsen's Avatar
k-knudsen k-knudsen is offline
Newbie Coder
 
Join Date: Jan 2004
Location: Denmark
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Stop make element follow page scroll ?! how-to

Hi PT Genius's

Did you ever see an effect you just could not remake or find ? well here is one of them!

I'm trying to make a portfolio site where theres alot of scrolling - so to make the site easy to navigate, I use Anchor links for every portefolio-case on the site. to make it even more easy for the user - it's combined with smooth scroll.. now for the difficult part:

The menu needs to scroll with the page - like on the dafont.com (look at the ad)

and on apple's store page store.apple.com/us/configure/MB940LL/A?mco=MTkzOTI1NQ (look at the Summary on the right).

I have only managed to replicate a bad version, this on my page limitededition.dk/test/referencer.html - the menu jumps and is not steady on the page.

does anyone here have suggestions, advice or an tutorial on this? any help would be extremely helpfull
__________________
Personal Homepage: K-Knudsen
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 03-12-09, 07:05 AM
Yeroon's Avatar
Yeroon Yeroon is offline
Code Master
 
Join Date: Aug 2007
Location: Netherlands, Nijmegen
Posts: 850
Thanks: 2
Thanked 20 Times in 20 Posts
Hi There,

Does it need to change place for some header like in the apple store and after stay fixed? If it can be in fixed place all the time the below code will work. It uses a fix for IE which doesnt allow proper fixed positioning:

HTML Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<style type="text/css">
		#fixme { 
			position: absolute; 
			left: 0px; 
			top: 0px; 
		}
		#fixmetoo { 
			position: absolute; 
			right: 0px; 
			bottom: 0px; 
		}
		div > div#fixme { position: fixed; }
		div > div#fixmetoo { position: fixed; }
		
		pre.fixit { 
			overflow:auto;
			border-left:1px dashed #000;
			border-right:1px dashed #000;
			padding-left:2px; }
		</style>
		
		<!--[if gte IE 5.5]><![if lt IE 7]>
			<style type="text/css">
			div#fixme {
				left: expression( ( 0 + ( ignoreMe2 = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ) ) + 'px' );
				top: expression( ( 0 + ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) ) + 'px' );
			}
			div#fixmetoo {
				right: auto; bottom: auto;
				left: expression( ( 0 - fixmetoo.offsetWidth + ( document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth ) + ( ignoreMe2 = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ) ) + 'px' );
				top: expression( ( 0 - fixmetoo.offsetHeight + ( document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight ) + ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) ) + 'px' );
			}
		</style>
		<![endif]><![endif]-->

	</head>
	<body>


<div id="content">
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>


	<div id="fixme">
		fixed div 1
	</div>
	<div id="fixmetoo">
		fixed div 2
	</div>

</div>

</body>
</html>
__________________
Feel free to thank people if they help you by clicking thanks at a post.
=================================
Make it idiot proof and someone will make a better idiot.
=================================
Realise the impotence of proof reading everything you publish
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 03-12-09, 06:04 PM
k-knudsen's Avatar
k-knudsen k-knudsen is offline
Newbie Coder
 
Join Date: Jan 2004
Location: Denmark
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you so much

Thank you so much yeroon!

That's exactly what i've been looking for!
i'm amazed how simple it looks

If it is not to much to ask for - could you help me to do the same as on apple's page ? it would make the page stand out perfectly!

Thank you so much for your advice and help - I really diden't expect anyone to help this is a very helpfull community here

/Kenneth
__________________
Personal Homepage: K-Knudsen
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 03-15-09, 09:48 AM
Yeroon's Avatar
Yeroon Yeroon is offline
Code Master
 
Join Date: Aug 2007
Location: Netherlands, Nijmegen
Posts: 850
Thanks: 2
Thanked 20 Times in 20 Posts
Hi,

So you want the fixed div to start at say 200 px from the top below some other div? And when that other div scrolls out of view the fixed div will scrool to the top and stay there?

Will look into it asap if this is what you need

Yeroon
__________________
Feel free to thank people if they help you by clicking thanks at a post.
=================================
Make it idiot proof and someone will make a better idiot.
=================================
Realise the impotence of proof reading everything you publish
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 03-15-09, 11:38 AM
k-knudsen's Avatar
k-knudsen k-knudsen is offline
Newbie Coder
 
Join Date: Jan 2004
Location: Denmark
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
That is excatly what i need thank you so much!
__________________
Personal Homepage: K-Knudsen
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 03-16-09, 05:40 AM
Yeroon's Avatar
Yeroon Yeroon is offline
Code Master
 
Join Date: Aug 2007
Location: Netherlands, Nijmegen
Posts: 850
Thanks: 2
Thanked 20 Times in 20 Posts
This should do the trick. If you need any explanation just let me know. This sample has a 200px high header div. Fixed div under that. Stays under and scrolls to top on scroll and stays fixed at 0px top when header out of view:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<style type="text/css">
		#fixme { 
			position: absolute; 
			left: 0px; 
			top:200px
		}

		div > div#fixme { position: fixed; }
		
		pre.fixit { 
			overflow:auto;
			border-left:1px dashed #000;
			border-right:1px dashed #000;
			padding-left:2px; }
			
		#header {
			top:200px;
			left:20px;
			height:200px;
		}
		</style>
		
		<!--[if gte IE 5.5]><![if lt IE 7]>
			<style type="text/css">
			div#fixme {
				left: expression( ( 0 + ( ignoreMe2 = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ) ) + 'px' );
				top: expression( ( 0 + ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) ) + 'px' );
			}
			div#fixmetoo {
				right: auto; bottom: auto;
				left: expression( ( 0 - fixmetoo.offsetWidth + ( document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth ) + ( ignoreMe2 = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ) ) + 'px' );
				top: expression( ( 0 - fixmetoo.offsetHeight + ( document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight ) + ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) ) + 'px' );
			}
		</style>
		<![endif]><![endif]-->
	<script type="text/javascript">
		function reposition(){
			var el = document.getElementById('fixme');
			
			var ScrollTop = document.body.scrollTop;
			if (ScrollTop == 0)
			{
				if (window.pageYOffset)
					ScrollTop = window.pageYOffset;
				else
					ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
			}
			if(ScrollTop < 200)
				el.style.top = 200 - ScrollTop + "px";
			else
				el.style.top = "0px";
		}
	</script>
	</head>
	<body onscroll="reposition()">
<div id="header">
	header
</div>

<div id="content">
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>


	<div id="fixme">
		fixed div 1
	</div>


</div>

</body>
</html>
__________________
Feel free to thank people if they help you by clicking thanks at a post.
=================================
Make it idiot proof and someone will make a better idiot.
=================================
Realise the impotence of proof reading everything you publish
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 03-16-09, 11:26 AM
k-knudsen's Avatar
k-knudsen k-knudsen is offline
Newbie Coder
 
Join Date: Jan 2004
Location: Denmark
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Hi again

It works perfectly ! flawlessly even!

here it is in action: http://limitededition.dk/test/referencer.html

by the way - i have seen through the css and Js and i can't seem to fit it into a div ? I want to move it to the right side
__________________
Personal Homepage: K-Knudsen
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 03-17-09, 11:44 AM
Yeroon's Avatar
Yeroon Yeroon is offline
Code Master
 
Join Date: Aug 2007
Location: Netherlands, Nijmegen
Posts: 850
Thanks: 2
Thanked 20 Times in 20 Posts
To make it stick on the right side use right instead of left. Set opx right to 200 if you need it to be 200px from the right side:

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<style type="text/css">
		#fixme { 
			position: absolute; 
			right: 0px; 
			top:200px
		}

		div > div#fixme { position: fixed; }
		
		pre.fixit { 
			overflow:auto;
			border-left:1px dashed #000;
			border-right:1px dashed #000;
			padding-left:2px; }
			
		#header {
			top:200px;
			left:20px;
			height:200px;
		}
		</style>
		
		<!--[if gte IE 5.5]><![if lt IE 7]>
			<style type="text/css">
			div#fixme {
				right: expression( ( 0 + ( ignoreMe2 = document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ) ) + 'px' );
				top: expression( ( 0 + ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) ) + 'px' );
			}
			
		</style>
		<![endif]><![endif]-->
	<script type="text/javascript">
		function reposition(){
			var el = document.getElementById('fixme');
			
			var ScrollTop = document.body.scrollTop;
			if (ScrollTop == 0)
			{
				if (window.pageYOffset)
					ScrollTop = window.pageYOffset;
				else
					ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
			}
			if(ScrollTop < 200)
				el.style.top = 200 - ScrollTop + "px";
			else
				el.style.top = "0px";
		}
	</script>
	</head>
	<body onscroll="reposition()">
<div id="header">
	header
</div>

<div id="content">
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>
<p>long content</p>


	<div id="fixme">
		fixed div 1
	</div>


</div>

</body>
</html>

Quote:
and i can't seem to fit it into a div
I am not sure what you mean with this.
__________________
Feel free to thank people if they help you by clicking thanks at a post.
=================================
Make it idiot proof and someone will make a better idiot.
=================================
Realise the impotence of proof reading everything you publish
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 03-17-09, 04:20 PM
k-knudsen's Avatar
k-knudsen k-knudsen is offline
Newbie Coder
 
Join Date: Jan 2004
Location: Denmark
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
It's Working !

take a look: http://www.limitededition.dk/test/referencer.html

thank you so much ! this would not have been possible without your solution Yeroon!

i'm so happy i could almost cry

Thank you again!

/Kenneth

( oh - i removed the #fixme position and margins it did the trick! )
__________________
Personal Homepage: K-Knudsen
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 03-18-09, 08:07 AM
Yeroon's Avatar
Yeroon Yeroon is offline
Code Master
 
Join Date: Aug 2007
Location: Netherlands, Nijmegen
Posts: 850
Thanks: 2
Thanked 20 Times in 20 Posts
glad you got it working!
__________________
Feel free to thank people if they help you by clicking thanks at a post.
=================================
Make it idiot proof and someone will make a better idiot.
=================================
Realise the impotence of proof reading everything you publish
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
how do i make php open another web page? wonderland PHP 6 09-23-08 07:55 PM
web page browser with hover scroll efx? manifest360 Script Requests 0 02-12-08 05:04 PM
need to make a confirmation page codeguru21 PHP 1 10-02-03 05:18 PM
How to make the index.html page for each categories ? vuzzan PHP 2 07-01-03 06:35 AM


All times are GMT -5. The time now is 09:24 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.