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

              
                <section id="one" class="l-wrap">
  <h2>下にスクロールすると<br />スクロールに合わせて<br />流れるテキストが出てきます</h2>
 <p>↓</p>
 <p>↓</p>
</section>
<section id="two" class="l-wrap">
<div id="scrolling-text" class="l-heading-a scrolling-text en">Move left and right with scrolling Move left and right with scrolling Move left and right with scrolling Move left and right with scrolling Move left and right with scrolling Move left and right with scrolling</div>
  <h2>"position: fixed"で<br />固定することもできます</h2>
</section>
              
            
!

CSS

              
                .l-wrap {
  position: relative;
  height: 500px;
  padding: 48px 0;
}

h2 ,p {
  padding-top: 48px;
  line-height: 1.5;
  text-align: center;
  font-size: 32px;
  font-weight: bold;
}

#one {
  background-color: #fff000;
}

#two {
  background-color: #ffdddd;
}

.scrolling-text {
    position: absolute;
    top: 0;
    left: 0;
    transform: translate(-100%, -50%);
    white-space: nowrap;
    margin-bottom: 196px;
    line-height: 1;
    font-size: 64px;
    font-family: 'Ubuntu', sans-serif;
    transition: opacity 1s ease-out;
}

              
            
!

JS

              
                
$(function() {
    const text = document.getElementById("scrolling-text");

    // 初期位置設定
    let direction = "left";
    text.style.top = "0";
    text.style.left = "-200%"; // 初期位置を指定

    window.addEventListener("load", () => {
        // フェードイン
        setTimeout(() => {
            text.style.opacity = 0.6;
        }, 100);
    });

    let currentPosition = 0;
    let lastScrollY = 0;
    let isScrolling = false;

    function updatePosition() {
        const newPosition = window.scrollY * (direction === "left" ? -1.5 : 1.5) / 2;
        currentPosition = newPosition;
        text.style.transform = `translateX(${newPosition}px)`;
    }

    function applyInertia() {
        isScrolling = false;
    }

    function onScroll() {
        const scrollY = window.scrollY || window.pageYOffset;

        if (scrollY === 0) {
            direction = "left";
            text.style.transform = "translateY(-100%, -50%)";
        } else {
            direction = "right";
            text.style.transform = "translate(-100%, -50%)";
        }

        if (!isScrolling) {
            isScrolling = true;
            requestAnimationFrame(() => {
                updatePosition();
                isScrolling = false;
            });
        }

        lastScrollY = scrollY;
    }

    window.addEventListener("scroll", onScroll);

    // 初期表示時にテキストを表示する
    text.style.display = "block";
});

              
            
!
999px

Console