Options

All options are optional. Some options overwrite default value.

Setup

itemSelector

Type: String

Select elements to be used as items in the layout. Recommended to always set up, to exclude elements that are not part of the layout. By default, all children elements are selected.

$('.grid').magnet({
  itemSelector: '.grid-item'
});

resize

Type: Boolean Default: true

Enables items to adjust size and position on window resize.

//Items stay on same position
$('.grid').magnet({
  resize: false
});
Hydrogenium 1 H 1.008
Helium 2 He 4.002
Lithium 3 Li 6.94
Beryllium 4 Be 9.012
Borium 5 B 10.81
Carbonium 6 C 12.011
Nitrogenium 7 N 14.007
Oxygenium 8 O 15.999
Fluorum 9 F 18.998
Neon 10 Ne 20.179
Natrium 11 Na 22.989
Magnesium 12 Mg 24.305

resizeContainer

Type: Boolean Default: true

Enables item container to resize.

$('.grid').magnet({
  resizeContainer: false
});
//Container has fixed size
.grid {
  width: 500px;
  height: 500px;
}
Hydrogenium 1 H 1.008
Helium 2 He 4.002
Lithium 3 Li 6.94
Beryllium 4 Be 9.012
Borium 5 B 10.81
Carbonium 6 C 12.011
Nitrogenium 7 N 14.007
Oxygenium 8 O 15.999
Fluorum 9 F 18.998
Neon 10 Ne 20.179
Natrium 11 Na 22.989
Magnesium 12 Mg 24.305

initLayout

Type: Boolean Default: true

Enables layout on initialization. Set initLayout: false to disable layout on initialization to use methods or add events.

var grid = $('.grid').magnet({
  //Disable initial layout
  initLayout: false
});

//Bind event
grid.on('arrangeComplete', function() {
  console.log('Arrange is completed');
});

//Manual initial layout
grid.magnet();

layoutInstant

Type: Boolean

Disables transitions. Set layoutInstant: false to enable transitions on initialization. Transitions on initialization are disabled by default, but enabled after first layout.

$('.grid').magnet({
  layoutInstant: true
});
Hydrogenium 1 H 1.008
Helium 2 He 4.002
Lithium 3 Li 6.94
Beryllium 4 Be 9.012
Borium 5 B 10.81
Carbonium 6 C 12.011
Nitrogenium 7 N 14.007
Oxygenium 8 O 15.999
Fluorum 9 F 18.998
Neon 10 Ne 20.179
Natrium 11 Na 22.989
Magnesium 12 Mg 24.305

itemStyle

Type: Object Default: { position: 'absolute' }

Style applied to items.

$('.grid').magnet({
  itemStyle: { 
    //Do not overwrite
    position: 'absolute',
    //Additional properties with
    //priority over CSS
    borderRadius: 0,
    boxShadow: 'none'
  }
});

containerStyle

Type: Object Default: { position: 'relative' }

Style applied to item container.

$('.grid').magnet({
  containerStyle: { position: 'relative' }
});

Layout

layoutMode

Type: String Default: 'grid' Value: 'grid', 'rows', 'columns', 'horizontal' or 'vertical'

Sets the layout mode to position items.

$('.grid').magnet({
  layoutMode: 'grid'
});

horizontal

Type: Boolean

Lays out items vertically instead of horizontally. Only for layoutMode: 'grid'.

$('.grid').magnet({
  horizontal: true
});
Hydrogenium 1 H 1.008
Helium 2 He 4.002
Lithium 3 Li 6.94
Beryllium 4 Be 9.012
Borium 5 B 10.81
Carbonium 6 C 12.011
Nitrogenium 7 N 14.007
Oxygenium 8 O 15.999
Fluorum 9 F 18.998
Neon 10 Ne 20.179
Natrium 11 Na 22.989
Magnesium 12 Mg 24.305
Aluminium 13 Al 26.981
Silicium 14 Si 28.085
Phosphorus 15 P 30.973
Sulphur 16 S 32.06
Chlorum 17 Cl 35.45
Argon 18 Ar 39.948
Kalium 19 K 39.098
Calcium 20 Ca 40.078
Scandium 21 Sc 44.955
Titanium 22 Ti 47.867
Vanadium 23 V 50.941
Chromium 24 Cr 51.996
Manganum 25 Mn 54.938
Ferrum 26 Fe 55.845
Cobaltum 27 Co 58.933
Niccolum 28 Ni 58.693
Cuprum 29 Cu 63.546
Zincum 30 Zn 65.38
Gallium 31 Ga 69.723
Germanium 32 Ge 72.63
Arsenicum 33 As 74.921
Selenium 34 Se 78.971
Bromum 35 Br 79.904
Krypton 36 Kr 83.798
//Both are valid values
.grid { height: 50%; }
.grid { height: 500px; }

Filtering

filter

Type: String or Function

Shows items that match the filter and hides items that don’t.

$('.grid').magnet({
  filter: '.selector'
});

Items can be filtered using a function. The function requires a parameter elem, which is the item element selector, and must return the data.

$('.grid').magnet({
  filter: function(elem) {
    //Get text from .symbol element
    var symbol = $(elem).find('.symbol').text();
    //Return elements with one character symbol
    return symbol.match(/^[A-Z]$/);
  }
});

Sorting

sortData

Type: Object

Gets data from the HTML. Object’s keys, are used as keywords to sort by. Object’s values, are a string or a function to retrieve data.

$('.grid').magnet({
  sortData: {
    category: '[data-category]',
    symbol: '.symbol'
  }
});

Any string wrapped in brackets, like [attribute], will be used to get the value of an attribute.

$('.grid').magnet({
  sortData: {
    //Use the value of the data-category attribute
    category: '[data-category]'
  }
});

Numeric values should be parsed to get a correct order. Add parser keywords to the keyword string to parse values.

$('.grid').magnet({
  sortData: {
    //Parse value as an integer
    number: '.number parseInt',
    //Parse value as a float
    weight: '.weight parseFloat'
  }
});

A function can be used as a value. This function is used to get data from each item. The function requires a parameter elem, which is the item element selector, and must return the data.

$('.grid').magnet({
  sortData: {
    weight: function(elem) {
      //Get text from .weight element
      var weight = $(elem).find('.weight').text();
      //Remove parenthesis and parse value as a float
      return parseFloat(weight.replace(/[()]/g, ''));
    }
  }
});

sortBy

Type: String or Array

Sorts items according to a property value. Value must match a key name in sortData.

$('.grid').magnet({
  sortData: {
    name: '.name'
  },
  sortBy: 'name'
});

There are two built-in sortBy options.

//Original order
$('.grid').magnet({
  sortBy: 'original'
});
//Random order
$('.grid').magnet({
  sortBy: 'random'
});

To sort by multiple properties, set sortBy to an array. Items will be sort by the first property, then by the next.

//Sort by category, then by symbol
$('.grid').magnet({
  sortBy: ['category', 'symbol']
});

Set to an object to set sort direction for each property.

$('.grid').magnet({
  sortAscending: {
    name: true,
    number: false,
    symbol: true,
    weight: false,
    category: true
  }
});

sortAscending

Type: Boolean Default: true

Sorts items ascending. Set sortAscending: false to sort descending.

$('.grid').magnet({
  sortAscending: true
});

Transitions

duration

Type: Number Default: 500

Items transition duration, in milliseconds.

$('.grid').magnet({
  duration: 250
});
Helium 2 He 4.002
Lithium 3 Li 6.94
Neon 10 Ne 20.179
Natrium 11 Na 22.989
Argon 18 Ar 39.948
Kalium 19 K 39.098
Krypton 36 Kr 83.798
Rubidium 37 Rb 85.467
Xenon 54 Xe 131.293
Caesium 55 Cs 132.905
Radon 86 Rn (222)
Francium 87 Fr (223)

easing

Type: String Default: 'ease'

Items transition effect speed curve.

$('.grid').magnet({
  easing: 'ease-in-out'
});
Helium 2 He 4.002
Lithium 3 Li 6.94
Neon 10 Ne 20.179
Natrium 11 Na 22.989
Argon 18 Ar 39.948
Kalium 19 K 39.098
Krypton 36 Kr 83.798
Rubidium 37 Rb 85.467
Xenon 54 Xe 131.293
Caesium 55 Cs 132.905
Radon 86 Rn (222)
Francium 87 Fr (223)

stagger

Type: Number Default: 0

Staggers items transitions, incrementally after one another.

$('.grid').magnet({
  stagger: 50
});
Helium 2 He 4.002
Lithium 3 Li 6.94
Neon 10 Ne 20.179
Natrium 11 Na 22.989
Argon 18 Ar 39.948
Kalium 19 K 39.098
Krypton 36 Kr 83.798
Rubidium 37 Rb 85.467
Xenon 54 Xe 131.293
Caesium 55 Cs 132.905
Radon 86 Rn (222)
Francium 87 Fr (223)

hiddenStyle

Type: Object Default: { opacity: 0, transform: 'scale(0.001)' }

Style applied to hide items.

$('.grid').magnet({
  hiddenStyle: { 
    opacity: 0
  },
  visibleStyle: { 
    opacity: 1
  }
});
Helium 2 He 4.002
Lithium 3 Li 6.94
Neon 10 Ne 20.179
Natrium 11 Na 22.989
Argon 18 Ar 39.948
Kalium 19 K 39.098
Krypton 36 Kr 83.798
Rubidium 37 Rb 85.467
Xenon 54 Xe 131.293
Caesium 55 Cs 132.905
Radon 86 Rn (222)
Francium 87 Fr (223)

visibleStyle

Type: Object Default: { opacity: 1, transform: 'scale(1)' }

Style applied to reveal hidden items.

$('.grid').magnet({
  hiddenStyle: { 
    opacity: 0
  },
  visibleStyle: { 
    opacity: 1
  }
});
Helium 2 He 4.002
Lithium 3 Li 6.94
Neon 10 Ne 20.179
Natrium 11 Na 22.989
Argon 18 Ar 39.948
Kalium 19 K 39.098
Krypton 36 Kr 83.798
Rubidium 37 Rb 85.467
Xenon 54 Xe 131.293
Caesium 55 Cs 132.905
Radon 86 Rn (222)
Francium 87 Fr (223)