jQuery INVIEW 无法使用由另一个函数添加的 class

jQuery INVIEW is not working with a class added by another function

在第一个代码中,如果您单击 LIKE,它将向活动部分添加一个 class .loved.section .active。 (.active class 由 fullpage 插件自动添加到可见部分)。

而且在这里你可以看到INVIEW插件不起作用。

$('#fullpage').fullpage({});

$(document).on("click", ".like", function() {
  $('.section.active').addClass("loved")
})

$('.loved').on('inview', function(event, isInView) {
  if (isInView) {
    console.log("IN VIEW")
  } else {
    console.log("GONE")
  }
});
body {
  margin: 0;
  padding: 0;
  font-size: 10vw;
  line-height: 1.2em;
}

.section {
  text-align: center;
  text-transform: uppercase;
  background: grey;
  color: #141414;
}

.like {
  color: red;
  position: fixed;
  cursor: pointer;
  top: .3em;
  right: .3em
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/alvarotrigo/fullPage.js/master/jquery.fullPage.min.js"></script>
<script src="https://rawgit.com/protonet/jquery.inview/master/jquery.inview.min.js"></script>
<div id="fullpage">
  <div class="section">One</div>
  <div class="section">Two</div>
  <div class="section">Three</div>
  <div class="section">Four</div>
</div>
<div class=like>LIKE</div>

在第二个函数中,如果我将 class .loved 添加到该部分,则 INVIEW 可以顺利运行。
有什么想法吗?

$('#fullpage').fullpage({});

$('.loved').on('inview', function(event, isInView) {
  if (isInView) {
    console.log("IN VIEW")
  } else {
    console.log("GONE")
  }
});
body {
  margin: 0;
  padding: 0;
  font-size: 10vw;
  line-height: 1.2em;
}

.section {
  text-align: center;
  text-transform: uppercase;
  background: grey;
  color: #141414;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/alvarotrigo/fullPage.js/master/jquery.fullPage.min.js"></script>
<script src="https://rawgit.com/protonet/jquery.inview/master/jquery.inview.min.js"></script>
<div id="fullpage">
  <div class="section loved">One</div>
  <div class="section">Two</div>
  <div class="section">Three</div>
  <div class="section">Four</div>
</div>

您的问题在这一行:

$('.loved').on('inview', function(event, isInView) {

因为在第一个片段中您添加了 class "in future" 您需要将事件委托给文档:

$(document).on('inview', '.loved', function(event, isInView) {

此外,在创建代码段时,如果您需要使用 github 库,则需要从以下位置进行转换:

https://raw.githubusercontent.com/....

至:

https://rawgit.com/....

片段:

$('#fullpage').fullpage({});

$(document).on("click", ".like", function() {
    $('.section.active').addClass("loved")
})

$(document).on('inview', '.loved', function(event, isInView) {
    if (isInView) {
        console.log("IN VIEW")
    } else {
        console.log("GONE")
    }
});
body {
  margin: 0;
  padding: 0;
  font-size: 10vw;
  line-height: 1.2em;
}

.section {
  text-align: center;
  text-transform: uppercase;
  background: grey;
  color: #141414;
}

.like {
  color: red;
  position: fixed;
  cursor: pointer;
  top: .3em;
  right: .3em
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://rawgit.com/alvarotrigo/fullPage.js/master/jquery.fullPage.min.js"></script>
<script src="https://rawgit.com/protonet/jquery.inview/master/jquery.inview.min.js"></script>


<div id="fullpage">
    <div class="section">One</div>
    <div class="section">Two</div>
    <div class="section">Three</div>
    <div class="section">Four</div>
</div>
<div class=like>LIKE</div>