I have a html-document with a main navigation and a subnavigation consists of anchors. What I'm trying is to toggle a class to the anchor (subnavigation) while scrolling through the document.
With JQuery i tried the following:
$(function() {
$(window).scroll(function () {
//define position where i want to know the class
var elem = document.elementFromPoint(400, 300) // x, y
//read the class at position
el = $(elem).attr('class');
//get substring of last class in element
//last classes are class_1, class_2, class_3, aso.
subclass = el.substr(el.length-1); //throws an error "el is not defined"
//toggle class
$('a.class_'+subclass).toggleClass('additionalClass');
});
It works more or less. Here are my two questions:
- why does it throw an error "el is not defined"
- the result "flickers" because the there are many subsequent elements with the same class, e.g. "class_1". Can the actual value, e.g. "1" be stored as long there is not an other one is on the position where I read the classes?
Thanks for your help.
Fabian