angularjs - Ionic: Get the currently visible items in ion-content -
i have long, scrollable ion-content
area in app, filled items using collection-repeat
.
i need know items visible user.
i cannot use $ionicscrolldelegate.getscrollposition
calculate answer, because each item has different height (item height calculated per item).
ended calculating summed heights myself of elements, , querying translatey
value of .scroll
element, can find out item in visible part of scroll.
it's reinventing wheel, works.
when load items, call scrollmanager.setitemheights(heights)
(heights
array of item heights in pixels), , index of visible item: scrollmanager.getvisibleitemindex()
angular.module("services") .service('scrollmanager', function() { var gettranslatey, getvisibleitemindex, setitemheights, summedheights; summedheights = null; setitemheights = function(heights) { var height, sum, _i, _len; summedheights = [0]; sum = 0; (_i = 0, _len = heights.length; _i < _len; _i++) { height = heights[_i]; sum += height; summedheights.push(sum); } }; // returns style translatey of .scroll element, in pixels gettranslatey = function() { return number(document.queryselector('.scroll').style.transform.match(/,\s*(-?\d+\.?\d*)\s*/)[1]); }; getvisibleitemindex = function() { var i, y; y = -gettranslatey(); = 0; while (summedheights[i] < y) { i++; } return i; }; return { setitemheights: setitemheights, getvisibleitemindex: getvisibleitemindex }; });
Comments
Post a Comment