JavaScript Scroll current view's element id

JenonD

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>

Mohamed-Yousef

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JavaScript Scroll current view's element id

From Dev

Javascript scroll to specific element by ID

From Dev

Add element before scroll position, and not affect current scroll / view

From Dev

JavaScript - Scroll overflowed list element into view

From Dev

JavaScript - Scroll down to element

From Dev

Scroll to a element onClick JavaScript

From Dev

selenium scroll element into (center of) view

From Dev

Scroll to element and position it in the middle of view

From Dev

Kendo UI Grid get id of current element javascript

From Dev

How to find current div element's id on scrolling container

From Dev

Javascript scroll to div id

From Dev

Javascript not working on some element by id's

From Dev

jQuery scroll to bottom of ID element

From Dev

AngularJS / JQuery: scroll from current location to element

From Dev

How to get element's id of another view SAPUI5

From Dev

UICollectionView scroll outside it's view

From Dev

Javascript touchmove event on scroll element

From Dev

View doesn't scroll to selected element QuickDialog

From Dev

Scroll to element on page using the elements id, Meteor

From Dev

jquery smooth variable scroll to element id

From Dev

View: Exclude nodes that have the same taxonomy term id(s) as current node

From Dev

Changing a specific element's CSS without an ID in Javascript

From Dev

Rendering view for an element in rails by :id

From Dev

Make current post link's ID "current"

From Dev

GDB TUI: Scroll assembly view above current instruction?

From Dev

How to scroll to top (or anchor) from current position on view change with AngularJS?

From Dev

Android View Id's

From Dev

Access element by ID in javascript

From Dev

Call element by id in javascript

Related Related

HotTag

Archive