문제

i'am using jquery waypoints (http://imakewebthings.com/jquery-waypoints/#docs) to check if an element is the view of the browser, here the html:

<div class="container" id="container_1">1. Container</div>
<div class="container" id="container_2">2. Container</div>
<div class="container" id="container_3">3. Container</div>
<div class="container" id="container_4">4. Container</div>

here the js

$('#container_1').waypoint(function() {
     console.log("container 1 is visible");
});

this works fine!

But is it possible to find out the current id of the element which is in view and the waypoint get fired? something like this:

$('.container').waypoint(function() {
         console.log("id of element: " + $(this).attr('id');
    });

thanks!

도움이 되었습니까?

해결책 2

You are missing parenthesis: ) at the end of console.log otherwise it would work.

$('.container').waypoint(function() {
         console.log("id of element: " + $(this).attr('id'));
    });

If you are confusing, you can use like this too:

$('.container').waypoint(function() {
    var $this = $('.container');         
    console.log("id of element: " + $this.attr('id'));
});

다른 팁

In waypoints.js I've found that this refers to a waypoints internal object. If you console.log it though, you easily find how to select that element with jquery.

handler: function (direction){
    var DOMElement = $(this.element);
    console.log($(this.element).attr('data-id');
}

Also... element attribute ID return undefined

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top