Back to Materialize

Dropdown Content

jade/page-contents/dropdown_content.html

1.0.03.5 KB
Original Source

Add a dropdown list to any button. Make sure that the data-target attribute matches the id in the <ul> tag. You can add a divider with the <li class="divider"></li> tag. You can also add icons. Just add the icon inside the anchor tag.

Drop Me!

<!-- Dropdown Trigger -->
  <a class='dropdown-trigger btn' href='#' data-target='dropdown1'>Drop Me!</a>

  <!-- Dropdown Structure -->
  <ul id='dropdown1' class='dropdown-content'>
    <li><a href="#!">one</a></li>
    <li><a href="#!">two</a></li>
    <li class="divider" tabindex="-1"></li>
    <li><a href="#!">three</a></li>
    <li><a href="#!"><i class="material-icons">view_module</i>four</a></li>
    <li><a href="#!"><i class="material-icons">cloud</i>five</a></li>
  </ul>

Initialization

document.addEventListener('DOMContentLoaded', function() {
    var elems = document.querySelectorAll('.dropdown-trigger');
    var instances = M.Dropdown.init(elems, options);
  });

  // Or with jQuery

  $('.dropdown-trigger').dropdown();

Options

NameTypeDefaultDescription
alignmentString'left'Defines the edge the menu is aligned to.
autoTriggerBooleantrueIf true, automatically focus dropdown el for keyboard.
constrainWidthBooleantrueIf true, constrainWidth to the size of the dropdown activator.
containerElementnullProvide an element that will be the bounding container of the dropdown.
coverTriggerBooleantrueIf false, the dropdown will show below the trigger.
closeOnClickBooleantrueIf true, close dropdown on item click.
hoverBooleanfalseIf true, the dropdown will open on hover.
inDurationNumber150The duration of the transition enter in milliseconds.
outDurationNumber250The duration of the transition out in milliseconds.
onOpenStartFunctionnullFunction called when dropdown starts entering.
onOpenEndFunctionnullFunction called when dropdown finishes entering.
onCloseStartFunctionnullFunction called when dropdown starts exiting.
onCloseEndFunctionnullFunction called when dropdown finishes exiting.

Methods

Because jQuery is no longer a dependency, all the methods are called on the plugin instance. You can get the plugin instance like this:

var instance = M.Dropdown.getInstance(elem);

/* jQuery Method Calls
You can still use the old jQuery plugin method calls.
But you won't be able to access instance properties.

$('.dropdown-trigger').dropdown('methodName');
$('.dropdown-trigger').dropdown('methodName', paramName);
*/
.open();

Open dropdown.

instance.open();
.close();

Close dropdown.

instance.close();
.recalculateDimensions();

While dropdown is open, you can recalculate its dimensions if its contents have changed.

instance.recalculateDimensions();
.destroy();

Destroy plugin instance and teardown

instance.destroy();

Properties

NameTypeDescription
elElementThe DOM element the plugin was initialized with.
optionsObjectThe options the instance was initialized with.
idStringID of the dropdown element.
dropdownElElementThe DOM element of the dropdown.
isOpenBooleanIf the dropdown is open.
isScrollableBooleanIf the dropdown content is scrollable.
focusedIndexNumberThe index of the item focused.