This project is deprecated.
You can achieve most of the functionality with features like CSS Grid and grid areas.
Move elements on page depending on media query.
npm install kist-dochopper --save
bower install kist-dochopper --saveReturns: jQuery
Type: Object|String
Type: Array
Contains objects which define conditions where and when should specific element be moved when media query is matched or unmatched.
Arguments:
| Name | Type | Description |
|---|---|---|
into |
`String | Function |
condition |
String |
Media query condition on which upon content should hop. |
Additionaly, into can be:
-
String- value of[data-hop-from]attribute -
Function- return value which can beStringorjQuery(has one argument - original element on which Dochopper is activated) -
jQuery- jQuery element found inside document
Type: Function
Provides: Array|jQuery, Array|Object
Event: dochopperhop
Callback for when context switching is complete, either when entering or exiting media query.
Array for first and second argument will be returned on plugin initialization. Every array contains list of currently active conditions, first argument containing elements which had their content filled with initial content, and second argument containing list of media query objects which are currently activate and correspond to list of containing elements in first argument.
Every next condition matching or unmatching will return active jQuery element and media query object.
Type: String
Default: hop-conditions
data property which will be used to for optional setting of conditions (see options → conditions). If you provide data through this property, value should be valid JSON object.
Type: String
Default: hop-from
data property which will be used as name of the element in which will the content hop to.
Destroy plugin instance.
Retrigger hopping for current instance. Useful if hop-from element depends on some external conditions.
Set proper HTML markup.
<div class="hopper hopper-a">
<b>Content A</b>
</div>
<div class="hopper hopper-b" data-hop-conditions='[{"into":"hop3","media":"screen and (min-width:900px)"},{"into":"hop4","media":"screen and (min-width:1400px)"}]'>
<b>Content B</b>
</div>
<div data-hop-from="hop1"></div>
<div data-hop-from="hop2"></div>
<div data-hop-from="hop3"></div>
<div data-hop-from="hop4"></div>Initialize plugin.
$('.hopper-a').dochopper({
conditions: [
{
into: 'hop1',
media: 'screen and (min-width:1000px)'
},
{
into: $('.hop2'),
media: 'screen and (min-width:1200px)'
},
{
into: function ( el ) {
if ( $('body').hasClass('foo') ) {
return 'hop3';
} else {
return 'hop4';
}
},
media: 'screen and (min-width:1200px)'
}
],
hop: function ( element, media ) {
// Hop!
}
});
$('.hopper-b').dochopper();
$('.hopper-a, .hopper-b').on('dochopperhop', function ( e, element, media ) {
// Hop!
});Retrigger instance.
$('.hopper').dochopper('rehop');Destroy plugin instance.
$('.hopper').dochopper('destroy');It’s not unusual to experience FOUC (Flash Of Unmoved Content) because browser is waiting to render some other part of document and your hopping portion becomes blank and takes space where it shouldn’t be taking it.
To prevent this type of problems, you can use some simple CSS trickery to visually hide elements until they are ready. You have classes like .kist-Dochopper.is-ready applied when elements are switched to their correct places for current context and are ready to be displayed.
.hopper {
height:1px;
visibility:hidden;
}
.hopper.kist-Dochopper.is-ready {
height:auto;
visibility:visible;
}If you have delegated events on some elements which will at some point be switched to another place, those events won’t work as intended since now those elements are in the new place.
If you use combination of min- and max- media queries (e.g. screen and (min-width:100px) and (max-width:299px)), those media queries won’t be shown in inital list of media queries which where activated if the current viewport is larger than that media query condition. This is normal media query listener behavior.
Also, exiting that media query will trigger next matching query and the last ones which also match current viewport.
Tested in IE8+ and all modern browsers.
MIT © Ivan Nikolić