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

              
                .hero
    h1 🔥WOAH!
              
            
!

CSS

              
                
html {
    color: black;
    font-family: sans-serif;
}

.hero {
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    color: black;
}

h1 {
    text-shadow: 10px 10px 0 rgba(0, 0, 0, 1);
    font-size: 100px;
}

              
            
!

JS

              
                const hero = document.querySelector(".hero");
const text = hero.querySelector("h1");

hero.addEventListener("mousemove", moveShadow);

const walk = 100;
function moveShadow(e) {
    /**
     * [Offset]
     * offsetLeft, offsetTop 是 Element 的屬性,指該 element 到 offsetParent 的距離。
     * offsetWidth, offsetHeight 是 Element 的屬性,指該 element 的寬高。
     * offsetX, offsetY 是 Event 的屬性,指滑鼠到外層容器的距離。
     **/

    /**
    * [陣列的解構賦值]
     * 等同於
     * const heroWidth = hero.offsetWidth
     * const heroHeight = hero.offsetHeight
     * let offsetXAfterAdjust = e.offsetX
     * let offsetYAfterAdjust = e.offsetY
     **/
    const { offsetWidth: heroWidth, offsetHeight: heroHeight } = hero;
    let { offsetX: offsetXAfterAdjust, offsetY: offsetYAfterAdjust } = e;
    /**
     * "this" 一直都會是 addEventListener 的對象(hero)
     * "e.target" 則會隨著觸發事件的不同而變
     **/
    if (this !== e.target) {
        // 當 this !== e.target ,意思就是指 e.target === <h1> 的時候

        /**
         * 因為 <h1> 的外層沒有設定 position,所以它的 offsetParent 一樣會是 <body>
         * 因此 e.target.offsetLeft 指的就是 <h1> 到 <body> 的距離。
         * 因為 offsetX, offsetY 是 event 的屬性,所以當它進入到 <h1> 時,offsetX 指的是該滑鼠到容器(h1)外層的距離,
         * 如果要取得滑鼠相對於 body 的座標(滑鼠 <-> body),我們就用 offestX(滑鼠 <-> h1) + e.target.offsetLeft(h1 <-> body)
         **/
        offsetXAfterAdjust = offsetXAfterAdjust + e.target.offsetLeft;
        offsetYAfterAdjust = offsetYAfterAdjust + e.target.offsetTop;
    }

    /**
     * (offsetXAfterAdjust / heroWidth) 可以取得當前滑鼠 X 在 heroWidth 的百分比,此時原點在畫面左上角
     * ((offsetXAfterAdjust / heroWidth)  * walk) - (walk / 2 ) 可以讓原點變成在畫面中心
     **/
    const xWalk = Math.round(offsetXAfterAdjust / heroWidth * walk) - walk / 2;
    const yWalk = Math.round(offsetYAfterAdjust / heroHeight * walk) - walk / 2;

    /**
     * 把 xWalk 和 yWalk 的值代入 Text Shadow 中
     * 記得每個單位都要加上 px
     **/
    text.style.textShadow = `${xWalk}px ${yWalk}px 10px rgba(130, 130, 130, 0.5)`;
}

              
            
!
999px

Console