Skip to content

Commit af2adbb

Browse files
committed
Add is-visible functionality:
metafizzy#1032
1 parent fb96a24 commit af2adbb

File tree

5 files changed

+39
-0
lines changed

5 files changed

+39
-0
lines changed

js/animate.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ proto.settle = function( previousX ) {
127127
this.positionSlider();
128128
this.dispatchEvent( 'settle', null, [ this.selectedIndex ] );
129129
}
130+
131+
this.checkVisibility();
130132
};
131133

132134
proto.shiftWrapCells = function( x ) {

js/drag.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ proto.updateDraggable = function() {
8383
// disable dragging if less than 2 slides. #278
8484
if ( this.options.draggable == '>1' ) {
8585
this.isDraggable = this.slides.length > 1;
86+
} else if (this.options.draggable === 'onOverflow') {
87+
this.isDraggable = this.viewport.scrollWidth > this.viewport.offsetWidth;
8688
} else {
8789
this.isDraggable = this.options.draggable;
8890
}

js/flickity.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,27 @@ proto.queryCell = function( selector ) {
757757
return this.getCell( selector );
758758
};
759759

760+
proto.checkVisibility = function() {
761+
var viewportX = this.viewport.getBoundingClientRect().x;
762+
var viewportWidth = this.viewport.offsetWidth;
763+
764+
this.cells.forEach(function (cell) {
765+
var cellX = cell.element.getBoundingClientRect().x - viewportX;
766+
var isVisible = (
767+
(cellX + cell.size.innerWidth > viewportX) && (cellX + cell.size.innerWidth < viewportWidth) ||
768+
(cellX > viewportX) && (cellX < viewportWidth)
769+
);
770+
771+
if (isVisible) {
772+
cell.element.classList.add('is-visible');
773+
cell.element.removeAttribute('aria-hidden');
774+
} else {
775+
cell.element.classList.remove('is-visible');
776+
cell.element.setAttribute('aria-hidden', true);
777+
}
778+
});
779+
};
780+
760781
// -------------------------- events -------------------------- //
761782

762783
proto.uiChange = function() {

js/slide.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ proto.getLastCell = function() {
5151
};
5252

5353
proto.select = function() {
54+
this.parent.checkVisibility();
55+
5456
this.cells.forEach( function( cell ) {
5557
cell.select();
5658
} );

sandbox/basic.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,18 @@ <h2>contain, few</h2>
143143

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

146+
<h2>contain, only draggable if overflow</h2>
147+
148+
<div class="container variable-width js-flickity"
149+
data-flickity-options='{ "contain": true, "cellAlign": "left", "draggable": "onOverflow" }'>
150+
<div class="cell n1">1</div>
151+
<div class="cell n2">2</div>
152+
<div class="cell n3">3</div>
153+
<div class="cell n4">4</div>
154+
<div class="cell n5">5</div>
155+
<div class="cell n6">6</div>
156+
</div>
157+
146158
<div id="gallery5" class="container variable-width js-flickity"
147159
data-flickity-options='{ "wrapAround": true, "watchCSS": true }'>
148160
<div class="cell n1 w3"></div>

0 commit comments

Comments
 (0)