enquire.js 플러그인을 사용하여 브라우저 너비에 따라 DOM의 요소 위치를 이동시키는 데이터 속성으로 저장된 미디어 쿼리를 구문 분석합니다.요소를 다시 원래 위치로 이동
그래서 수동으로 불일치를 지정할 때 잘 작동하는 다음을 작성했습니다. 문제는 입니다. 원래 위치를 기억하고 요소가 미디어 쿼리 밖에있을 때 요소를 자동으로 뒤로 이동 시키길 원합니다.
그러나 이전에 참조했던 prev/next 요소도 이동하는 문제가 있습니다. 요소를 원래 위치로 되돌릴 수있는 또 다른 방법이 있습니까? 아니면 요소를 이동하는 대신 요소를 숨기는 것이 좋습니다.
/*
* 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
내가 통해 UR 코드에 경고를 많이 볼 수 있습니다, 그 문제를 해결하시기 바랍니다 ..! –
jsbin에서 쓸모없는 오류가 많이 발생하지만 대신 codepen을 사용해보십시오. http://codepen.io/anon/pen/xBown –
@ John Magnolia : 멋지다 .. !! –