0

I'm currently building a script to create a slider and stumbled upon an error I can't seem to solve. Basically I trying to get the width of a container and multiply it base on the number of slides inside.

Here is a snippet a of the code I'm working on. Whenever I try to use .width in order to fetch the width of a container it returns undefined on the console. I tried looking back and forth on my code but I can't seem to pinpoint the issue.

_setSliderWidth() {
    this.sliderBanner = this.$el.find('.slider-banner');
    this.sliderBannerWidth = this.sliderBanner.width();

    console.log(this.sliderBannerWidth);


    this.slides.width(this.sliderBannerWidth);
    this.slidesContainer.width(this.sliderBanner.width() * this.slideCount);

  }



  -- -- -- -- --


  'use strict';

(function($) {

  /**
   * Base Image Slider class
   */
  class ImageSlider {

    constructor(el) {
      this.$el = $(el);
      this._dom();

      this.slideCount = this.slides.length;
      this.currentSlide = 0;

      this.arrows = {
        prev: this.$el.find('.arrow.-prev'),
        next: this.$el.find('.arrow.-next')
      };

      // image formatting and detection
      this.$el.find('img').each(function(e, el) {
        let $img = $(el);

        if ($img.height() > $img.width())
          $img.addClass('-portrait');
      });

      this._setSliderWidth();
    }

    _dom() {
      this.slides = this.$el.find('.slides');
      this.slidesContainer = this.$el.find('.slider-items');

    }

    init() {
      this._bind();
      this._evaluatePosition();
    }

    _bind() {
      this.arrows.next.on('click', this._nextSlide.bind(this));
      this.arrows.prev.on('click', this._prevSlide.bind(this));
    }

    _nextSlide() {
      this.currentSlide++;
      this._moveSlide();
    }

    _prevSlide() {
      this.currentSlide--;
      this._moveSlide();
    }

    _setSliderWidth() {
      this.sliderBanner = this.$el.find('.slider-banner');
      this.sliderBannerWidth = this.sliderBanner.width();

      console.log(this.sliderBannerWidth);


      this.slides.width(this.sliderBannerWidth);
      this.slidesContainer.width(this.sliderBanner.width() * this.slideCount);

    }

    _moveSlide() {

      // set the min and max range
      if (this.currentSlide < 0) this.currentSlide = 0;
      if (this.currentSlide > this.slideCount - 1) this.currentSlide = this.slideCount - 1;


      this._evaluatePosition();
      this._move();
    }

    _move() {
      let position = this.currentSlide * -100;
      this.slidesContainer.css({
        transform: 'translate(' + position + '%, 0)'
      });
    }

    _evaluatePosition() {
      this.arrows.prev.toggleClass('-hide', (this.currentSlide === 0));
      this.arrows.next.toggleClass('-hide', (this.currentSlide === this.slideCount - 1));
    }

  }



  $(document).ready(function() {

    //--------------------------------------------------
    // Image Slider
    let $imageSliders = $('.image-slider');

    $imageSliders.each(function(e, el) {
      let imageSlider = new ImageSlider(el);
      imageSlider.init();
    });


    //--------------------------------------------------
    // Slider Banner
    let $bannerSliders = $('.slider-banner');

    $bannerSliders.each(function(e, el) {
      let bannerSlider = new ImageSlider(el);
      bannerSlider.init();
    });

  });

})(jQuery);

HTML

<div class="slider-banner -alternate">
  <span href="#" class="arrow -prev -hide"></span>
  <span href="#" class="arrow -next"></span>
  <div class="slider-items">
    <div class="slides">
      <div class="image" style="background-image:url(/iom/sites/default/files/2018-07/partnerships-2_0.jpg)">
        <div class="banner-detail">
          <div class="article-detail">
            <div class="timestamp">
              <a href="#" class="tag-label">page</a>
            </div>
            <h2 class="title">
              <a href="#">Migrant Integration</a>
            </h2>
            <div class="mini-caption">
              IOM supports policies and strategies that promote the social, economic and cultural inclusion of migrants within existing legal frameworks in countries of destination.
            </div>
            <a href="/iom/node/65348" class="button">More Details</a>
          </div>
        </div>
      </div>
    </div>
    <div class="slides">
      <div class="image" style="background-image:url(/iom/sites/default/files/2018-07/definitional-issues_1.jpg)">
        <div class="banner-detail">
          <div class="article-detail">
            <div class="timestamp">
              <a href="#" class="tag-label">page</a>
            </div>
            <h2 class="title">
              <a href="#">Forum on Migration, Trade and the Global Economy</a>
            </h2>
            <div class="mini-caption">
              IOM, together with partners ICTSD and Fundanción Foro del Sur will host the Forum on Migration, Trade &amp; the Global Economy in Buenos Aires on 14 December.
            </div>
            <a href="/iom/forum-migration-trade-and-global-economy" class="button">More Details</a>
          </div>
        </div>
      </div>
    </div>
    <div class="slides">
      <div class="image" style="background-image:url(/iom/sites/default/files/2018-07/identity-management_0.jpg)">
        <div class="banner-detail">
          <div class="article-detail">
            <div class="timestamp">
              <a href="#" class="tag-label">page</a>
            </div>
            <h2 class="title">
              <a href="#">Comparative Research on the Assisted Voluntary Return and Reintegration of Migrants</a>
            </h2>
            <div class="mini-caption">
              Assisted Voluntary Return and Reintegration (AVRR) is an indispensable part of a comprehensive approach to migration management aiming at orderly and humane return and reintegration of migrants.
            </div>
            <a href="/iom/comparative-research-assisted-voluntary-return-and-reintegration-migrants" class="button">More Details</a>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
Anshuman
  • 758
  • 7
  • 23
clestcruz
  • 1,081
  • 3
  • 31
  • 75
  • 2
    What's the output of `console.log(this.sliderBanner)`? – Samleo Oct 11 '18 at 01:30
  • @Samleo it returns the object? Sorry I'm familiar with the right word to associate it. I have attached a link to see the screenshot https://imgur.com/a/F83rfyT – clestcruz Oct 11 '18 at 01:35
  • It is supposed to return the DOM (div) object.. since .width() can only be called on the DOM objects, so according to what the console log in the image says, u might have to use `this. sliderBanner.prevObject.width()` – Samleo Oct 11 '18 at 01:41
  • @Samleo Yes, I really can't pinpoint the issue here. – clestcruz Oct 11 '18 at 01:42
  • @Samleo that is weird that I have to use `this. sliderBanner.prevObject.width()` to fetch the width. – clestcruz Oct 11 '18 at 02:08
  • @clestcruz In your screenshot, `this.sliderBanner` has no elements in it. Do you notice that the `length` is `0` there? – choz Oct 11 '18 at 02:15
  • @clestcruz I'm not to sure but you might want to check it out in this thread: https://stackoverflow.com/questions/19697495/what-is-prevobject-and-why-is-my-selector-returning-that – Samleo Oct 11 '18 at 02:15
  • @clestcruz also did it work though – Samleo Oct 11 '18 at 02:16

1 Answers1

1

It seems from your screenshots and code that the this. sliderBanner object does NOT return a DOM object and thus the .width() would be undefined.

To resolve you can: 1) Retrieve the DOM object through a weird method of this.sliderBanner.prevObject. More information in this thread: What is prevObject and why is my selector returning that?

The main problem is that the .find from the $el object can't doesn't have the slider banner object within it's DOM, so...

2) Try using this.sliderBanner = $(".slider banner") to select the banner from the document object instead

Samleo
  • 605
  • 5
  • 16