javascript - Making 'scroll-sneak' work -
i've been trying make javascript code 'scroll-sneak' (http://mrcoles.com/blog/scroll-sneak-maintain-position-between-page-loads/) work few weeks now. code stops 'page jump' (to top) when anchor link clicked, , without disabling functionality of link. i'd have page not move top when navigation link in table row below clicked. works on developer's demo page, none documented. takers tacking this?
<tr id="tabs"> <th><a href="index.htm">information</a></th> <th><a href="plan.htm">research</a></th> <th><a href="councils.htm">sources</a></th> <th><a href="university.htm">institution</a></th> <th><a href="contact.htm">contact</a></th> </tr> <script> (function() { var sneaky = new scrollsneak(location.hostname), tabs = document.getelementbyid('tabs').getelementsbytagname('th'), = 0, len = tabs.length; (; < len; i++) { tabs[i].onclick = sneaky.sneak; } document.getelementbyid('next').onclick = sneaky.sneak; })(); </script>
update
in terms of original question (and given various problems , bugs described in comments below, accepted answer proving unpredictable in behaviour), managed figure out simple solution, below, works in ie6, ff3, qz6 , webkit 537.21.
(function() { var sneaky = new scrollsneak(location.hostname); document.getelementbyid('tabs').onclick = sneaky.sneak; })();
one more edit:
if last image still giving trouble, can make change , see if helps:
replace this:
$(active).show(); $(active).siblings().hide();
with this:
$("#gallery-interior li").hide(0, function(){ $("#gallery-interior " + active).show(); });
previous
here, combined script other answer , scroll-sneak tabs. tested both in ff3 , verified work properly, complete javascript:
var sneaky = new scrollsneak("scrolltrack"); $(document).ready(function() { var urlhash = window.location.hash; if(urlhash) { $(".thumbs a[href='" + urlhash + "'] img").addclass("dimmed"); } else { $(".thumbs a:first-child img").addclass("dimmed"); } $("#tabs th a").on("click", function(e) { sneaky.sneak(); }); $(".thumbs a").on("click", function(e) { changeimage($(this).attr("href"), e); }); $("#gallery-interior area").on("click", function(e) { changeimage($(this).attr("href"), e); }); }); function changeimage(active, e) { var e = window.event || e; preventnav(active, e); $(".thumbs img").removeclass("dimmed"); $(".thumbs a[href='" + active + "'] img").addclass("dimmed"); $(active).show(); $(active).siblings().hide(); } function preventnav(hash, e) { if (e) { if (typeof e.preventdefault != 'undefined') { e.preventdefault(); } else { e.returnvalue = false; } } var node = $(hash); node.attr("id", ""); document.location.hash = hash; node.attr("id", hash.replace("#", "")); }
previous
here go, script adapted tabs. in solution posted gallery (click anchor link , keep page position / prevent page-jump top?), page jump happens because hash links don't reload page , scrolling happens default , has undone. won't have same jumping effect normal links, left ability capture hash links in case need it. best if placed @ bottom of html of site, before closing body tag.
var sneaky = new scrollsneak("tabs", false); var capture = true; $(document).ready(function() { var urlhash = window.location.hash; $("#tabs th a").on("click", function(e) { sneaky.sneak(); capture = true; }); capture = false; }); window.onscroll = function () { if (capture) sneaky.scroll(), capture = false; // part needed hash links };
Comments
Post a Comment