快速简单实现查看更多功能

2019-06-12  本文已影响0人  果子煎饼同学

近期遇到需要根据屏幕宽度展示不同数量item,且需要收缩展开功能,记录一下

<!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">
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        li {
            list-style: none;
        }
        .wrap {
            width: 40vw;
            height: 40vh;
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            grid-template-rows: repeat(3, 1fr);
        }
        @media screen and (max-width: 800px) {
            .wrap .item:nth-of-type(n + 1) {
                display: none;
            }
        }
        @media screen and (max-width: 1500px) {
            .wrap .item:nth-of-type(n + 2) {
                display: none;
            }
        }
        @media screen and (max-width: 1800px) {
            .wrap .item:nth-of-type(n + 3) {
                display: none;
            }
        }
        .box .item {
            display: block!important;
        }
        .show {
            width: 100px;
            height: 50px;

        }
    </style>
</head>

<body>
    <ul class="wrap">
        <li class="item">1</li>
        <li class="item">2</li>
        <li class="item">3</li>
        <li class="item">4</li>
        <li class="item">5</li>
        <li class="item">6</li>
        <li class="item">7</li>
        <li class="item">8</li>
        <li class="item">9</li>
    </ul>
    <button class="show">显示更多</button>
</body>
<script>
    $('.show').click(()=> {
        if (!$('.wrap').hasClass('box')) {
            $('.wrap').addClass('box')
        } else {
            $('.wrap').removeClass('box')
        }
    })
</script>
</html>
上一篇 下一篇

猜你喜欢

热点阅读