Я использую плагин enquire.js для анализа медиа-запросов, хранящихся в качестве атрибутов данных, которые перемещают позицию элементов в DOM на основе ширины браузера.Переместить элемент в исходное положение
Итак, я написал следующее, которое отлично работает, когда вы вручную указываете unmatch, проблема связана с авто. Я хочу, чтобы это помнят исходную позицию и автоматически перемещает элемент назад, когда он выходит за пределы медиа-запроса.
Но у меня возникают проблемы, когда предыдущий/следующий элемент, который был первоначально ссылкой, также перемещается. Есть ли другой способ, чтобы я мог переместить элемент обратно в исходное положение? Или мне лучше просто скрывать элемент, а не перемещать его?
/*
* Organise elements using meta data
*
* @example:
* data-respond='{
* "query":"screen and (max-width:481px)",
* "match": {
* "target": "#page",
* "method": "appendTo"
* },
* "unmatch":{
* "target": "#footer",
* "method": "appendTo"
* }
* }'
*/
var $this,
data,
options,
allowedMethods = ['appendTo', 'prependTo', 'insertAfter', 'insertBefore'];
$("[data-respond]").each(function() {
$this = $(this),
data = $this.data("respond");
options = {};
// check we have object
/*if(typeof data !== 'object'){
data = eval(data);
}*/
if (data.match) {
if ($.inArray(data.match.method, allowedMethods) > -1) {
options.match = function() {
if (data.match.method == 'insertAfter') {
if ($this[0] == $(data.match.target).next()[0]) {
return;
}
}
if ($(data.match.target).length) {
$this[data.match.method](data.match.target);
}
}
}
} //match
if (data.unmatch) {
if (data.unmatch == 'auto') {
data.unmatch = {};
// a) insert after
if ($this.prev().length) {
data.unmatch.target = $this.prev();
data.unmatch.method = 'insertAfter';
} else if ($this.next().length) {
// c) insert before
data.unmatch.target = $this.next();
data.unmatch.method = 'insertBefore';
} else {
// d) append to parent
data.unmatch.target = $this.parent();
data.unmatch.method = 'appendTo';
}
if ($.inArray(data.unmatch.method, allowedMethods) > -1) {
options.unmatch = function() {
$this[data.unmatch.method](data.unmatch.target);
}
}
} else {
// Manually set unmatch
if ($.inArray(data.unmatch.method, allowedMethods) > -1) {
options.unmatch = function() {
if ($(data.unmatch.target).length) {
$this[data.unmatch.method](data.unmatch.target);
}
}
}
}
} //unmatch
enquire.register(data.query, options);
});
Jsbin - http://jsbin.com/akAV/1/edit
Я вижу много предупреждений в Уре код, когда я cheked в firbug, пожалуйста, исправить те ..! –
Много бесполезных ошибок от jsbin, попробуйте кодэп вместо: http://codepen.io/anon/pen/xBown –
@ John Magnolia: thats cool .. !! –