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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="css/all.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.css">
    <script src="js/jquery-3.4.1.min.js"></script>
    <script src="js/all.js"></script>
    <title>offcanvas_menu</title>
</head>

<body>
    <div class="container">
        <div class="aside">
            <a href="#" class="mobile_close"><i class="fas fa-window-close"></i></a>
            <!-- 作為關閉選單用 -->
            <ul class="aside_menu">
                <li><a href="#">選單</a></li>
                <li><a href="#">選單</a></li>
                <li><a href="#">選單</a></li>
                <li><a href="#">選單</a></li>
            </ul>
        </div>
        <!-- aside end -->

        <div class="main">
            <!-- PC 顯示的目錄 -->
            <div class="header">
                <a href="#" class="mobile_open"><i class="fas fa-bars"></i></a>
                <!-- 作為手機版的側邊選單按鈕 -->
                <ul class="menu">
                    <li><a href="#">選單</a></li>
                    <li><a href="#">選單</a></li>
                    <li><a href="#">選單</a></li>
                    <li><a href="#">選單</a></li>
                </ul>
            </div>
            <div class="content">
                <h2>期間令人,在這另外。</h2>
                <p>就好隨意崇拜也在幾天專業上去材料不但禁止同時沒想到恐怖,本類告知體驗一週不在味道資料庫富岡包裝,和平上一頁,西部貨幣激烈國人採訪兒童取得反映衛生著名充分寂寞都有承擔,教程少女政策涉及出版社出來職務臺中太多規定各自我都還不,預計顏色未經出去城市,規格落實個。
                </p>
                <h2>期間令人,在這另外。</h2>
                <p>就好隨意崇拜也在幾天專業上去材料不但禁止同時沒想到恐怖,本類告知體驗一週不在味道資料庫富岡包裝,和平上一頁,西部貨幣激烈國人採訪兒童取得反映衛生著名充分寂寞都有承擔,教程少女政策涉及出版社出來職務臺中太多規定各自我都還不,預計顏色未經出去城市,規格落實個。
                </p>
                <h2>期間令人,在這另外。</h2>
                <p>就好隨意崇拜也在幾天專業上去材料不但禁止同時沒想到恐怖,本類告知體驗一週不在味道資料庫富岡包裝,和平上一頁,西部貨幣激烈國人採訪兒童取得反映衛生著名充分寂寞都有承擔,教程少女政策涉及出版社出來職務臺中太多規定各自我都還不,預計顏色未經出去城市,規格落實個。
                </p>
            </div>
        </div>

    </div>

    </div>
    </div>
</body>

</html>
              
            
!

CSS

              
                * {
  box-sizing: border-box;
}
@mixin pc {
  @media (max-width: 1024px) {
    @content;
  }
}

@mixin pad {
  @media (max-width: 768px) {
    @content;
  }
}

@mixin phone {
  @media (max-width: 375px) {
    @content;
  }
}

$font-m: 16px;
$font-l: $font-m * 1.5;
$font-s: $font-m * 0.8;

//變數設定

$primary-color:#ffbe0b;

body {
    background-color: #cfcfcf;
}

a {
    text-decoration: none;
    font-size: $font-m;
}

h2 {
    font-size: $font-l;
    font-weight: bold;
    padding-left: 16px;
    padding-top: 1em;
}

p {
    line-height: 1.8;
    padding: 1em;
}

//全域設定

.container {
    max-width: 1200px;
    margin: 0 auto;
    height: auto;
    overflow: hidden;
    position: relative;
}

.mobile_close,
.mobile_open {
    display: inline-block;
    // 使用 inline-block 才可以調整大小
    font-size: $font-m;
    padding: 1px;
    color: $primary-color;
    line-height: 50px;
}

// pad 螢幕以下 a 連結按鈕的共用設定(漢堡選單跟關閉)

.mobile_open {
    display: none;

    @include pad {
        display: inline-block;

    }
}

.fas {
    padding: 1em;
}

//pad 螢幕以下按鈕設定

.aside {
    overflow: hidden;
    // 在 PC 時隱藏
    width: 270px;
    background-color: #333;
    transform: translateX(-270px);
    // 在 PC 顯示下,往左邊推擠到 container 外
    position: absolute;
    top: 0;
    bottom: 0;
    // 將 aside 選單絕對定位在 container 上,並且使用上方與下方都為 0px ,靠左邊對齊,讓 header 可往上移動


    @include pad {
        display: block;
        border-right: 3px solid $primary-color;
        transition: all 0.5s;

        li {
            margin: 1em;
            // 在 pad 螢幕用 margin 推擠出 li 的空間

            a {
                display: block;
                // 在 pad 螢幕下屬性改成 block 佔滿整行
                color: $primary-color;
                border-bottom: 1px dashed $primary-color;
                padding: 1em;

                &:hover {
                    background-color: $primary-color;
                    color: #333;
                    transition: 0.3s;
                }
            }
        }
    }

}
.main{
    transform: translateX(0px);
    transition: all 0.5s;
    // 讓 aside 在收闔時,可以同步收闔
}
.header {
    background-color: #333;
    height: 50px;
    border-bottom: 3px solid $primary-color;

    .menu {
        float: right;
        margin-right: 1em;

        li {
            float: left;

            a {
                color: $primary-color;
                font-size: $font-m;
                padding-right: 1em;
                padding-left: 1em;
                line-height: 50px;

                &:hover {
                    background-color: $primary-color;
                    padding-top: 17px;
                    padding-bottom: 15px;
                    color: #333;
                    transition: all 0.3s;
                    // 滑鼠移過去有漸變效果
                }
            }
        }
    }

    @include pad {
        .menu {
            display: none;
            // 在 pad 螢幕以下會不顯示選單
            transition: all 0.5s;

            li {
                float: left;

                a {
                    display: block;
                    // pad 螢幕以下 aside 選單內的 a 連結顯示占滿整行
                    color: $primary-color;
                    line-height: 50px;
                    border-bottom: 1px dashed $primary-color;
                    transition: all 0.5s;

                    &:hover {
                        background-color: $primary-color;
                        color: #333;
                    }
                }
            }
        }
    }
}

// 透過 jQuery 增加 class open 觸發選單
.open {
    .main {
        transform: translateX(270px);
        // 當觸發 .open 時,main 會跳出選單
        transition: all 0.5s;
    }

    .aside {
        transform: translateX(0);
        // 當觸發 .open 時, aside 會打開
        transition: all 0.5s;
    }
}
              
            
!

JS

              
                $(document).ready(function () {
    $('.mobile_open').click(function (e) {
        $('body').addClass('open');
        // 用 click 監聽 .mobile_open,當 click 觸發功能,會把 body 中的側邊選單打開       
    });

    $('.mobile_close').click(function (e) {
        $('body').removeClass('open')
        //用 click 監聽 .mobile_close,當 click 觸發功能,會把 body 中的側邊選單關閉

    });
});

//使用 JS 增加 class 名稱,不需要加上 .class
              
            
!
999px

Console