Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/watch visibility #1032

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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: 2 additions & 0 deletions js/animate.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ proto.settle = function( previousX ) {
this.positionSlider();
this.dispatchEvent( 'settle', null, [ this.selectedIndex ] );
}

this.checkVisibility();
};

proto.shiftWrapCells = function( x ) {
Expand Down
4 changes: 4 additions & 0 deletions js/drag.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,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 Expand Up @@ -239,6 +241,8 @@ proto.dragMove = function( event, pointer, moveVector ) {

this.dragMoveTime = new Date();
this.dispatchEvent( 'dragMove', event, [ pointer, moveVector ] );

this.checkVisibility();
};

proto.dragEnd = function( event, pointer ) {
Expand Down
19 changes: 19 additions & 0 deletions js/flickity.js
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,25 @@ 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 = (

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my testing, isVisible will be false when the is-selected cell is itself wider than the browser window, despite that the cell is obviously visible (being the is-selected cell centered on screen).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isVisible will not function correctly unless the slider is full width. Anything in-line will not work if the cell is not wider than the viewport offset.

I have added a test to check if the value of cellX === 0 as it is the first element within the viewport if the value of cellX is 0 (More specifically I am checking if cellX > -1 && cellX < 1 due to the fluctuation of cell placement when the carousel stops moving).

This is not an ideal solution as I am getting flashing before a slide comes to rest, but it is solving the issue in my use case.

(cellX + cell.size.innerWidth > viewportX) && (cellX + cell.size.innerWidth < viewportWidth) ||
(cellX > viewportX) && (cellX < viewportWidth)
);

if (isVisible) {
cell.element.classList.add('is-visible');
} else {
cell.element.classList.remove('is-visible');
}
});
};

// -------------------------- 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 @@ -52,6 +52,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 @@ -141,6 +141,18 @@ <h2>contain, few</h2>
<!-- <div class="cell n3">3</div> -->
</div>

<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>

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

<div id="gallery5" class="container variable-width js-flickity"
Expand Down