Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

Save Automatically?

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

              
                <div class="container">
    <div class="nav">
        <ul>
            <li class="active"></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>
    <div class="p01"></div>
    <div class="p02"></div>
    <div class="p03"></div>
    <div class="p04"></div>
    <div class="p05"></div>
</div>

<button id="btn">button</button>
              
            
!

CSS

              
                * {
    padding: 0;
    margin: 0;
}

body {
    overflow: hidden;
}
/*插入導覽列*/
.nav {
    position: fixed;
    top: 50%;
    right: 0;
}
.nav ul li {
    width: 10px;
    height: 10px;
    margin: 10px;
    background-color: #fff;
    box-shadow: 0px 0px 0px rgba(0, 0, 0, 0.5) inset;
    cursor: pointer;
    border-radius: 50%;
}
.nav ul li.active {
    background-color: darkcyan;
}

/*每頁顯示的圖片*/
.p01 {
    background-image: url(https://placeimg.com/1000/1000/animals);
    background-size: cover;
    height: 100vh;
}
.p02 {
    background-image: url(https://placeimg.com/1000/1000/arch);
    background-size: cover;
    height: 100vh;
}
.p03 {
    background-image: url(https://placeimg.com/1000/1000/nature);
    background-size: cover;
    height: 100vh;
}
.p04 {
    background-image: url(https://placeimg.com/1000/1000/people);
    background-size: cover;
    height: 100vh;
}
.p05 {
    background-image: url(https://placeimg.com/1000/1000/tech);
    background-size: cover;
    height: 100vh;
}

              
            
!

JS

              
                $(document).ready(function() {
    // 重新載入此頁面時,回到第一張圖,並將右方導覽列置中
    $('body, html').scrollTop(0);
    center();

    let numberOfListItem = $(".nav li").length;
    let currentItem = 1;
    let isMoving = 0;
    
    // 滑鼠滾輪捲動時,移動圖片
    $(window).mousewheel(debounce(e => scrollToCurrentItem(e), 100,true));
    function scrollToCurrentItem(e) {
        if (isMoving === 0) {
            isMoving = 1;
            if (e.deltaY < 0) {
                if (currentItem < numberOfListItem) {
                    currentItem++;
                }
            } else {
                if (currentItem > 1) {
                    currentItem--;
                }
            }
        }
        
        $("html, body").stop().animate({
                    scrollTop: $(".p0" + currentItem).offset().top
                },
                function() {
                    isMoving = 0;
                }
            );
    }

    //  點選右方導覽列時會到指定圖片
    for (var i = 0; i <= numberOfListItem; i++) {
        $(".nav li:eq(" + i + ")").click({id: i},
            function(e) {
                $(".nav li").removeClass('active');
                var page = e.data.id + 1;
                $("html,body").animate({
                    scrollTop: $(".p0" + page).offset().top
                });
                $(this).addClass('active');
                currentItem = e.data.id + 1;
            }
        );
    }

    //  縮放網頁時,重新將導覽列置中
    $(window).resize(function() {
        center();
    });

    //  計算導覽列垂直置中的高度
    function center() {
        var pos = $(window).height() / 2 - $(".nav").height() / 2;
        $(".nav").css("top", pos);
    }

    // 監控 window scroll 事件,用來改變右方導覽列 active 的項目
    $(window).scroll(function() {
        console.log('scroll', $(window).scrollTop());
        if (
            $(window).scrollTop() >= $(".p01").offset().top &&
            $(window).scrollTop() < $(".p02").offset().top
        ) {
            //除了被點擊到的游標,其他都恢復成原來的顏色
            $('.nav li').removeClass('active');
            $('.nav li:eq(0)').addClass('active');
        } else if (
            $(window).scrollTop() >= $(".p02").offset().top &&
            $(window).scrollTop() < $(".p03").offset().top
        ) {
            $('.nav li').removeClass('active');
            $('.nav li:eq(1)').addClass('active');
        } else if (
            $(window).scrollTop() >= $(".p03").offset().top &&
            $(window).scrollTop() < $(".p04").offset().top
        ) {
            $('.nav li').removeClass('active');
            $('.nav li:eq(2)').addClass('active');
        } else if (
            $(window).scrollTop() >= $(".p04").offset().top &&
            $(window).scrollTop() < $(".p05").offset().top
        ) {
            $('.nav li').removeClass('active');
            $('.nav li:eq(3)').addClass('active');
        } else if ($(window).scrollTop() >= $(".p05").offset().top) {
            $('.nav li').removeClass('active');
            $('.nav li:eq(4)').addClass('active');
        }
    });
});


/**
 * 將要 debounce 的函式代入 func 中,不用 invoke
 * wait,設定時間(毫秒)
 * immeditate 如果是 true 則在事件觸發時會立即執行,
 * 但事件結束時不會執行;如果是 false 則事件觸發時不會立即執行,
 * 但事件結束時會執行。
 **/
function debounce(func, wait, immediate) {
    var timeout;
    return function() {
        var context = this,
            args = arguments;
        var later = function() {
            timeout = null;
            if (!immediate) func.apply(context, args);
        };
        var callNow = immediate && !timeout;
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
        if (callNow) func.apply(context, args);
    };
}

              
            
!
999px

Console