首页 > 其他分享 >html制作水平滚动容器

html制作水平滚动容器

时间:2023-02-09 18:02:04浏览次数:37  
标签:容器 滚动 center flex refs scollView relative html offset

  如上图所示,制作一个这样的水平容器,用来在点击的时候左右滑动元素,html代码如下,

<div style="display: flex;justify-content: space-between;background-color: #2D3653;">

    <div @click="left" style="
                              padding: 0 10px;display: flex;align-items: center;border-radius: 10px 0 0 10px;">
        <span style="color:#fff;font-size:20px;font-weight:bold;">&lt;</span>
    </div>

    <!--滑动区域-->
    <div class="div01">

        <div class="div02" ref="scollView">
            <div class="houseContainer">
                <div class="divOut" v-for="(item,index) in datas" :key="index" @click="chooseHouse(item,index)">
                    <img :src="require('@/assets/leatherFactory/p7.png')" alt=""
                         style="" class="img1" />


                    <div style="" class="divRight">
                        <label style="" class="label1">{{item.name}}</label>
                        <div style="" class="divMiddle">
                            <label style="" class="label2">{{item.value}}</label>
                            <label style="" class="label3">千克</label>
                        </div>
                    </div>

                </div>
            </div>


        </div>

    </div>



    <div @click="right" style="
                               padding: 0 10px;display: flex;align-items: center;border-radius: 10px 0 0 10px;">
        <span style="color:#fff;font-size:20px;font-weight:bold;">></span>
    </div>

</div>

  上述代码还不能实现图片效果,需要CSS,最主要是div01,div02和houseContainer的CSS, 

.div01{
    overflow: hidden;
    flex:1
}
.div02{
    position: relative;
    overflow-x: scroll; //让元素可以X轴滚动,如果是Hidden,则无法滚动
}
.div02::-webkit-scrollbar { //此处主要是为了去除chrome中展示滚动条,其他浏览器无效
    display: none; 
}

.houseContainer{
    display: flex;
    position: relative;
    align-items: center;
}

.divOut{
    border-radius: 10px;
    text-align: center;
    display: flex;
    justify-content: center;
    background-color: #7A6FF0;
    margin-right: 10px;
    position: relative;
    // opacity: 0.5;

    text-align: center;
    flex: 1;
    min-width: 19rem;

}

  接下来就是添加左右的点击事件,

left(){
    let offset = this.$refs.scollView.scrollLeft
    this.$refs.scollView.scroll({
        left: offset - 200,
            behavior: 'smooth'
    })
},
right(){
    let offset = this.$refs.scollView.scrollLeft
    this.$refs.scollView.scroll({
        left: offset + 200,
            behavior: 'smooth'
    })
}

 

标签:容器,滚动,center,flex,refs,scollView,relative,html,offset
From: https://www.cnblogs.com/weiyanei/p/17106538.html

相关文章

  • stl序列式容器的接口和实现
    vector容器的接口和实现: list容器的接口和实现:双向循环链,最后一个节点为空节点        deque容器的接口和实现:deque的构造和内存管理:push_bac......
  • PYTHON数据容器
    PYTHON数据容器一种可以容纳多份数据的数据类型,容纳的每一份数据称之为1个元素,每一个元素,可以使任意类型的数据数据容器根据特点的不同,分为五类:列表(list)、元组(tupl......
  • 如何实现页面刷新后不定位到之前的滚动位置?
    if(history.scrollRestoration){history.scrollRestoration='manual';}button.onclick=function(){if(history.scrollRestoration){histo......
  • STL-map/multimap容器
    简介:map中所有元素都是pairpair第一个元素为key键值,起到索引作用,第二个元素为value实质所有元素都会按照key键值自动排序本质:map/multimap属于关联式容器,底层结构使用二叉树......
  • Containerd 的 Bug 导致容器被重建!如何避免?
    作者简介邓宇星,SUSERancher中国区软件架构师,6年云原生领域经验,参与Rancher1.x到Rancher2.x版本迭代,目前负责RancherForopenEuler(RFO) 项目开发。最近我们关注......
  • Devops——基于容器化平台日志和监控系统
    摘要日志和监控是容器云平台系统最常见的必备组件,形象一点形容其原理就是咖啡和伴侣一样必须配套使用,让你的应用运行的更贴合用户满意的服务运营目标(SLO)。当容器技术被大量......
  • html全局异常捕获
    全局异常捕获:前端资源会出现加载失败问题,如何捕获了?图片加载失败,如何优化显示错误了?在vue中:<script>mounted(){//或者指定局部的:this.$refs.div1.addEventListene......
  • 直播软件app开发,CSS超出隐藏并且能滚动
    直播软件app开发,CSS超出隐藏并且能滚动实现CSS代码: height:500rpx;overflow-x:hidden;overflow-y:scroll;​效果图的代码:<!--豆豆明细弹窗--><viewclass="mxB......
  • 搭建直播平台,uniapp滚动条置顶实现
    搭建直播平台,uniapp滚动条置顶实现实现代码: uni.pageScrollTo({  scrollTop:0,  duration:300});​以上就是搭建直播平台,uniapp滚动条置顶实现,更多内容欢......
  • 云原生技术在容器方面的应用
    随着近几年云原生生态的不断壮大,众多企业纷纷开展了用云上云的工作,学习和了解云原生及容器技术对于现代工程师是必不可少的,本文主要为大家介绍云原生及其在容器方面的应用。......