javascript - 如何滑动切换 Selectize.js 下拉菜单?

标签 javascript jquery jquery-plugins jquery-animate selectize.js

我正在使用很棒的 Selectize.js plugin ,我尝试在使用 jQuery 打开下拉菜单时添加一个简单的幻灯片动画。我还将位置更改为静态。

<强> http://jsfiddle.net/TAuEH/1/

我改变了这个:

/**
* Shows the autocomplete dropdown containing
* the available options.
*/
open: function() {
  var self = this;

  if (self.isLocked || self.isOpen || (self.settings.mode === 'multi' && self.isFull())) return;
  self.focus(true);
  self.isOpen = true;
  self.refreshClasses();
  self.$dropdown.css({visibility: 'hidden', display: 'block'});
  self.positionDropdown();
  self.$dropdown.css({visibility: 'visible'});
  self.trigger('dropdown_open', this.$dropdown);
},

/**
* Closes the autocomplete dropdown menu.
*/
close: function() {
  var self = this;

  if (!self.isOpen) return;
  self.$dropdown.hide();
  self.setActiveOption(null);
  self.isOpen = false;
  self.refreshClasses();
  self.trigger('dropdown_close', self.$dropdown);
},

通过这个:

/**
* Shows the autocomplete dropdown containing
* the available options.
*/
open: function() {
  var self = this;

  if (self.isLocked || self.isOpen || (self.settings.mode === 'multi' && self.isFull())) return;
  self.focus(true);
  self.isOpen = true;
  self.refreshClasses();
  self.$dropdown.slideDown();
  self.positionDropdown();
  self.trigger('dropdown_open', this.$dropdown);
},

/**
* Closes the autocomplete dropdown menu.
*/
close: function() {
  var self = this;

  if (!self.isOpen) return;
  self.$dropdown.slideUp();
  self.setActiveOption(null);
  self.isOpen = false;
  self.refreshClasses();
  self.trigger('dropdown_close', self.$dropdown);
},

嗯,正如你所看到的,恐怕它无法正常打开,并且当你关闭它时不会执行幻灯片动画...有人有更好的主意吗?

任何建议、提示或帮助将不胜感激,如果您需要更多信息,我将编辑帖子。

最佳答案

无需对插件代码进行任何更改,您可以尝试使用事件 onDropdownOpenonDropdownClose 并强制播放动画。

虽然有点笨拙,但确实有效。

代码:

$(document).ready(function () {
    $('#sel').selectize({
        create: false,
        sortField: 'text',
        onDropdownOpen: function () {
            $(".selectize-dropdown").hide().slideToggle();
        },
        onDropdownClose: function () {
            $(".selectize-dropdown").show().slideToggle();
        }
    });
});

演示:http://jsfiddle.net/IrvinDominin/cHxcZ/

编辑

在以前的版本中,当您尝试再次按输入关闭它时会出现问题,可使用 stop 解决。

引用:

When .stop() is called on an element, the currently-running animation (if any) is immediately stopped. If, for instance, an element is being hidden with .slideUp() when .stop() is called, the element will now still be displayed, but will be a fraction of its previous height. Callback functions are not called.

If more than one animation method is called on the same element, the later animations are placed in the effects queue for the element. These animations will not begin until the first one completes. When .stop() is called, the next animation in the queue begins immediately. If the clearQueue parameter is provided with a value of true, then the rest of the animations in the queue are removed and never run.

代码:

$(document).ready(function () {
    $('#sel').selectize({
        create: false,
        sortField: 'text',
        onDropdownOpen: function ($dropdown) {
            $dropdown.stop().hide().slideToggle();
        },
        onDropdownClose: function ($dropdown) {
            $dropdown.stop().show().slideToggle();
        }
    });
});

演示:http://jsfiddle.net/IrvinDominin/cHxcZ/1/

关于javascript - 如何滑动切换 Selectize.js 下拉菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18823334/

相关文章:

javascript - 无法理解 Node.js 中的错误

javascript - jQuery .parents([selector]) 不适用于 IE9 和 Firefox

javascript - jQuery 微醉插件。焦点触发不起作用

jquery - 当 jQuery 本身被传递时,到底会发生什么?

jquery - 使用jquery获取两个日期之间的差异

javascript - 属性指令 scope.apply 在 ng-repeat 中使用时不起作用

javascript - 使用 pdfmake.js 在列中自动宽度图像

javascript - Browserify,如何访问 main.js 函数

javascript - 使用 jquery 在页面中添加基于 html 的类?

jquery - ASP.net MVC - 停止页面渲染直到 JQuery 完成