Examples of two ways to implement a horizontal scroll bar

Examples of two ways to implement a horizontal scroll bar

Preface:

During the project development, we encountered a requirement to implement a horizontal scroll bar when there are too many navigation bars on one line. When I started working on the project, the time given was too short and I was in a hurry. I thought there were ready-made ones on the Internet, but when I searched, I found none, so I had to write it myself. In the beginning, I used ordinary CSS+JS to implement the functions. Later, I learned flex layout, so I thought of using flex to implement the horizontal scroll bar. These two methods are recorded for future reference.

text:

Both methods have their own advantages. If you don't consider compatibility issues, you should use flex. After all, I still like the saying: Write Less, Do More. Ha ha

html:

<div class="nav_wrap">
    <ul class="nav_mine">
        <li class="nav_item">All</li>
        <li class="nav_item">Adobe</li>
        <li class="nav_item">Microsoft</li>
        <li class="nav_item">Accounting</li>
        <li class="nav_item">Painting</li>
        <li class="nav_item">Adobe</li>
        <li class="nav_item">Microsoft</li>
        <li class="nav_item">Accounting</li>
        <li class="nav_item">Painting</li>
    </ul>
</div>
<script src="node_modules/jQuery/tmp/jquery.js"></script>

A raw css + jquery to achieve horizontal scroll bar (native js can be achieved, jQuery is used for convenience)

CSS:

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0
}
.nav_wrap{
    overflow-x: scroll;
}
.nav_mine {
    padding: 15px 10px;
    border-bottom: 1px solid #aca9a7;
    height: 75px;
    overflow-x: scroll;
    overflow-y: hidden;
}

.nav_mine .nav_item {
    border: 1px solid #1a110b;
    border-radius: 40px;
    color: #aca9a7;
    margin-right: 10px;
    font-size: 24px;
    padding: 4px 18px;
    float: left;
    list-style: none;
}

js code:

$(function(){
    var width = 0;
    for (let i = 0; i < $('.nav_item').length; i++) {
        width += $('.nav_item').eq(i).outerWidth(true);
    }
    $('.nav_mine').width(width+20); //width is only the width of the content, the width of the padding needs to be added})

PS: The reason why js is used is because we don’t know how many tabs there are, so the width cannot be hard-coded. We can only dynamically obtain the width of the tabs, and then add them up to get the total width, which is convenient for multiple use. outerWidth plus the parameter true means the width includes padding+margin+border.

2. CSS3 -- flex

css:

* {
    box-sizing: border-box;
}

.nav_mine {
    padding: 15px 20px;
    border-bottom: 1px solid #aca9a7;
    height: 75px;
    display: flex;
    align-items: center;
    overflow-y: hidden;
    flex-wrap: nowrap;
}

.nav_mine .nav_item {
    border: 1px solid #aca9a7;
    border-radius: 40px;
    color: #aca9a7;
    margin-right: 22px;
    font-size: 24px;
    padding: 4px 18px;
    list-style: none;
    white-space: nowrap;
}

For white-space, when item does not use white-space:nowrap, I found a problem. When the width is not set, a word will not wrap, but Chinese characters will wrap. I thought it was related to display:flex. After checking online, I found out that white-space looks at the space to identify whether to wrap, and the word is regarded as a character. Therefore, for both Chinese characters and English, white-space:nowrap should be set to prevent line wrap. Because Chinese characters and English characters are different, the widths they occupy are inconsistent, so 1 to 2 pixels should be reserved.

I put the corresponding notes and examples on GitHub, https://github.com/sqh17/notes (local download). If you want to practice, you can clone it.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

<<:  MySQL query tree structure method

>>:  Detailed explanation of HTML area tag

Recommend

How to install MySQL 5.7.29 with one click using shell script

This article refers to the work of 51CTO blog aut...

How to analyze SQL execution plan in MySQL through EXPLAIN

Preface In MySQL, we can use the EXPLAIN command ...

JavaScript selector functions querySelector and querySelectorAll

Table of contents 1. querySelector queries a sing...

Upgrading Windows Server 2008R2 File Server to Windows Server 2016

The user organization has two Windows Server 2008...

How to change the terminal to a beautiful command line prompt in Ubuntu 18

I reinstalled VMware and Ubuntu, but the command ...

Mysql command line mode access operation mysql database operation

Usage Environment In cmd mode, enter mysql --vers...

Summary of vue's webpack -v error solution

Xiaobai learned about Vue, then learned about web...

Detailed explanation based on event bubbling, event capture and event delegation

Event bubbling, event capturing, and event delega...

Introduction to the method attribute of the Form form in HTML

1 method is a property that specifies how data is ...

Tutorial on installing MySQL 8.0.11 under Linux

1. Go to the official website to download the ins...

CentOS 8 is now available

CentOS 8 is now available! CentOS 8 and RedHat En...

Discussion on the numerical limit of the ol element in the html document

Generally speaking, it is unlikely that you will ...

JavaScript imitates Jingdong magnifying glass special effects

This article shares the specific code of JavaScri...

WeChat Mini Program video barrage position random

This article shares the specific code for randomi...