@momo-kits/carousel 0.151.1-beta.3 → 0.151.1-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.tsx +9 -16
  2. package/package.json +1 -1
package/index.tsx CHANGED
@@ -398,25 +398,18 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
398
398
  return 0;
399
399
  }
400
400
 
401
- if (
402
- this._positions[lastIndex] &&
403
- offset >= this._positions[lastIndex].start
404
- ) {
405
- return lastIndex;
406
- }
407
-
401
+ let minDistance = Infinity;
408
402
  for (let i = 0; i < this._positions.length; i++) {
409
403
  const { start, end } = this._positions[i];
410
- if (
411
- center + activeSlideOffset >= start &&
412
- center - activeSlideOffset <= end
413
- ) {
404
+ const itemCenter = (start + end) / 2;
405
+ const distance = Math.abs(itemCenter - center);
406
+ if (distance < minDistance) {
407
+ minDistance = distance;
414
408
  itemIndex = i;
415
- break;
416
409
  }
417
410
  }
418
411
 
419
- return itemIndex || 0;
412
+ return itemIndex !== undefined ? itemIndex : 0;
420
413
  }
421
414
 
422
415
  _initPositionsAndInterpolators(props = this.props) {
@@ -515,10 +508,10 @@ class Carousel extends React.PureComponent<CarouselProps, CarouselState> {
515
508
 
516
509
  if (nextActiveItem !== this._onScrollActiveItem) {
517
510
  this._onScrollActiveItem = nextActiveItem;
518
- onScrollIndexChanged &&
519
- onScrollIndexChanged(this._getDataIndex(nextActiveItem));
511
+ const dataIndex = this._getDataIndex(nextActiveItem);
520
512
 
521
- onSnapToItem && onSnapToItem(this._getDataIndex(nextActiveItem));
513
+ onScrollIndexChanged && onScrollIndexChanged(dataIndex);
514
+ onSnapToItem && onSnapToItem(dataIndex);
522
515
  }
523
516
 
524
517
  if (
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@momo-kits/carousel",
3
- "version": "0.151.1-beta.3",
3
+ "version": "0.151.1-beta.5",
4
4
  "private": false,
5
5
  "main": "index.tsx",
6
6
  "peerDependencies": {