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

[FIX] wrong value set for the created frame width on first render #204

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
19 changes: 16 additions & 3 deletions src/siema.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export default class Siema {
Math.max(0, Math.min(this.config.startIndex, this.innerElements.length - this.perPage));
this.transformProperty = Siema.webkitOrNot();

// flag that will be used inside the buildSliderFrame method
this.isFirstRender = true;

// Bind all event handlers for referencability
['resizeHandler', 'touchstartHandler', 'touchendHandler', 'touchmoveHandler', 'mousedownHandler', 'mouseupHandler', 'mouseleaveHandler', 'mousemoveHandler', 'clickHandler'].forEach(method => {
this[method] = this[method].bind(this);
Expand Down Expand Up @@ -157,12 +160,10 @@ export default class Siema {
* Build a sliderFrame and slide to a current item.
*/
buildSliderFrame() {
const widthItem = this.selectorWidth / this.perPage;
const itemsToBuild = this.config.loop ? this.innerElements.length + (2 * this.perPage) : this.innerElements.length;

// Create frame and apply styling
// Create frame
this.sliderFrame = document.createElement('div');
this.sliderFrame.style.width = `${widthItem * itemsToBuild}px`;
this.enableTransition();

if (this.config.draggable) {
Expand Down Expand Up @@ -197,6 +198,18 @@ export default class Siema {
this.selector.innerHTML = '';
this.selector.appendChild(this.sliderFrame);

if (this.isFirstRender) {
// re-set the selectorWidth, because the offsetWidth value of the selector
// may have changed since the constructor was called
this.selectorWidth = this.selector.offsetWidth;

this.isFirstRender = false
}

// apply styling on the created frame, now that we are sure the offsetWidth is correct
const widthItem = this.selectorWidth / this.perPage;
this.sliderFrame.style.width = `${widthItem * itemsToBuild}px`;

// Go to currently active slide after initial build
this.slideToCurrent();
}
Expand Down