媒體查詢實現頁面適配問題

通常的網絡頁面都會應對瀏覽器實際頁面大小去做一些頁面佈局的適配,今天小編通過一個demo來實現一個簡單的導航欄以及內容的適配,就是通過查詢頁面實際寬度來做一個響應式的佈局

一、視口

首先我們瞭解一下視口這個概念,視口在pc端就等於瀏覽器實際頁面寬度,移動端視口默認是廠家所定義的一個固定值。簡單來講媒體查詢是基於視口寬度去做的一個判斷,然後對應的去改變模塊樣式。

  <!-- 這樣的一行代碼會使手機端視口根據其手機實際寬度來定義視口大小 -->
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

二、媒體查詢demo

  • 語法
    這邊案例就代表視口寬度在830px到1024時對應發生的變化
@media (min-width: 830px) and (max-width: 1024px){
      /* 條件下樣式的修改 */
}
  • demo展示
    這邊小編直接展示一下基本的適配demo,然後代碼會放在最後面大家也可以看一下。
    demo說明: 一般的瀏覽器適當縮小,爲了更好的體驗會有一個佈局的適配。

全屏顯示:
全屏顯示
頁面適當縮小:
在這裏插入圖片描述
最後手機端的一個適配(列表欄收縮隱藏按鈕點擊顯示):
在這裏插入圖片描述
代碼如下:

<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="header.css">
    <title>適配pc與手機</title>
</head>
<body>
    <!-- 頭部導航欄 -->
    <div class="header">
        <div class="centre">
            <div class="logo">
                <a href="" style="display:block; padding: 30px 60px ;overflow: hidden; background-color: gray"></a>
            </div>
            <div class="nav-list" id="navList">
                <span class="line"></span>
                <span class="line"></span>
                <span class="line"></span>
            </div>
            <div class="navigation" id="navbox">
                <nav>
                    <ul>
                        <li class="active"><a href="">首頁</a></li>
                        <li><a href="">首頁</a></li>
                        <li><a href="">首頁</a></li>
                        <li><a href="">首頁</a></li>
                        <li><a href="">首頁</a></li>
                    </ul>
                </nav>
            </div>
        </div>
    </div>
    <div class="content">
        <article class="text">文章內容</article>
        <article class="text">文章內容</article>
        <article class="text">文章內容</article>
        <article class="text">文章內容</article>
        <article class="text">文章內容</article>
        <article class="text">文章內容</article>
        <article class="text">文章內容</article>
        <article class="text">文章內容</article>
    </div>
    <script type="text/javascript" src="adaptive.js"></script>
</body>
let navList = document.getElementById('navList');
let navbox = document.getElementById('navbox');
let choose = true;
navList.addEventListener('click',function(){
    if(choose){
        navbox.classList.add('navigationheight');
        choose = false;
    }
    else{
        navbox.classList.remove('navigationheight');
        choose = true;
    }
},false);

*{
    margin: 0;
    padding: 0;
}
a{
    margin: 0;
    padding: 0;
    border: 0;
    outline: 0;
    font-style: inherit;
    font-family: inherit;
    font-size: 100%;
    vertical-align: baseline;
    text-decoration: none;
    color: black;
}
article{
    display: block;
}
ol,ul{
    list-style: none;
}
/* 頭部整體 */
.header{
    height: 60px;
    width: 100%;
}
/* 頁面實體區域 */
.centre{
    overflow: hidden;
    width: 990px;
    margin: 0 auto;
}
/* 頁面logo */
.header .centre .logo{
    float: left;
}
.nav-list{
    margin-top: 26px;
    margin-right: 2px;  
    float: right;
    padding: 6px;
}
.line{
    margin: 2px 0;
    display: block;
    width: 12px;
    height: 2px;
    background-color: #6190e8;
}
/* nav欄 */
nav{
    float: left;
    padding-left: 50px;
}
nav ul li{
    float: left;
    height: 100%;
    line-height: 60px;
    font-size: 16px;
    margin: 0 10px;
    position: relative;
}
/* 導航列表上的3條藍色線條類 */
.nav-list{
    display: none;
}
nav ul li.active::before{
    content: ' ';
    position: absolute;
    height: 3px;
    width: 100%;
    background-color: #6190e8; 
}
nav ul li a{
    display: block;
    height: 100%;
    padding: 0 15px;
}
/* 內容區域 */
.content{
    width: 990px;
    margin: auto;
    display: flex; 
    flex-wrap: wrap; 
}
.text{
    width: 200px;
    height: 100px;
    background-color: skyblue;
    float: left;
    text-align: center;
    margin:0 15px 20px 0;
}

/* 像素小於1024px時改變 */
@media (min-width: 830px) and (max-width: 1024px){
    /* 中心區域自適應滿屏 */
    .centre{
        width: 880px;
        margin: 0 auto;
    }
    .content{
        width: 880px;
        margin: 0 auto;
    }
    .text{
        width: 250px;
        height: 200px;
    }
}

/* 手機端適配 */
@media (max-width: 829px){
    .content{
        display: block;
        width: 100%;
    }
    .text{
        float: none;
        width: auto;
        height: auto;
        line-height: 200px;
        margin: 0 0 10px 0;
    }
    .centre{
        width: 100%;
    }
    /* 顯示導航列表按鈕 */
    .nav-list{
        display: block;
    }
    /* 隱藏手機端nav欄需點擊打開 */
    .navigation{ 
        width: 100%;
        /* 高度設0溢出隱藏實現 */
        overflow: hidden;
        height: 0;
        position: absolute;
        margin-top: 60px;
        background-color: rgba(255, 255, 255, 0.5);
        transition: height .5s;
    }
    .navigationheight{
        height: 210px;
    }
    nav{
        padding: 0;
    }
    nav ul li{
        float: none;
        margin-bottom: 15px; 
        height: 30px;
        line-height: 30px;
    }
    nav ul li.active::before{
        content: ' ';
        height: 100%;
        width: 2px;
        background-color: #6190e8; 
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章