使用 media 實現響應式布局


最近工作有一個需求是將一個界面改為響應式布局,由於UI還沒有給設計,於是自己先查了一下資料做了一個demo。其實實現響應式布局的方式有很多,利用media實現就是其中一種,但是他也有一些缺點,比如說要對特別的屏幕單獨定制樣式代碼。在我的代碼里面我把屏幕分為了三種,代表為iPhone、iPad、PC三種,分別對應着三種不同的樣式。

目前可以實現:

  • 根據界面大小自動調整布局
  • 界面寬度小到一定程度時會隱藏header,將其放到側拉欄中

效果圖如下(代碼會在下面全部放上來):

media使用

原理

media簡單來說就是一種查詢工具,加入說你想知道打開你網頁的屏幕寬度是768px的時候才使用這個樣式,這個時候你就可以這樣寫:

@media screen and (max-width:768px){
    body{
        background-color: black
    }
}

這個代碼的效果就是當前界面的寬度小於768px的時候,將網頁背景變成黑色。screen是用於電腦屏幕、平板電腦、智能手機等。對於@media的更多媒體類型如下:

描述
all 用於所有設備
print 用於打印機或打印預覽
screen 用於電腦屏幕、平板電腦、智能手機等
speech 用於屏幕閱讀器等發聲設備

在做響應式布局的時候我主要用到max-widthmin-width兩種屬性,min-width的作用於max-width的作用相反。

應用

<link rel="stylesheet" href="./index.css">
<link rel="stylesheet" href="./index_ipad.css" media="screen and (max-width:1200px)">
<link rel="stylesheet" href="./index_mobile.css" media="screen and (max-width:768px)">

由我的代碼可以得知我將頁面分為三種大小,分別為(1200, +∞),(768, 1200),(0, 768),這個分類我是參照bootstrap來分的。

首先引入index.css,這也是你的電腦打開時的默認樣式,當你的電腦寬度逐漸減小時,就會開始應用index_ipad.css這個樣式文件,在這個文件中並不是將index.css的樣式代碼全部重寫了一遍,而是把需要更改樣式的代碼做了編寫。

舉個例子,比如說我index.css中有四個方塊,默認布局是float布局,全部排在一行,但是當頁面寬度變為ipad大小是頁面方塊就會變成兩行,原理是改一下方塊的寬度。具體實現代碼如下:

/* index.css */
.board {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 200px;
    float: left;
    width: 25%;
    color: white
}

.first {
    background-color: #F44336
}

.second {
    background-color: #E91E63
}

.third {
    background-color: #9C27B0
}

.fourth {
    background-color: #009688
}
/* index_ipad.css */
.first,
.second,
.third,
.fourth {
    width: 50%;
}

側拉欄

側拉欄的原理其實並不難,就是先寫一個div,保持與header元素相同,然后再設置其left屬性,使其隱藏,通過js操作其left,將其顯示出來。

<div class="nav">
    <ul>
        <li>
            <a>第一個</a>
        </li>
        <li>
            <a>第二個</a>
        </li>
        <li>
            <a>第三個</a>
        </li>
    </ul>
</div>
.nav {
    position: absolute;
    z-index: 11;
    left: -10rem;
    top: 0;
    width: 10rem;
    height: 100%;
    background: #607D8B;
}
window.onload = function() {
    let btn = document.getElementsByClassName('menu')[0]
    let nav = document.getElementsByClassName('nav')[0]
    // 改變側拉欄狀態
    btn.addEventListener('click', function() {
        nav.style.left = nav.style.left == '-10rem' || nav.style.left.length == 0 ? 0 : '-10rem';
    }, false);
}

全部代碼

<!-- index.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">
    <title>響應式布局</title>

    <link rel="stylesheet" href="./index.css">
    <link rel="stylesheet" href="./index_ipad.css" media="screen and (max-width:1200px)">
    <link rel="stylesheet" href="./index_mobile.css" media="screen and (max-width:768px)">
    <script src="./index.js"></script>
</head>

<body>
    <div class="nav">
        <ul>
            <li>
                <a>第一個</a>
            </li>
            <li>
                <a>第二個</a>
            </li>
            <li>
                <a>第三個</a>
            </li>
        </ul>
    </div>
    <nav>
        <img src="./img/菜單.png" alt="菜單" class="menu">
        <a href="#">第一個</a>
        <a href="#">第二個</a>
        <a href="#">第三個</a>
    </nav>
    <div>
        <div class="board first">
            第一個
        </div>
        <div class="board second">
            第二個
        </div>
        <div class="board third">
            第三個
        </div>
        <div class="board fourth">
            第四個
        </div>
    </div>
</body>

</html>
/* index.css */
.board {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 200px;
    float: left;
    width: 25%;
    color: white
}

.first {
    background-color: #F44336
}

.second {
    background-color: #E91E63
}

.third {
    background-color: #9C27B0
}

.fourth {
    background-color: #009688
}

nav {
    background-color: #607D8B;
    text-align: right;
    height: 5vh;
    display: flex;
    align-items: center;
    justify-content: right
}

a {
    text-decoration-line: none;
    color: white;
    margin-right: 3%
}

.menu {
    width: 1.5rem;
    margin-left: 0px;
    display: none;
    cursor: pointer;
}

ul,
li {
    list-style: none;
    padding: 0;
    margin: 0;
}

.nav {
    position: absolute;
    z-index: 11;
    left: -10rem;
    top: 0;
    width: 10rem;
    height: 100%;
    background: #607D8B;
}

.nav {
    transition: left linear .1s;
}

.nav a {
    display: block;
    padding: 1em 0;
    border-bottom: 1px solid #888;
    font-size: 16px;
    color: #eee;
    text-align: center;
}

.nav li {
    cursor: pointer;
}
/* index_mobile.css */
.first,
.second,
.third,
.fourth {
    float: none;
    width: 100%;
}

.menu {
    display: block;
    margin-right: 2%;
}

a {
    display: none
}
/* index_ipad.css */
.first,
.second,
.third,
.fourth {
    width: 50%;
}

.menu {
    display: block;
    margin-right: 2%;
}

a {
    display: none
}
//index.js
window.onload = function() {
    let btn = document.getElementsByClassName('menu')[0]
    let nav = document.getElementsByClassName('nav')[0]
    btn.addEventListener('click', function() {
        nav.style.left = nav.style.left == '-10rem' || nav.style.left.length == 0 ? 0 : '-10rem';
    }, false);
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM