@av_tyschenko

JQ плагин, почему смешиваются события?

Пишу пишу по работе с меню, он по идеи должен убирать лишние пункты, что не поместились в блок. Но проблема не в том.

Переменная This смешивается события, путает переменные, но только до тех пор, пока я не вставляю console.log(''); в любом месте кода, в любом исполнении. То есть по факту) Я даже замерить параметры не могу)

У кого есть идеи насчет var $this = $(this); ? Буду очень признателен

(function( $ ){

  $.fn.menu_adaptive = function(method){

    var $this = $(this);

    var methods = {

      init : function( options ) {

        return this.each(function(){

          console.log('');

          $this.nav = $(this);
          $this.nav_li = $( ' > li', $this.nav );

          $this.numOfItems  = 0;
          $this.totalSpace  = 0;
          $this.breakWidths = [];

          $this.nav_li.each(function(index, el) {

            $this.totalSpace += $(this).outerWidth();
            $this.numOfItems += 1;
            $this.breakWidths.push( $this.totalSpace );

          });

          methods._build();

        });

      },

      _build : function() {

        $this.availableSpace    = $this.nav.width() - 10;
        $this.numOfVisibleItems = $this.nav.children().length;
        $this.requiredSpace     = $this.breakWidths[$this.numOfVisibleItems - 1];

        if ( $this.requiredSpace > $this.availableSpace ) {

          if ( $('li.hidden-items', $this.nav).length == 0 ) {

            $this.nav.append( '<li class="hidden-items">...<ul></ul></li>' );
            $this.hidden_nav = $( '.hidden-items ul', $this.nav );

          }

          $this.nav.children('.menu-item').last().prependTo( $this.hidden_nav );
          $this.numOfVisibleItems -= 1;

          methods._build();

        } else if ($this.availableSpace > $this.breakWidths[$this.numOfVisibleItems - 1]) {


          if($this.numOfItems != $this.numOfVisibleItems) {

            $this.hidden_nav.children('.menu-item').first().insertBefore( $( ' > .hidden-items', $this.nav ) );
            $this.numOfVisibleItems += 1;

          }

        }

      }

    };

    if (methods[method]) {
      return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
    } else if (typeof method === 'object' || ! method) {
      return methods.init.apply(this, arguments);
    } else {
      return methods.init.apply(this);
    }

  };

})( jQuery );
  • Вопрос задан
  • 67 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы