custom HTMLDivElement

HTMLDivElement¸¦ »ç¿ëÇؼ­ Ä¿½ºÅÒ ¿¤¸®¸ÕÆ®¸¦ ¸¸µé¾î º¸ÀÚ.
¾Õ¿¡¼­ Custom Element ( tutorial28.html )¿¡¼­ Ä¿½ºÅÒ ¿¤¸®¸ÕÆ®ÀÇ ±âº»ÀûÀÎ »ç¿ë¹ý¿¡ ´ëÇؼ­ ¹è¿ü´Ù.

¸¹ÀÌ »ç¿ëµÇ´Â div¸¦ ÀÌ¿ëÇؼ­ Ä¿½ºÅÒ ¿¤¸®¸ÕÆ®¸¦ ¸¸µé¾î º¸ÀÚ.

//HTML
<div is="scroll-effect-element" id="scrolltest_two"></div>

//SCRIPT
class ScrollEffectElement extends HTMLDivElement {
    constructor () {
        super ();
        this._title = null;
    }
    
    connectedCallback() {
        this.innerHTML = "<b>custom div element test!</b>";
    }    
    
    disconnectedCallback() {
    }

    //¼Ó¼ºÀ» »ç¿ëÇÏ´Â °æ¿ì¿¡ ´ÙÀ½ µÎÇÔ¼ö¸¦ Ãß°¡ ÇÑ´Ù.
    //observedAttributes, attributeChangedCallback
    static get observedAttributes () {
        return [ 'title', 'disabled'];
    }   
    
    attributeChangedCallback (name, oldValue, newValue) {
        if(name === 'disabled') {
            if(newValue !== oldValue) {
                if(newValue)
                    this.style.color = 'lightgray';
                else
                    this.style.color = 'black';
            }
        }
        console.log(newValue);
    }   
    
    get disabled () {
        return this.getAttribute('disabled');
    }
    
    set disabled (v) {
        this.setAttribute ( 'disabled', v);
    }    
}

customElements.define( 'scroll-effect-element', ScrollEffectElement, {extends: 'div'});

let list = document.getElementById('scrolltest_two');
list.setAttribute( 'title', 'this is scrollEffectElement' );
//list.setAttribute('disabled', 'true');
list.disabled = true;   //list.setAttribute¿Í °°Àº ±â´É
console.log('HTMLDivElement disabled = ' + list.disabled);

1. HTML Ãß°¡

<div is="scroll-effect-element" id="scrolltest_two"></div>

HTMLDivElement ¿ä¼Ò¸¦ »ó¼ÓÇؼ­ ¸¸µé¾ú±â ¶§¹®¿¡ "-"°¡ µé¾î À̸§À» is¿¡ ÁöÁ¤ÇÑ´Ù.

2. HTML ÁöÁ¤

customElements.define( 'scroll-effect-element', ScrollEffectElement, {extends: 'div'});

ÀÚ¹Ù½ºÅ©¸³Æ®¿¡¼­ customElements.defineÀ» ÁöÁ¤ÇÑ´Ù.

3. ScrollEffectElement

ÀÚ¹Ù½ºÅ©¸³Æ®¿¡¼­ Ŭ·¡½º¸¦ ¸¸µå´Â ¹æ¹ý¿¡ ´ëÇؼ­ ¼³¸íÇÑ´Ù.
´ÙÀ½ ÇÔ¼ö´Â ÇʼöÀûÀ¸·Î Ãß°¡ Çϴ°ÍÀÌ ÁÁ´Ù.

constructor(), connectedCallback(), disconnectedCallback()

¼Ó¼ºÀ» »ç¿ëÇÏ´Â °æ¿ì¿¡´Â ´ÙÀ½ µÎÇÔ¼ö¸¦ Ãß°¡ ÇÑ´Ù.

observedAttributes(), attributeChangedCallback()

observedAttributes ÇÔ¼ö·Î °¨½ÃÇÒ ¼Ó¼ºÀ» ÁöÁ¤ÇÏ°í, ¼Ó¼ºÀÌ ¹Ù²î¸é attributeChangedCallback ÇÔ¼ö°¡ È£ÃâµÈ´Ù.

4. ÇÊ¿äÇÑ ¼Ó¼ºÀ» Ãß°¡ÇÑ´Ù.

¿©±â¼­´Â title, disabled ¼Ó¼ºÀ» Ãß°¡ ÇÏ¿´´Ù.
title ¼Ó¼ºÀº Ãâ·ÂÇÒ ¹®ÀÚ¿­ ¼Ó¼ºÀÌ´Ù.
disabled ¼Ó¼ºÀº ¿ä¼ÒÀÇ enable, disable ¼Ó¼ºÀ» °ü¸® Çϱâ À§Çؼ­ Ãß°¡ÇÑ´Ù.

¼Ó¼ºÀ» Ãß°¡ÇÏ¸é ¿ä¼ÒÀÇ attribute·Î ´ÙÀ½°ú °°ÀÌ Ãß°¡ µÈ´Ù.



5. attributeChangedCallback¿¡¼­ ¼Ó¼º¿¡ ´ëÇÑ ¸í·ÉÀ» ó¸®ÇÑ´Ù.

¼Ó¼º¿¡ ´ëÇÑ ¸í·ÉÀ» set disabled¿¡¼­ ÇÏÁö ¾Ê°í  attributeChangedCallback¿¡¼­ ó¸®Çϸé Ŭ·¡½º ¸í·É ´ë½Å ¿ä¼ÒÀÇ setAttribute ¸í·ÉÀ¸·Îµµ Ŭ·¡½º¸¦ Á¦¾î ÇÒ¼ö ÀÖ´Ù.

let list = document.getElementById('scrolltest_two');
list.setAttribute('disabled', 'true');

        if(name === 'disabled') {
            if(newValue !== oldValue) {
                if(newValue === 'true')
                    this.style.color = 'lightgray';
                else
                    this.style.color = 'black';
            }
        }

´Ù¿î·Îµå : custom_htmldivelement.html