Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/assets/natural-gallery-js/natural-gallery.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/assets/natural-gallery-js/natural-gallery.js.map

Large diffs are not rendered by default.

18 changes: 12 additions & 6 deletions src/js/galleries/AbstractGallery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,20 +667,26 @@ export abstract class AbstractGallery<Model extends ModelAttributes = ModelAttri
const scrollable = element;
const wrapper: HTMLElement = element instanceof Document ? element.documentElement : element;

const startScroll = debounce(() => this.elementRef.classList.add('scrolling'), 300, {edges: ['leading']});
const endScroll = debounce(() => this.elementRef.classList.remove('scrolling'), 300);
const startScroll = debounce(() => this.elementRef.classList.add('scrolling'), 100, {edges: ['leading']});
const endScroll = debounce(() => this.elementRef.classList.remove('scrolling'), 150);

scrollable.addEventListener('scroll', () => {
startScroll();
endScroll();

const endOfGalleryAt =
this.elementRef.offsetTop + this.elementRef.offsetHeight + this.options.infiniteScrollOffset;

// Avoid to expand gallery if we are scrolling up
// Calculate scroll position and delta
const current_scroll_top = wrapper.scrollTop - (wrapper.clientTop || 0);
const wrapperHeight = wrapper.clientHeight;
const scroll_delta = current_scroll_top - this.old_scroll_top;

// Only apply scrolling class if there's actual scroll position change
// This prevents the class from being applied when scroll events are triggered
// without actual scrolling (e.g., on mousehover in some browsers/zoom levels)
if (Math.abs(scroll_delta) > 0) {
startScroll();
endScroll();
}

this.old_scroll_top = current_scroll_top;

// "enableMoreLoading" is a setting coming from the BE bloking / enabling dynamic loading of thumbnail
Expand Down
Empty file removed tests/logs/.gitkeep
Empty file.
36 changes: 36 additions & 0 deletions tests/unit/natural.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,40 @@ describe('Natural Gallery', () => {

expect(gallery.collection.map(getSize)).toEqual(result);
});

it('should not apply scrolling class when scroll events are triggered without actual scrolling', (done) => {
const images: ModelAttributes[] = [
{
thumbnailSrc: 'foo.jpg',
enlargedWidth: 6000,
enlargedHeight: 4000,
},
{
thumbnailSrc: 'bar.jpg',
enlargedWidth: 3648,
enlargedHeight: 5472,
},
];

const container = getContainerElement(500);
const gallery = new Natural(container, {rowHeight: 400});
gallery.addItems(images);

// Simulate a scroll event without actual scroll position change
// This simulates what happens when mouse hover triggers scroll events in some browsers
const scrollEvent = new Event('scroll');

// Initially, the gallery should not have the scrolling class
expect(container.classList.contains('scrolling')).toBe(false);

// Dispatch a scroll event without changing scroll position
document.dispatchEvent(scrollEvent);

// Wait for any potential debounced functions to trigger
setTimeout(() => {
// The scrolling class should NOT be applied since no actual scrolling occurred
expect(container.classList.contains('scrolling')).toBe(false);
done();
}, 200); // Wait longer than the debounce timeout
});
});
Loading