How to hide drawer upon user click

后端 未结 12 1761
北恋
北恋 2020-12-16 14:43

How do I hide the drawer when the user clicks on an item? Or when a button is clicked?

12条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-16 14:57

    Showing and hiding the menu is as easy as adding and removing the .is-visible class as it can be seen in the source:

    MaterialLayout.prototype.drawerToggleHandler_ = function() {
      'use strict';
    
      this.drawer_.classList.toggle(this.CssClasses_.IS_DRAWER_OPEN);
    };
    

    So you would have something like this:

    function toggle_drawer() {
      var drawer = document.getElementsByClassName('mdl-layout__drawer')[0];
      drawer.classList.toggle("is-visible");
    }
    

    I was hoping for a more convenient method of the MaterialLayout widget, but the best that I came up with was:

    var layout = document.getElementsByClassName('mdl-layout')[0];
    layout.MaterialLayout.drawerToggleHandler_();
    

    although this happens to be working, that _ at the end of the method name shows that this function is not supposed to be (mis)used as a public API method.

提交回复
热议问题