/ Published in: jQuery
Sometimes you click a link like faq.html#item5 and, after the browser jumps you to the anchor, you have trouble spotting the item you're looking for. anchorAway is a little function that helps you call a function on anchored items, maybe something like a scrollTo or a background color, that helps users easily spot the content you directed them to.
anchorAway parses the window.location for an anchor (#foo), finds it, and calls the function (act(bar)) on it. It also finds any on-page links to the objects you've specified and binds the same function to their click event.
anchorAway parses the window.location for an anchor (#foo), finds it, and calls the function (act(bar)) on it. It also finds any on-page links to the objects you've specified and binds the same function to their click event.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
$.fn.anchorAway = function(act){ urlArr = window.location.href.split("#"); anchorName = urlArr[(urlArr.length - 1)]; $(this).each(function(){ targetId = $(this).attr("id"); bindIt = function(i){ $("a[href='#" + i + "']").each(function(){ $(this).bind('click', function(){ act($("#" + i)); }); }); } bindIt(targetId); if (targetId == anchorName) { act($(this)); } }); } === USAGE === $("div").anchorAway(function(obj){ obj.css("background-color","yellow"); });