Code JavaScript:
var fromTop = 200;
 
	scrolled();
	$(window).scroll( function () {
	  	scrolled();
	});
 
	function scrolled () {
		if ( ( $(window).scrollTop() - $('#test').offset().top ) > fromTop ) {
	  		$('#test').addClass("fixed");
	  	} else {
	  		$('#test').removeClass("fixed");
	  	}
	}

I'm trying to add a class to #test when #test less than 200px from top of viewport. Otherwise, I'd like the class removed.

I'm not too far, but a) I get a pretty bad flickr (the "fixed" class seems to be added and removed like crazy) and b) I'm not sure that fromTop is really taken into account (but it's hard to say because of a) )

Does anyone know how to fix the flickr problem and improve this code?