Floating Action Button
Materialize is a modern responsive CSS framework based on Material Design by Google.
Copied!
content_copy
Initialization
Copied!
content_copy
document.addEventListener('DOMContentLoaded', function() {
var elems = document.querySelectorAll('.fixed-action-btn');
var instances = M.FloatingActionButton.init(elems, {
// specify options here
});
});
document.addEventListener('DOMContentLoaded', function() {
var elems = document.querySelectorAll('.fixed-action-btn');
var instances = M.FloatingActionButton.init(elems, {
// specify options here
});
});
Options
Name | Type | Default | Description |
---|---|---|---|
direction | String | 'top' | Direction FAB menu opens. Can be 'top', 'right', 'bottom', 'left' |
hoverEnabled | Boolean | true | If true, FAB menu will open on hover instead of click |
toolbarEnabled | Boolean | false | Enable transit the FAB into a toolbar on click |
Methods
All the methods are called on the plugin instance. You can get the plugin instance like this:
Copied! content_copyvar instance = M.FloatingActionButton.getInstance(elem);
.open();
Opens FAB.
instance.open();
.close();
Closes FAB.
instance.close();
.destroy();
Destroy plugin instance and teardown
instance.destroy();
Properties
Name | Type | Description |
---|---|---|
el | Element | The DOM element the plugin was initialized with. |
options | Object | The options the instance was initialized with. |
isOpen | Boolean | Describes open/close state of the FAB. |
Horizontal FAB
Creating a horizontal FAB is easy! Just set the direction option.
Copied!
content_copy
document.addEventListener('DOMContentLoaded', function() {
var elems = document.querySelectorAll('.fixed-action-btn');
var instances = M.FloatingActionButton.init(elems, {
direction: 'left'
});
});
Click-only FAB
If you want to disable the hover behaviour, and instead toggle the FAB menu when the user clicks on the large button
(works great on mobile!), just add the
click-to-toggle
class to the FAB.
Copied!
content_copy
document.addEventListener('DOMContentLoaded', function() {
var elems = document.querySelectorAll('.fixed-action-btn');
var instances = M.FloatingActionButton.init(elems, {
direction: 'left',
hoverEnabled: false
});
});
FAB to Toolbar
Deprecated since Version 2.1.0!
Instead of displaying individual button options, you can transition your FAB into a toolbar on click. Just add the
toolbar
class to the FAB.
Copied!
content_copy
document.addEventListener('DOMContentLoaded', function() {
var elems = document.querySelectorAll('.fixed-action-btn');
var instances = M.FloatingActionButton.init(elems, {
toolbarEnabled: true
});
});