使用 media 实现响应式布局

前端开发 作者: 2024-08-20 06:40:02
最近工作有一个需求是将一个界面改为响应式布局,由于UI还没有给设计,于是自己先查了一下资料做了一个demo。其实实现响应式布局的方式有很多,利用media实现就是其中一种,但是他也有一些缺点,比如说要

原理

@media screen and (max-width:768px){
    body{
        background-color: black
    }
}
描述
all 用于所有设备
print 用于打印机或打印预览
screen 用于电脑屏幕、平板电脑、智能手机等
speech 用于屏幕阅读器等发声设备

应用

<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)">
/* 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 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,.fourth {
    float: none;
    width: 100%;
}

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

a {
    display: none
}
/* index_ipad.css */
.first,.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',false);
}
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_65374.html