Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8953493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:06:26+00:00 2026-06-15T14:06:26+00:00

I am trying to do a reverse infinite scroll. I have a comment list

  • 0

I am trying to do a reverse infinite scroll. I have a comment list where I receive the last 10 most recent comments and want the user to be able to scroll up to retrieve the next 10 – similar to FB where it shows the most recent comments with a ‘get previous’ link, but via scroll event rather than a link.

I started with http://jsfiddle.net/vojtajina/U7Bz9/ and tried to modify it to a reverse infinite scroll and pretty quickly ended up with something like this:

  function Main($scope, $timeout) {
    $scope.items = [];

    var counter = 0;
    $scope.loadMore = function() {
      // simulate an ajax request
      $timeout( function() {
        for (var i = 0; i < 5; i++) {
          $scope.items.unshift({id: counter});
          counter += 10;
        }}, 1000);
    };

    $scope.loadMore();
  }

  angular.module('scroll', []).directive('whenScrolled', ['$timeout', function($timeout) {
    return function(scope, elm, attr) {
      var raw = elm[0];

      $timeout(function() {
        raw.scrollTop = raw.scrollHeight;
      }, 1100);

      elm.bind('scroll', function() {
        // note: if test for < 100 get into infinite loop due to 
        // the delayed render
        if (raw.scrollTop === 0) {
          var sh = raw.scrollHeight
          scope.$apply(attr.whenScrolled);
          // the items are not loaded and rendered yet, so
          // this math doesn't work
          raw.scrollTop = raw.scrollHeight - sh;
        }
      });
    };
  }]);
  ​

http://jsfiddle.net/digger69/FwWqb/2/

The issue is that when the next 10 items are retrieved, they are added to the top of the list and the entire list re-renders, and the item that was at the of the list is completely scrolled out of view. In the fiddle item “40” is at the top and when you scroll (down slightly) and then up to trigger the scrolled, item “90” is at the top. I’m looking for a good strategy to keep “40” at the top of the scroll area after it has rendered.

Note: In the fiddle I was able to get it to work by saving off the top li in the scroll event and calling scrollIntoView() until I added the timeout to simulate the ajax call. With the timeout the top li is scrolled into view before the request has come back and the new elements are rendered :/

var top = elm.find("li")[0];
scope.$apply(attr.whenScrolled);
top.scrollIntoView();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:06:27+00:00Added an answer on June 15, 2026 at 2:06 pm

    You can try something like this: http://jsfiddle.net/mNFmf/4/

    This will scroll to the bottom of the div:

    $timeout(function() {
        raw.scrollTop = raw.scrollHeight;          
    });    
    

    And this will keep the div from scrolling up to the first item on the list:

    var sh = raw.scrollHeight
    scope.$apply(attr.whenScrolled);
    raw.scrollTop = raw.scrollHeight - sh;
    

    Update

    To overcome the ajax request problem, try to use promises.

    http://jsfiddle.net/mNFmf/8/

    The loader would look something like this:

    $scope.loadMore = function() {
      var deferred = $q.defer();
    
      // do ajax request here and after getting the result call:   
      deferred.resolve();
    
      return deferred.promise;
    };
    

    And on the other side:

    loadMore.then(function() { 
      /* do whatever you want after the ajax request has been fulfilled */ 
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to reverse a Mach-O executable arm file and I'm able
I am trying to reverse a linked list. This is the code I have
Im trying to reverse the order of the following linked list, I've done so,
I'm trying to reverse items in a dictionary in C#. I have tried: Dictionary<double,
I am trying to reverse a doubly linked list using iterative approach and i'm
Possible Duplicate: Unable to reverse a linked list I'm trying to reverse a linked
I'm trying to reverse a linked list by using C++ and then print out
I have some code and I'm trying to do my For loop in reverse
I was trying to reverse a linked list, however whenever I execute the following
I am trying to reverse dns a list of IPs using socket.gethostbyaddr() in python,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.