CSS-如何制作动态的引导列表可滚动

发布于 2025-02-01 19:41:19 字数 2105 浏览 0 评论 0原文

我在VUE应用程序中有此代码,

            <form class="d-flex me-auto" id="searchBar">
                <div class="input-group">
                    <input class="form-control" type="text" placeholder="Cerca professionista" @keyup="search" aria-label="Search" v-model="searchInput">
                    <button class="btn btn-outline-success" @click.prevent="search">
                        <i class="fa-solid fa-search"></i>
                    </button>
                </div>
            </form>
            <div class="list-group" v-if="searchResults.length > 0" id="searchResultsList">
                <a href="#" class="list-group-item list-group-item-action" v-for="(result, index) in searchResults" :key="index" id="searchResult" @click.prevent="showUserProfile(result.username)">
                    <div class="d-flex w-100 justify-content-between">
                        <p class="ps-2 me-auto mb-1">{{ result.name }} {{ result.surname }}</p>
                        <!-- <img :src="result.propicUrl" id="searchResultThumbnail"> -->
                    </div>
                    <small class="ps-2 me-auto">{{ result.category }}</small>
                </a>
            </div>

我将有一个列表,其中包含搜索结果,因为有很多结果,如何使其可以滚动?目前,我添加了此CSS规则,但不起作用

<style lang="scss">
#menu {
    height: 75px;
    .navbar-brand {
        padding-top: 0;
        #logo {
            width: 60px;
            height: 60px;
        }
    }
    #searchBar {
        width: 420px;
    }
    #searchResultsList {
        position: absolute;
        top: 3.8em;
        left: 5.4em;
        width: 420px;
        overflow-y: scroll;
        #searchResult {
            overflow-y: scroll;
            #searchResultThumbnail {
                height: 40px;
                width: 40px;
                border-radius: 50%;
            }
        }
    }
}
</style>

I have this code in my vue app

            <form class="d-flex me-auto" id="searchBar">
                <div class="input-group">
                    <input class="form-control" type="text" placeholder="Cerca professionista" @keyup="search" aria-label="Search" v-model="searchInput">
                    <button class="btn btn-outline-success" @click.prevent="search">
                        <i class="fa-solid fa-search"></i>
                    </button>
                </div>
            </form>
            <div class="list-group" v-if="searchResults.length > 0" id="searchResultsList">
                <a href="#" class="list-group-item list-group-item-action" v-for="(result, index) in searchResults" :key="index" id="searchResult" @click.prevent="showUserProfile(result.username)">
                    <div class="d-flex w-100 justify-content-between">
                        <p class="ps-2 me-auto mb-1">{{ result.name }} {{ result.surname }}</p>
                        <!-- <img :src="result.propicUrl" id="searchResultThumbnail"> -->
                    </div>
                    <small class="ps-2 me-auto">{{ result.category }}</small>
                </a>
            </div>

I will have a list that will contain the results of a search, since there are a lot of results, how I can make it scrollable? At the moment I have added this css rules but not working

<style lang="scss">
#menu {
    height: 75px;
    .navbar-brand {
        padding-top: 0;
        #logo {
            width: 60px;
            height: 60px;
        }
    }
    #searchBar {
        width: 420px;
    }
    #searchResultsList {
        position: absolute;
        top: 3.8em;
        left: 5.4em;
        width: 420px;
        overflow-y: scroll;
        #searchResult {
            overflow-y: scroll;
            #searchResultThumbnail {
                height: 40px;
                width: 40px;
                border-radius: 50%;
            }
        }
    }
}
</style>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

海拔太高太耀眼 2025-02-08 19:41:19

您需要将高度max-height设置为导致具体大小的值(例如:40VW200px < /code>,20pt)用于溢出Y:自动|滚动有任何效果。

如果不设置(max-)高度,则该元素将增长以匹配其孩子的高度,并且永远不会显示活动的滚动栏。

旁注:考虑元素具有位置:绝对高度:100%可能会将高度请求委派给最近的定位的祖先但是,您确实需要一个带有 concrete concrete Height的位置祖先, Overfloflflow y :自动|滚动工作。

通用示例:

Vue.createApp().mount('#app')
#app {
  max-height: 100px;
  overflow-y: auto;
  border: 1px solid red;
}
<script src="https://unpkg.com/vue@next/dist/vue.global.prod.js"></script>
<div id="app">
  <div v-for="n in 20">test</div>
</div>


操场:

const { createApp, reactive, toRefs } = Vue;

createApp({
  setup: () => ({
    ...toRefs(reactive({
      setHeight: false,
      setMaxHeight: true,
      listLength: 20
    }))
  })
}).mount('#app')
#app {
  display: flex;
}

#app>* {
  flex: 0 0 50%;
}

.scroller {
  overflow-y: auto;
  border: 1px solid red;
}

.controls {
  padding: 0 1rem;
  display: flex;
  flex-direction: column;
}

* {
  box-sizing: border-box;
}
<script src="https://unpkg.com/vue@next/dist/vue.global.prod.js"></script>
<div id="app">
  <div class="scroller" :style="{ height: setHeight ? '100px' : '', maxHeight: setMaxHeight ? '100px': ''}">
    <div v-for="n in listLength">test</div>
  </div>
  <div class="controls">
    <div>
      Items: <input type="number" v-model="listLength">
    </div>
    <label>
      <input v-model="setHeight" type="checkbox">
      Set height
    </label>
    <label>
      <input v-model="setMaxHeight" type="checkbox">
      Set max height
    </label>
  </div>
</div>


设置高度Max-height之间的区别在于,即使没有足够的内容,元素即使它具有指定的高度也将具有指定的高度要填写它,而使用Max-Height,当您没有足够的内容时,该元素将一直缩小到0(除非其他东西给它一个软最小值:例如:flex容器)。

You need to set either height or max-height to a value that results in a concrete size (e.g: 40vw, 200px, 20pt) for overflow-y: auto | scroll to have any effect.

Without setting the (max-)height, the element will grow to match the height of its children and will never display an active scrollbar.

Side note: considering the element has position: absolute, height: 100% will likely delegate the height request to the closest positioned ancestor but, again, you do need a positioned ancestor with a concrete height for overflow-y: auto | scroll to work.

Generic example:

Vue.createApp().mount('#app')
#app {
  max-height: 100px;
  overflow-y: auto;
  border: 1px solid red;
}
<script src="https://unpkg.com/vue@next/dist/vue.global.prod.js"></script>
<div id="app">
  <div v-for="n in 20">test</div>
</div>


Playground:

const { createApp, reactive, toRefs } = Vue;

createApp({
  setup: () => ({
    ...toRefs(reactive({
      setHeight: false,
      setMaxHeight: true,
      listLength: 20
    }))
  })
}).mount('#app')
#app {
  display: flex;
}

#app>* {
  flex: 0 0 50%;
}

.scroller {
  overflow-y: auto;
  border: 1px solid red;
}

.controls {
  padding: 0 1rem;
  display: flex;
  flex-direction: column;
}

* {
  box-sizing: border-box;
}
<script src="https://unpkg.com/vue@next/dist/vue.global.prod.js"></script>
<div id="app">
  <div class="scroller" :style="{ height: setHeight ? '100px' : '', maxHeight: setMaxHeight ? '100px': ''}">
    <div v-for="n in listLength">test</div>
  </div>
  <div class="controls">
    <div>
      Items: <input type="number" v-model="listLength">
    </div>
    <label>
      <input v-model="setHeight" type="checkbox">
      Set height
    </label>
    <label>
      <input v-model="setMaxHeight" type="checkbox">
      Set max height
    </label>
  </div>
</div>


The difference between setting height and max-height is that with height the element will have the specified height even when it doesn't have enough content to fill it, while with max-height, when you don't have enough content, the element will shrink all the way down to 0 (unless something else gives it a soft min-height: e.g: a flex container).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文