css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <html ng-app="ionicApp">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>Ionic List Directive</title>
   
    <link href="//code.ionicframework.com/nightly/css/ionic.css" rel="stylesheet">
    <script src="//code.ionicframework.com/nightly/js/ionic.bundle.js"></script>
  </head>

  <body ng-controller="ItemsCtrl">
    <ion-header-bar class="bar-positive"></ion-header-bar>
    <ion-content id="content" has-bouncing="false">
      <ion-infinite-scroll-top ng-if="hmd" on-infinite="getItems()" distance="5px"></ion-infinite-scroll-top>
      <ion-list id="message-list">
        <ion-item ng-repeat="item in items">
          {{item.name}}
        </ion-item>
      </ion-list>
    </ion-content>
  </body>
</html>
            
          
!
            
              ion-infinite-scroll-top {
  height: 60px;
  width: 100%;
  display: block;
  display: -webkit-box;
  display: -webkit-flex;
  display: -moz-box;
  display: -moz-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-direction: normal;
  -webkit-box-orient: horizontal;
  -webkit-flex-direction: row;
  -moz-flex-direction: row;
  -ms-flex-direction: row;
  flex-direction: row;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  -webkit-justify-content: center;
  -moz-justify-content: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  -webkit-align-items: center;
  -moz-align-items: center;
  align-items: center;
}

ion-infinite-scroll-top .icon
{
  color: #666666;
  font-size: 30px;
  color: #666666;
}

ion-infinite-scroll-top:not(.active) .spinner,
ion-infinite-scroll-top:not(.active) .icon:before
{
    display: none;
}
            
          
!
            
              angular.module('ionicApp', ['ionic'])

.controller('ItemsCtrl', function($scope, $ionicScrollDelegate, $timeout) {
  $scope.items = [];
  $scope.hmd = true;
  
  $scope.scrollViewHeight = 0;

  $scope.getItems = function() {
    if ($scope.items.length >= 100) {
      $scope.hmd = false;
    } else {
      $scope.scrollViewHeight = document.getElementById("message-list").firstElementChild.clientHeight;
      var len = $scope.items.length;
      for (var i = len; i < len + 10; i++) {
        $scope.items.unshift({name: "Item "+i});
      }

      $timeout(function() {
        $scope.$broadcast('scroll.infiniteScrollTopComplete');
       	var newHeight = document.getElementById("message-list").firstElementChild.clientHeight;
        $ionicScrollDelegate.scrollTo(0, newHeight - $scope.scrollViewHeight, false);
      }, 500);
    }
  };
 
  $scope.getItems();
})

.directive('ionInfiniteScrollTop', ['$timeout', function($timeout) {
  return {
    restrict: 'E',
    require: ['?^$ionicScroll', 'ionInfiniteScrollTop'],
    template: function($element, $attrs) {
      if ($attrs.icon) return '<i class="icon {{icon()}} icon-refreshing {{scrollingType}}"></i>';
      return '<ion-spinner icon="{{spinner()}}"></ion-spinner>';
    },
    scope: true,
    controller: '$ionInfiniteScrollTop',
    link: function($scope, $element, $attrs, ctrls) {
      var infiniteScrollTopCtrl = ctrls[1];
      var scrollCtrl = infiniteScrollTopCtrl.scrollCtrl = ctrls[0];
      var jsScrolling = infiniteScrollTopCtrl.jsScrolling = !scrollCtrl.isNative();

      // if this view is not beneath a scrollCtrl, it can't be injected, proceed w/ native scrolling
      if (jsScrolling) {
        infiniteScrollTopCtrl.scrollView = scrollCtrl.scrollView;
        $scope.scrollingType = 'js-scrolling';
        //bind to JS scroll events
        scrollCtrl.$element.on('scroll', infiniteScrollTopCtrl.checkBounds);
      } else {
        // grabbing the scrollable element, to determine dimensions, and current scroll pos
        var scrollEl = ionic.DomUtil.getParentOrSelfWithClass($element[0].parentNode, 'overflow-scroll');
        infiniteScrollTopCtrl.scrollEl = scrollEl;
        // if there's no scroll controller, and no overflow scroll div, infinite scroll wont work
        if (!scrollEl) {
          throw 'Infinite scroll must be used inside a scrollable div';
        }
        //bind to native scroll events
        infiniteScrollTopCtrl.scrollEl.addEventListener('scroll', infiniteScrollTopCtrl.checkBounds);
      }

      // Optionally check bounds on start after scrollView is fully rendered
      var doImmediateCheck = typeof $attrs.immediateCheck !== 'undefined' ? $scope.$eval($attrs.immediateCheck) : true;
      if (doImmediateCheck) {
        $timeout(function() {
          infiniteScrollTopCtrl.checkBounds();
        });
      }
    }
  };
}])

.controller('$ionInfiniteScrollTop', function($scope, $attrs, $element, $timeout, $ionicScrollDelegate) {
  var self = this;
  self.isLoading = false;

  $scope.icon = function() {
    return typeof $attrs.icon !== 'undefined' ? $attrs.icon : 'ion-load-d';
  };

  $scope.spinner = function() {
    return typeof $attrs.spinner !== 'undefined' ? $attrs.spinner : '';
  };

  $scope.$on('scroll.infiniteScrollTopComplete', function() {
    finishInfiniteScrollTop();
  });

  $scope.$on('$destroy', function() {
    if (self.scrollCtrl && self.scrollCtrl.$element) self.scrollCtrl.$element.off('scroll', self.checkBounds);
    if (self.scrollEl && self.scrollEl.removeEventListener) {
      self.scrollEl.removeEventListener('scroll', self.checkBounds);
    }
  });

  // debounce checking infinite scroll events
  self.checkBounds = ionic.Utils.throttle(checkInfiniteBounds, 300);

  function onInfinite() {
    ionic.requestAnimationFrame(function() {
      $element[0].classList.add('active');
    });
    self.isLoading = true;
    $scope.$parent && $scope.$parent.$apply($attrs.onInfinite || '');
  }

  function finishInfiniteScrollTop() {
    ionic.requestAnimationFrame(function() {
      $element[0].classList.remove('active');
    });
    $timeout(function() {
      if (self.jsScrolling) self.scrollView.resize();
      // only check bounds again immediately if the page isn't cached (scroll el has height)
      if ((self.jsScrolling && self.scrollView.__container && self.scrollView.__container.offsetHeight > 0) ||
        !self.jsScrolling) {
        self.checkBounds();
      }
    }, 30, false);
    self.isLoading = false;
  }

  // check if we've scrolled far enough to trigger an infinite scroll
  function checkInfiniteBounds() {
    if (self.isLoading) return;
    var maxScroll = {};

    if (self.jsScrolling) {
      maxScroll = self.getJSMaxScroll();
      var scrollValues = self.scrollView.getValues();
      if ((maxScroll.left !== -1 && scrollValues.left <= maxScroll.left) ||
        (maxScroll.top !== -1 && scrollValues.top <= maxScroll.top)) {
        //    	  var scrollBefore = document.getElementById('content').scrollHeight;
        self.scrollView.freeze(true);
        onInfinite();
        //    	  var scrollAfter = document.getElementById('content').scrollHeight;
        //    	  $ionicScrollDelegate.scrollTo(0, scrollAfter-scrollBefore, false);
      }
    } else {
      maxScroll = self.getNativeMaxScroll();
      if ((
          maxScroll.left !== -1 &&
          self.scrollEl.scrollLeft <= maxScroll.left
        ) || (
          maxScroll.top !== -1 &&
          self.scrollEl.scrollTop <= maxScroll.top
        )) {
        onInfinite();
      }
    }
  }

  // determine the threshold at which we should fire an infinite scroll
  // note: this gets processed every scroll event, can it be cached?
  self.getJSMaxScroll = function() {
    var maxValues = self.scrollView.getScrollMax();
    return {
      left: self.scrollView.options.scrollingX ?
        calculateMaxValue(maxValues.left) :
        -1,
      top: self.scrollView.options.scrollingY ?
        calculateMaxValue(maxValues.top) :
        -1
    };
  };

  self.getNativeMaxScroll = function() {
    var maxValues = {
      left: self.scrollEl.scrollWidth,
      top: self.scrollEl.scrollHeight
    };
    var computedStyle = window.getComputedStyle(self.scrollEl) || {};
    return {
      left: maxValues.left &&
        (computedStyle.overflowX === 'scroll' ||
          computedStyle.overflowX === 'auto' ||
          self.scrollEl.style['overflow-x'] === 'scroll') ?
        calculateMaxValue(maxValues.left) : -1,
      top: maxValues.top &&
        (computedStyle.overflowY === 'scroll' ||
          computedStyle.overflowY === 'auto' ||
          self.scrollEl.style['overflow-y'] === 'scroll') ?
        calculateMaxValue(maxValues.top) : -1
    };
  };

  // determine pixel refresh distance based on % or value
  function calculateMaxValue(maximum) {
    var distance = ($attrs.distance || '2.5%').trim();
    var isPercent = distance.indexOf('%') !== -1;
    return isPercent ?
      0 * (1 + parseFloat(distance) / 100) :
      0 + parseFloat(distance);
  }

  //for testing
  self.__finishInfiniteScrollTop = finishInfiniteScrollTop;

})
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console