Skip to content

Commit

Permalink
Add is-visible functionality:
Browse files Browse the repository at this point in the history
  • Loading branch information
KelseyCooper committed Feb 18, 2021
1 parent fb96a24 commit af2adbb
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions js/animate.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ proto.settle = function( previousX ) {
this.positionSlider();
this.dispatchEvent( 'settle', null, [ this.selectedIndex ] );
}

this.checkVisibility();
};

proto.shiftWrapCells = function( x ) {
Expand Down
2 changes: 2 additions & 0 deletions js/drag.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ proto.updateDraggable = function() {
// disable dragging if less than 2 slides. #278
if ( this.options.draggable == '>1' ) {
this.isDraggable = this.slides.length > 1;
} else if (this.options.draggable === 'onOverflow') {
this.isDraggable = this.viewport.scrollWidth > this.viewport.offsetWidth;
} else {
this.isDraggable = this.options.draggable;
}
Expand Down
21 changes: 21 additions & 0 deletions js/flickity.js
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,27 @@ proto.queryCell = function( selector ) {
return this.getCell( selector );
};

proto.checkVisibility = function() {
var viewportX = this.viewport.getBoundingClientRect().x;
var viewportWidth = this.viewport.offsetWidth;

this.cells.forEach(function (cell) {
var cellX = cell.element.getBoundingClientRect().x - viewportX;
var isVisible = (
(cellX + cell.size.innerWidth > viewportX) && (cellX + cell.size.innerWidth < viewportWidth) ||
(cellX > viewportX) && (cellX < viewportWidth)
);

if (isVisible) {
cell.element.classList.add('is-visible');
cell.element.removeAttribute('aria-hidden');
} else {
cell.element.classList.remove('is-visible');
cell.element.setAttribute('aria-hidden', true);
}
});
};

// -------------------------- events -------------------------- //

proto.uiChange = function() {
Expand Down
2 changes: 2 additions & 0 deletions js/slide.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ proto.getLastCell = function() {
};

proto.select = function() {
this.parent.checkVisibility();

this.cells.forEach( function( cell ) {
cell.select();
} );
Expand Down
12 changes: 12 additions & 0 deletions sandbox/basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,18 @@ <h2>contain, few</h2>

<h2>watch, activate >900px</h2>

<h2>contain, only draggable if overflow</h2>

<div class="container variable-width js-flickity"
data-flickity-options='{ "contain": true, "cellAlign": "left", "draggable": "onOverflow" }'>
<div class="cell n1">1</div>
<div class="cell n2">2</div>
<div class="cell n3">3</div>
<div class="cell n4">4</div>
<div class="cell n5">5</div>
<div class="cell n6">6</div>
</div>

<div id="gallery5" class="container variable-width js-flickity"
data-flickity-options='{ "wrapAround": true, "watchCSS": true }'>
<div class="cell n1 w3"></div>
Expand Down

0 comments on commit af2adbb

Please sign in to comment.