non-academic guidance for computer science

Information

This article was written on 28 Zář 2011, and is filled under Tutoring.

Current post is tagged

, , , , ,

jQuery Sticky Float

 

Teorem

just a template that just modify DOM if JavaScript is enabled ( that mean that the page is still accesbile if CSS or JavaScript are disabled).

Try yourself.

Code

/*
 * TERMS OF USE - Sticky Float
 *
 * Open source under the BSD License.
 *
 * Copyright © 2011 Jan Cajthaml
 * All rights reserved.
 */

 $(document).ready(function(){

///////////////////////////////////////////////////////////////

	$('h1').each(function(index)
	{
	    $(this).wrap("
      <div class=\"wrapper\" id=\"section"+index+"Wrapper\" title=\""+index+"\">
       <div id=\"section"+index+"\"></div>
       </div>
      ");
	});

	$("body").append("<div id=\"section"+$("h1").size()+"Wrapper\"></div>");
	$("body").before("<div id=\"section0Wrapper\"></div>");

	$(window).scroll(function(){

		$("h1").each(function(index)
		{
			if(index>0)
			checkElem(index);

		});

	});

///////////////////////////////////////////////////////////////

function checkElem(id)
{

		var top = $(window).scrollTop();
		var y = 100;

		var cur = $('#section'+(id)+'Wrapper').position().top-y;
		var prev = $('#section'+(id-1)+'Wrapper').position().top-y;
		var el = $('#section'+(id));

		if(top < cur && top > prev+100)
			scroll(el);

		else
			stick(el);

}

///////////////////////////////////////////////////////////////

function scroll(element)
{
	element.addClass("float");
}

///////////////////////////////////////////////////////////////

function stick(element)
{
	element.removeClass("float");
}

///////////////////////////////////////////////////////////////

});

Needed

<link href="css/jQuery.Sticky.css" rel="stylesheet" type="text/css">
<script type='text/javascript' src='js/jquery.js'></script>
<script type="text/javascript" src="js/jQuery.Sticky.js"></script>

Example

<h1>Title 1</h1>
<p>text</p>

<h1>Title 2</h1>
<p>text</p>

<h1>Title 2</h1>
<p>text</p>

Watch Demo or Download zip package.

 

Komentáře: 2

  1. Cibulka.me
    1 Úno ’12

    Thumb up 1 Thumb down 0

    Hello, this sollution is EXACTLY what I was looking for past 6 hours!

    Plugin seems to work (wrappers are appended to each element with class „left“) but for some reason there is no „float“ class appended to the element after scrolling.

    I am novice (or more likely copypaster) of jQuery, so go easy on me. :) I use WordPress with plugins, so I am forced to use jQuery.noconflict method for added scripts such as Sticky Float.

    Basically I want to achieve effect similar to 9gag.com with floating fixed boxes relative to the article containers (see the loop of articles on the left). For this reason any tutorial for just one floating menu won’t help me.

    Any help would be MUCH appreciated. Thank you! Petr

    A jasně, jsem z Čech, pro ostatní uživatele se stejným problémem pro jistotu píšu anglicky. :)

    • jancajthaml
      1 Úno ’12

      Thumb up 0 Thumb down 0

      jQuery Sticky Float je zaevidovaný pod kategorií edit, to znamená že potřebuje další prácí a optimalizaci.
      Ve chvíli kdy to bude optimalizované a odbugované dám vám Petře vědět na mail.
      Jan Cajthaml

Napsat komentář