Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-13-2010, 02:26 PM   PM User | #1
tommyguntom
New to the CF scene

 
Join Date: May 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
tommyguntom is an unknown quantity at this point
javascript issue with google chrome (position:fixed horizontal position:absolute vert

I'm new to javascript and am not sure why this works in firefox and not chrome.
I am trying to create a script that keeps an object fixed horizontally while bing positioned absolute vertically.

if I replace the toPP variable in document.getElementById('fire').style.top = toPP; with say '50px' it will move the element down 50 pxs, but how I have it currently it doesn't do anything in chrome

Code:
<script type="text/javascript" >
	
	
window.onscroll = function() 
{
	if( window.XMLHttpRequest ) { 
		
			
			var x = 0 -document.documentElement.scrollTop;
			var toP = String(x);
			var toPP = toP + "px";
			
			document.getElementById('fire').style.position = 'fixed'; 
			document.getElementById('fire').style.top = toPP;
				
	}
}
	</script>

Last edited by tommyguntom; 05-14-2010 at 12:27 AM..
tommyguntom is offline   Reply With Quote
Old 05-13-2010, 02:43 PM   PM User | #2
xaltednet
New to the CF scene

 
Join Date: May 2010
Posts: 9
Thanks: 0
Thanked 1 Time in 1 Post
xaltednet is an unknown quantity at this point
firefox is smarter and more robust so it changes things for you at times... you might need to get rid of the 'px' in the scrollTop.... not 100% sure about this though

var x = 0 - document.documentElement.scrollTop.replace(/px/gi, "");
xaltednet is offline   Reply With Quote
Old 05-13-2010, 02:44 PM   PM User | #3
xaltednet
New to the CF scene

 
Join Date: May 2010
Posts: 9
Thanks: 0
Thanked 1 Time in 1 Post
xaltednet is an unknown quantity at this point
just make sure you variable in toPP is what you want it to be by echoing

window.alert(toPP);
xaltednet is offline   Reply With Quote
Old 05-14-2010, 12:02 AM   PM User | #4
tommyguntom
New to the CF scene

 
Join Date: May 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
tommyguntom is an unknown quantity at this point
Thanks for your time xaltednet.


Could you please explain what that last part does

.replace(/px/gi, "");

if I add this bit it makes it not work in firefox as well as chrome.


also if I use the window.alert(topp); thing on chrome it only ever comes up with 0px
but comes up with the right numbers on firefox.
tommyguntom is offline   Reply With Quote
Old 05-14-2010, 12:19 AM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Try this:
Code:
window.onscroll = function() 
{
	if( window.XMLHttpRequest ) 
	{ 
		var stop = parseInt(document.documentElement.scrollTop);
		if ( isNaN(stop) || stop == 0 )
		{
			stop = parseInt(document.body.scrollTop);
		}
		if ( ! isNaN(stop) && stop != 0 )
		{
			document.getElementById('fire').style.position = 'fixed'; 
			document.getElementById('fire').style.top = (- stop ) + "px;
		}
	}
}
</script>
Whether or not document.documentElement exists depends on the type of page you are using (HTML, XHTML transitional, XHTML strict) and seems to vary by browser. My hack is to try document.documentElement first and, if it doesn't work, then fall back to document.body.

And that replace() should have removed the "px" from scrollTop if it came back as "135px" (for example), but parseInt will do the same thing, so K.I.S.S.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 05-14-2010, 12:26 AM   PM User | #6
tommyguntom
New to the CF scene

 
Join Date: May 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
tommyguntom is an unknown quantity at this point
it appears that there is a bug in google chrome;

http://code.google.com/p/chromium/issues/detail?id=2891

so this code doesn't work for chrome but works for FF
document.documentElement.scrollTop

and this code doesnt work for FF but works for chrome

document.body.scrollTop

so just did a pick the greatest one and use that


Code:
		var x = 0;
			if(document.documentElement.scrollTop >= 
document.body.scrollTop){ 
			
			 x = 0 - document.documentElement.scrollTop;
			}
			
			else{
				
			x = 0 -	document.body.scrollTop;
			}
			
			
			var toP = String(x);
			var toPP = toP + "px";
			
			document.getElementById('fire').style.position = 'fixed'; 
			document.getElementById('fire').style.top = toPP;
tommyguntom is offline   Reply With Quote
Old 05-16-2010, 04:01 AM   PM User | #7
tommyguntom
New to the CF scene

 
Join Date: May 2010
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
tommyguntom is an unknown quantity at this point
hey didnt see your post old pedant, is there advantage in using your code rather than mine?


Thanks
tommyguntom is offline   Reply With Quote
Old 05-16-2010, 08:08 PM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Well, you are making the assumption that you will indeed get a zero value from whichever one doesn't exist.

But suppose you get null?

Code:
if(document.documentElement.scrollTop >= 
document.body.scrollTop){
would then become
Code:
   if ( null >= '42px' )
or 
   if ( '42px' >= null )
Would that always give the comparison you expect??

Me, I'm paranoid. I play it safe by ensuring that I *do* get a number by using isNaN().
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Tags
chrome, var

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:50 PM.


Advertisement
Log in to turn off these ads.