如何使用Django无限滚动到特定元素?

问题描述 投票:0回答:1

当我最初加载页面时,它加载了10个项目(1,2,3 ... 10),向下滚动后,我又得到了10个项目(11,12,13,... 20)。但是我需要直接滚动到第12个元素。我正在使用Waypoint无限滚动(JQuery)和Django分页。

Jquery:

  $(document).ready(function() {
      $('html, body').animate({
     scrollTop: $("#2").offset().top
     }, 'slow');
  });

//航点代码:

  var infinite = new Waypoint.Infinite({
  element: $('.infinite-container')[0],
  context: document.getElementById('#16'),
  onBeforePageLoad: function() {
  $('.loading').show();
   },
  onAfterPageLoad: function($items) {
  $('.loading').hide();
  }
  });

同时通过触发调用向下滚动来自Django分页的数据时:

http://localhost:8000/polls/?page=2&type=testpoll

最初加载页面时,我需要滚动到'page = 2'中的元素。我该怎么办?

jquery django infinite-scroll jquery-waypoints django-pagination
1个回答
0
投票

我曾尝试使用SetInterval滚动。但是,如果找不到第16个元素,它将滚动到页面底部。

if($("#16").length === 0){
    myVar =  setInterval(function(){ 
    $("html, body").animate({ scrollTop: 
    $(document).height()-$(window).height()});
    if($("#16").length !== 0){
        clearInterval(myVar);
        next = $(".infinite-item").parent().next().find($('#16'));
        $('html, body').animate({scrollTop: $('#16').offset().top}, 'slow');
      }
    }, 1000);

  }
});
© www.soinside.com 2019 - 2024. All rights reserved.