在Drupal 7中调用Ajax函数后,JQuery OnScroll无法正常工作

时间:2019-06-14 15:04:06

标签: jquery drupal-7 drupal-behaviors

因此,我有一个功能可以为多年的帖子添加索引,并且我还有一个辅助功能可以设置,该功能可以根据滚动位置应用/删除类。我通过Drupal行为添加了索引,但是在文档中找不到有关通过行为创建onscroll函数的任何信息。如何使滚动事件功能像Drupal行为一样,并在每次页面重新加载时运行?

添加索引的函数是

Drupal.behaviors.timelineAddIndex = {
  attach:function (context, settings){
    var previous = "";
    $("div.view-content").prepend("<div id='postIndex'></div>");
    $("ul li").each(function() {
        var current = $(this).find('span.year');
        current = current.text();
        if (current != previous) {
            $(this).attr("id", "first_year_" + current);
            previous = current;
            $("#postIndex").append("<a href='#first_year_" + current + "'>" + current + "</a><br/>");
        }
    });
  }
};

当前用于滚动事件的功能是

$(window).on('scroll', function() {
    if ($(window).scrollTop() < 135) {
        $('#postIndex').addClass('hidden');
    }
    else {
        $('#postIndex').removeClass('hidden');
    }
});

0 个答案:

没有答案