Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                .wrapper
    button.button(data-key="65" class="key") A
    button.button(data-key="66" class="key") B
    button.button(data-key="67" class="key") C

    audio(data-key="65" src="https://pjchender.github.io/JavaScript30/01%20-%20JavaScript%20Drum%20Kit/sounds/boom.wav")
    audio(data-key="66" src="https://pjchender.github.io/JavaScript30/01%20-%20JavaScript%20Drum%20Kit/sounds/clap.wav")
    audio(data-key="67" src="https://pjchender.github.io/JavaScript30/01%20-%20JavaScript%20Drum%20Kit/sounds/hihat.wav")
              
            
!

CSS

              
                *
    // border: 1px solid
html, body
    height: 100%
.wrapper
    height: 100%
    display: flex
    justify-content: center
    align-items: center
.button
    margin: 0 10px
    width: 50px 
    height: 50px
    border-style: none
    border-radius: 3px
    transition: 0.07s
    &:hover
        background-color: #CCC
.playing
    border: 3px solid #FFC600
    transform: scale(1.5)
              
            
!

JS

              
                //  播放音效的函式
let playSound = function(e) {
    const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`);
    const key = document.querySelector(`.key[data-key="${e.keyCode}"]`); //  取得按鍵對應的元素
    if (!audio) return; //  如果沒有按到對應的按鍵,則停止此函式
    audio.currentTime = 0; //  讓每次回到音效檔的起始點
    audio.play(); //  播放元素的音效
    key.classList.add('playing'); ///為該元素增加 class
}

//  移除 class 的函式
let removeTransition = function(e) {
    if (e.propertyName !== 'transform') return; //    省略掉其他 propertyName 不是 transform 的物件
    this.classList.remove('playing'); //  把 playing 這個 class 移除
}

// 監聽每一個按鍵是否有 transitionend 事件
let listenTransitionEnd = function() {
    const keys = document.querySelectorAll('.key'); //  選出所有具有 .key 的元素,回傳成陣列
    keys.forEach((key) => {
        key.addEventListener('transitionend', removeTransition)
    }); //  利用 forEach 方法讀取 NodeList 並為每一個元素加上 transitionend 事件,事件執行時會促發 removeTransition 
};

listenTransitionEnd();
window.addEventListener('keydown', playSound);

// console.log('========');
              
            
!
999px

Console