简体   繁体   English

mootools切换按钮问题

[英]mootools toggle button problem

I'm trying to code a function that let me toggle in and out all thumbnails in a list depending on their classes. 我正在尝试编写一个函数,该函数让我根据类的类别来切换列表中的所有缩略图。 eg, if I click "print" on my menu bar, I need all thumbs with the "print" class to be hidden. 例如,如果单击菜单栏上的“打印”,则需要隐藏所有带有“打印”类的拇指。 If I click it a second time, the hidden thumbs are showing up. 如果第二次单击,则显示隐藏的拇指。

Here is what I came up with : 这是我想出的:

window.addEvent('domready', function(){
       $$('.menu_button').toggle(
        function() {
            this.fade(0.5);
            var buttonId = this.id;
            $('slider_list').getElements('.'+buttonId).each(function(li) {
                li.tween('width','0');
            });
        },
        function() {
            this.fade(1);
            var buttonId = this.id;
            $('slider_list').getElements('.'+buttonId).each(function(li) {
                li.tween('width','100');
            });
        }
    );  

});



//toggle (emulate JQuery's toggle)
(function() {
    var toggled = 0;
    Element.implement({
        toggle: function(fn1, fn2) {
            var fns = [fn1, fn2];
            return this.addEvent('click', function(){
                fns[toggled].apply(this, arguments);
                toggled = toggled == 0 ? 1 : 0;
            });
        }
    });
})();

I've found the toggle function here 我在这里找到了切换功能

Now I experience some issues. 现在我遇到了一些问题。 First no matter what I do there will always be a thumb left at the end of the list. 首先,无论我做什么,列表末尾总是会留下拇指。 Then some clicks on the menu won't do anything. 然后,单击菜单将无济于事。 Generally when I click on a button in a different state (hidden/shown) than the previous one, there will always be a null click... 通常,当我单击状态不同于上一个按钮(隐藏/显示)的按钮时,总是会出现空点击...

Anybody ? 有人吗

I implemented it in a way that allows multiple functions, although not at all MooTools like, it will work. 我以允许多种功能的方式实现了它,尽管一点也不像MooTools那样,它将起作用。 The problem with the code you are using is that each element which uses toggle is toggling the same toggled variable 您使用的代码的问题在于,每个使用切换的元素都在切换相同的toggled变量

Element.implement({
    toggle: function() {
       this.store('toggle_options', {
         fn: arguments,
         cur: 0
       });
       this.addEvent('click', function(e) {
           e.stop();
           var opts = this.retrieve('toggle_options');
           console.log(opts.fn.length, opts.cur);
           opts.fn[opts.cur++].apply(this);
           if(opts.cur >= opts.fn.length) opts.cur = 0;
       }); 
    } 
});

$('button').toggle(
    function() {
      this.set('text', 'foo 1');  
    },
    function() {
      this.set('text', 'bar 2');
    }
);

fiddle here: http://jsfiddle.net/94FFj/ 在这里提琴: http : //jsfiddle.net/94FFj/

Although I would recommend you implemented your code as this: 尽管我建议您按以下方式实现代码:

  $$('.menu_button').each(function(button) {
    button.store('toggle_active', 0);
    button.addEvent('click', function(e) {
      e.stop();
      var active = this.retrieve('toggle_active');
      var opts = [{opacity: 1, width: 0}, {opacity: 0.5, width: 100}];
      this.fade(opts[active].opacity);
      $$('slider_list .' + this.get('id')).tween('width', opts[active].width);
      this.store('toggle_active', active ? 0 : 1);
    });
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM