CSS横向滚动条实现

简介: CSS横向滚动条实现

需求:当从后端请求多个元素的时候,固定在一个div里面,若是超过div,会出现横向滚动条,限制换行

1:在父级元素中使用:white-space: nowrap,使父级元素中的内容不换行

2:在子级元素中使用:display: inline-block,使自己元素变成行内块元素


例子:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>div横向滚动条</title>
        <style>
            .healthName {
                width: 900px;
                height: 100px;
                overflow-x: scroll;
                overflow-y: hidden;
                white-space: nowrap;/* 父级元素中的内容不换行 */
            }
            .healthlist {
                display: inline-block;
            }
        </style>
    </head>
    <body>
        <div class="col-md-6  col-sm-6  col-xs-6  healthName">
            <!-- <div class="healthlist">
        <img class="img"  src="images/point.png"/>
        <p>张三丰</p>
        <p>89bpm</p>
        <p>120/78</p>
        </div> -->
        </div>
    </body>
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
    <script>
        $.ajax({
            url: "health.json",
            data: {},
            type: 'GET',
            success: function(data) {
                var html = "";
                $.each(data, function(i, item) {
                    html += "   <div class='healthlist'>";
                    html += "<img class='img'  src=" + item.image1 + "/>";
                    html += "<p>" + item.doneNum + "</p>";
                    html += "<p>" + item.date + "</p>";
                    html += "<p>" + item.date + "</p></div>";
                })
                $(".healthName").append(html)
            }
        });
    </script>
</html>

json:

[
    {
        "image1": "images/fence/fence1.png",
        "doneNum": 130,
        "date": "120/78"
    }, {
        "image1": "images/fence/fence1.png",
        "doneNum": 80,
        "date": "120/78"
    }, {
        "image1": "images/fence/fence1.png",
        "doneNum": 90,
        "date": "120/78"
    }, {
        "image1": "images/fence/fence1.png",
        "doneNum": 90,
        "date": "120/78"
    }, {
        "image1": "images/fence/fence1.png",
        "doneNum": 90,
        "date": "120/78"
    }, {
        "image1": "images/fence/fence1.png",
        "doneNum": 90,
        "date": "120/78"
    }, {
        "image1": "images/fence/fence1.png",
        "doneNum": 90,
        "date": "120/78"
    }, {
        "image1": "images/fence/fence1.png",
        "doneNum": 90,
        "date": "120/78"
    }, {
        "image1": "images/fence/fence1.png",
        "doneNum": 90,
        "date": "120/78"
    }, {
        "image1": "images/fence/fence1.png",
        "doneNum": 90,
        "date": "120/78"
    }, {
        "image1": "images/fence/fence1.png",
        "doneNum": 90,
        "date": "120/78"
    }, {
        "image1": "images/fence/fence1.png",
        "doneNum": 90,
        "date": "120/78"
    }, {
        "image1": "images/fence/fence1.png",
        "doneNum": 90,
        "date": "120/78"
    }, {
        "image1": "images/fence/fence1.png",
        "doneNum": 90,
        "date": "120/78"
    }, {
        "image1": "images/fence/fence1.png",
        "doneNum": 90,
        "date": "120/78"
    }, {
        "image1": "images/fence/fence1.png",
        "doneNum": 90,
        "date": "120/78"
    }, {
        "image1": "images/fence/fence1.png",
        "doneNum": 90,
        "date": "120/78"
    }, {
        "image1": "images/fence/fence1.png",
        "doneNum": 110,
        "date": "120/78"
    }, {
        "image1": "images/fence/fence1.png",
        "doneNum": 52,
        "date": "120/78"
    }
]
相关文章
|
7月前
|
前端开发
CSS 滚动条样式修改
CSS 滚动条样式修改
78 0
|
前端开发
CSS div隐藏滚动条
CSS div隐藏滚动条
70 0
|
7月前
|
容器
mui.css 滚动条消失 导致超出部分无法显示 overflow属性
mui.css 滚动条消失 导致超出部分无法显示 overflow属性
68 0
|
4月前
|
Web App开发 前端开发 iOS开发
如何使用CSS样式化滚动条
如何使用CSS样式化滚动条
31 0
|
5月前
|
前端开发
【UI】 elementui的dialog弹窗打开时CSS的BUG | 滚动条消失bug
【UI】 elementui的dialog弹窗打开时CSS的BUG | 滚动条消失bug
180 0
|
6月前
|
前端开发
CSS实现将垂直滚动条放置在左侧方法
CSS实现将垂直滚动条放置在左侧方法
190 2
|
7月前
|
前端开发 容器
如何用css给网页添加滚动条
如何用css给网页添加滚动条
103 2
|
7月前
|
前端开发 小程序
css div添加滚动条(附加源码)
css div添加滚动条(附加源码)
85 0
|
7月前
|
前端开发
CSS 隐藏滚动条
CSS 隐藏滚动条
49 0
|
JSON 前端开发 数据格式
CSS横向滚动条自定义样式
CSS横向滚动条自定义样式
218 0