div태그의 사이즈가 변경되는 이벤트를 처리하기
div태그는 일종의 랩퍼이어서 그 영역사이즈가 변해도 정확한 그 사이즈가 얻어지지 못합니다.
다음의 라이브러리를 이용하면 해결됩니다.
[code]
(function () {
var attachEvent = document.attachEvent,
stylesCreated = false;
if (!attachEvent) {
var requestFrame = (function(){
var raf = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame ||
function(fn){ return window.setTimeout(fn, 20); };
return function(fn){ return raf(fn); };
})();
var cancelFrame = (function(){
var cancel = window.cancelAnimationFrame || window.mozCancelAnimationFrame || window.webkitCancelAnimationFrame ||
window.clearTimeout;
return function(id){ return cancel(id); };
})();
function resetTriggers(element){
var triggers = element.__resizeTriggers__,
expand = triggers.firstElementChild,
contract = triggers.lastElementChild,
expandChild = expand.firstElementChild;
contract.scrollLeft = contract.scrollWidth;
contract.scrollTop = contract.scrollHeight;
expandChild.style.width = expand.offsetWidth + 1 + 'px';
expandChild.style.height = expand.offsetHeight + 1 + 'px';
expand.scrollLeft = expand.scrollWidth;
expand.scrollTop = expand.scrollHeight;
};
function checkTriggers(element){
return element.offsetWidth != element.__resizeLast__.width ||
element.offsetHeight != element.__resizeLast__.height;
}
function scrollListener(e){
var element = this;
resetTriggers(this);
if (this.__resizeRAF__) cancelFrame(this.__resizeRAF__);
this.__resizeRAF__ = requestFrame(function(){
if (checkTriggers(element)) {
element.__resizeLast__.width = element.offsetWidth;
element.__resizeLast__.height = element.offsetHeight;
element.__resizeListeners__.forEach(function(fn){
fn.call(element, e);
});
}
});
};
/* Detect CSS Animations support to detect element display/re-attach */
var animation = false,
animationstring = 'animation',
keyframeprefix = '',
animationstartevent = 'animationstart',
domPrefixes = 'Webkit Moz O ms'.split(' '),
startEvents = 'webkitAnimationStart animationstart oAnimationStart MSAnimationStart'.split(' '),
pfx = '';
{
var elm = document.createElement('fakeelement');
if( elm.style.animationName !== undefined ) { animation = true; }
if( animation === false ) {
for( var i = 0; i < domPrefixes.length; i++ ) {
if( elm.style[ domPrefixes[i] + 'AnimationName' ] !== undefined ) {
pfx = domPrefixes[ i ];
animationstring = pfx + 'Animation';
keyframeprefix = '-' + pfx.toLowerCase() + '-';
animationstartevent = startEvents[ i ];
animation = true;
break;
}
}
}
}
var animationName = 'resizeanim';
var animationKeyframes = '@' + keyframeprefix + 'keyframes ' + animationName + ' { from { opacity: 0; } to { opacity: 0; } } ';
var animationStyle = keyframeprefix + 'animation: 1ms ' + animationName + '; ';
}
function createStyles() {
if (!stylesCreated) {
//opacity:0 works around a chrome bug https://code.google.com/p/chromium/issues/detail?id=286360
var css = (animationKeyframes ? animationKeyframes : '') +
'.resize-triggers { ' + (animationStyle ? animationStyle : '') + 'visibility: hidden; opacity: 0; } ' +
'.resize-triggers, .resize-triggers > div, .contract-trigger:before { content: \" \"; display: block; position: absolute; top: 0; left: 0; height: 100%; width: 100%; overflow: hidden; } .resize-triggers > div { background: #eee; overflow: auto; } .contract-trigger:before { width: 200%; height: 200%; }',
head = document.head || document.getElementsByTagName('head')[0],
style = document.createElement('style');
style.type = 'text/css';
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
head.appendChild(style);
stylesCreated = true;
}
}
window.addResizeListener = function(element, fn){
if (attachEvent) element.attachEvent('onresize', fn);
else {
if (!element.__resizeTriggers__) {
if (getComputedStyle(element).position == 'static') element.style.position = 'relative';
createStyles();
element.__resizeLast__ = {};
element.__resizeListeners__ = [];
(element.__resizeTriggers__ = document.createElement('div')).className = 'resize-triggers';
element.__resizeTriggers__.innerHTML = '<div class="expand-trigger"><div></div></div>' +
'<div class="contract-trigger"></div>';
element.appendChild(element.__resizeTriggers__);
resetTriggers(element);
element.addEventListener('scroll', scrollListener, true);
/* Listen for a css animation to detect element display/re-attach */
animationstartevent && element.__resizeTriggers__.addEventListener(animationstartevent, function(e) {
if(e.animationName == animationName)
resetTriggers(element);
});
}
element.__resizeListeners__.push(fn);
}
};
window.removeResizeListener = function(element, fn){
if (attachEvent) element.detachEvent('onresize', fn);
else {
element.__resizeListeners__.splice(element.__resizeListeners__.indexOf(fn), 1);
if (!element.__resizeListeners__.length) {
element.removeEventListener('scroll', scrollListener);
element.__resizeTriggers__ = !element.removeChild(element.__resizeTriggers__);
}
}
}
})();
// 이용방법
// workspace_container라는 아이디를 가진 div태그의 사이즈 변경이벤트 발생시 처리
function onResize(e) {
console.log(e);
}
window.addResizeListener(document.getElementById('workspace_container'), onResize);
[/code]
게시판 목록
개발자팁
개발과 관련된 유용한 정보를 공유하세요.
질문은 QA에서 해주시기 바랍니다.
질문은 QA에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 4916 | 웹서버 | 6년 전 | 2861 | ||
| 4915 | PHP |
|
6년 전 | 2174 | |
| 4914 | 웹서버 | 6년 전 | 1888 | ||
| 4913 | JavaScript | 6년 전 | 2595 | ||
| 4912 | node.js | 6년 전 | 3706 | ||
| 4911 | 기타 | 6년 전 | 4188 | ||
| 4910 | 기타 | 6년 전 | 2273 | ||
| 4909 | 기타 | 6년 전 | 1977 | ||
| 4908 | 기타 | 6년 전 | 2009 | ||
| 4907 | Mobile | 7년 전 | 2280 | ||
| 4906 | JavaScript | 7년 전 | 2282 | ||
| 4905 | 기타 | 7년 전 | 2270 | ||
| 4904 | jQuery | 7년 전 | 2511 | ||
| 4903 | PHP | 7년 전 | 5223 | ||
| 4902 | jQuery | 7년 전 | 5127 | ||
| 4901 | 기타 | 7년 전 | 2703 | ||
| 4900 | MySQL | 7년 전 | 4110 | ||
| 4899 | 기타 | 7년 전 | 2264 | ||
| 4898 | 웹서버 | 7년 전 | 2446 | ||
| 4897 | MySQL | 7년 전 | 2198 | ||
| 4896 | MySQL | 7년 전 | 2632 | ||
| 4895 | JavaScript | 7년 전 | 9767 | ||
| 4894 | 웹서버 | 7년 전 | 2354 | ||
| 4893 | 기타 | 7년 전 | 8313 | ||
| 4892 | jQuery | 7년 전 | 5677 | ||
| 4891 | 기타 | 7년 전 | 2830 | ||
| 4890 | PHP | 7년 전 | 3413 | ||
| 4889 | JavaScript | 7년 전 | 6374 | ||
| 4888 | MySQL | 7년 전 | 3132 | ||
| 4887 | MySQL | 7년 전 | 2761 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기