2

Apologies if this question is already asked/answered. I have an html page (this page has JQuery library) with lots of paragraph tags. Each paragraph (p) tag is associated with an anchor tag with a name. Please note content inside these paragraphs may vary. When user scrolls through the page, how can I get the name of the anchor tag in the current view?

<p><a name="para1"></a> some long text </p> 
<p><a name="para2"></a> some text </p> 
<p><a name="para3"></a> some long text </p>

Keval Bhatt
  • 6,224
  • 2
  • 24
  • 40
RasikaSam
  • 5,363
  • 6
  • 28
  • 36
  • Possible duplicate of [How to get on-screen visible element objects in jQuery?](http://stackoverflow.com/questions/19498068/how-to-get-on-screen-visible-element-objects-in-jquery) – Alex Nov 17 '15 at 04:43
  • You can use `scrollIntoView` or `scroll` event here. – Domain Nov 17 '15 at 04:47
  • @alireza safian, yes it looks like a duplicate. The answer by Mohamed-Yousef is a different way of solving the problem. So I will keep this question. – RasikaSam Nov 17 '15 at 06:00

2 Answers2

4

you can use

$(document).ready(function(){
    $(window).on('scroll',function(){
        var Wscroll = $(this).scrollTop();
        $('a[name^="para"]').each(function(){
            var ThisOffset = $(this).closest('p').offset();
            if(Wscroll > ThisOffset.top &&  Wscroll < ThisOffset.top  + $(this).closest('p').outerHeight(true)){
                $(this).closest('p').css('background','red');
                console.log($(this).attr('id')); // will return undefined if this anchor not has an Id
                // to get parent <p> id
                console.log($(this).closest('p').attr('id')); // will return the parent <p> id
            }
        });
    });
});

Demo

Note: don't forget to include Jquery

To Explain : $(this) inside .each() select anchors with name starts with para .. closest('p') to select parent <p> of this anchor .. so play around this to reach the thing you want

Mohamed-Yousef
  • 23,946
  • 3
  • 19
  • 28
  • apparently what i am having is anchor tags inside paragraphs (

    some text

    ). I tried to use your answer on this but couldnt get it to a shape. Could you please shed some light on that

    – RasikaSam Nov 17 '15 at 05:26
  • @JenonD finally? :) .. glad it helped .. you welcome and very good luck to you :) – Mohamed-Yousef Nov 17 '15 at 06:00
  • yeah... I am actually working on a community project on my free time, and this html is loaded inside a windows form using cef winforms and it is working :-) – RasikaSam Nov 17 '15 at 06:02
  • @JenonD Good Job .. community projects needs a lot of time and a lot of coding ... I hope you to finish it soon ..and if you need anything I'm here to help .. Good Luck :) .. – Mohamed-Yousef Nov 17 '15 at 06:08
0

If anybody interest of a out the box solution, take a look at https://www.customd.com/articles/13/checking-if-an-element-is-visible-on-screen-using-jquery as well. Works well when you mix it with the Mohamed-Yousef answer.

  • Add jquery reference
  • Add jquery.visible.js reference
$(function(){
 $(window).on('scroll',function(){
  $('a[name^="para"]').each(function(){               
      var visible = $(this).visible( false );
      if(visible){
      console.log($(this).attr('name'));
      return false;
          }       
      });
  }); 
});
RasikaSam
  • 5,363
  • 6
  • 28
  • 36