vue 盒子水平滚动 (滚轮事件+轮播)

html:

<div class="Ptitle">
                    <div class="item-Total">
                        <el-button type="primary" icon="el-icon-arrow-left" @click="ldLeftChange"></el-button>
                        <div class="lb-box" id="ksBox" @mousewheel="ONMouseWheel($event)">
                            <div class="box3" id="lbBox" :style="'left:'+lbBox.left+'px'" ref="outsideWrap" >
                                <div :class="ldActive == i.id ? 'item-ld img' : 'item-ld'" v-for="i in ldList" :key="i.id"  @click="changeLd(i.id)">
                                    <el-tooltip class="item" effect="dark" :content="i.wzbm" placement="top-start">
                                        <div>{{ i.wzmc}}{{i.wzbm}}</div>
                                    </el-tooltip>
                                </div>
                            </div>
                        </div>
                        <el-button type="primary" @click="ldRightChange"><i class="el-icon-arrow-right el-icon--right"></i></el-button>
                    </div>
                </div>

css:

.Ptitle{
   height: 0.56rem;
   margin-bottom: .2rem;
   background: rgba(0,149,255,0.20);
   display: flex;
   justify-content: space-between;
   align-items: center;
}
.item-Total{
   width: 100%;
   height: 0.56rem;
   display: flex;
}
.lb-box {
    flex: 1;
    overflow: hidden;
    position: relative;
}
.box3 {
    position: absolute;
    top: 0;
    left: 0;
    padding: 0 .2rem;
    display: flex;
}
.item-ld{
    cursor: pointer;
    display: block;
    /* width: 1rem; */
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    padding:0 0.17rem;
    height: 100%;
    line-height: 0.56rem;
    font-size: .18rem;
    font-family: Microsoft YaHei, Microsoft YaHei-Regular;
    font-weight: 400;
    color: #fff;
    margin-right: .3rem;
    box-sizing: border-box;
}

 js:

// 上一页
        ldLeftChange(){
            if(this.lbBox.page == 1){
                return;
            }
            if(this.lbBox.pageNow<=1){
                console.log('没有上一页了')
                this.lbBox.left = 0;
                return;
            }else {
                this.lbBox.pageNow--;
                this.lbBox.left = this.lbBox.left + this.lbBox.domWidth;
            }
        },
        // 下一页
        ldRightChange(){
            if(this.lbBox.page == 1){
                return;
            }
            if((0-this.lbBox.left) >= this.lbBox.lbWidth -this.lbBox.domWidth){
                console.log('没有下一页了')
                return;
            }else {
                this.lbBox.pageNow++;
                if(this.lbBox.pageNow >= this.lbBox.page){
                    // 最后一页
                    this.lbBox.left = 0-(this.lbBox.lbWidth - this.lbBox.domWidth);
                }else {
                    this.lbBox.left = this.lbBox.left - this.lbBox.domWidth;
                }
            }
        },
        // 事件
        ONMouseWheel (e) {
            // wheelDelta的值为正(120.240...)则是鼠标向上;为负(-120,-240)则是向下。 
            // 因为要兼容浏览器,所以才写成 -e.wheelDelta || -e.deltaY.部分浏览器这个e.wheelDeltaY值是没有的可能是null,部分浏览器e.deltaY这个值是没有的可能是null,
            // console.log(e.wheelDelta,e.deltaY);//下 -120 100 上 120 -100
            if(this.lbBox.page == 1){
                return;
            }
            let maxLeft = (this.lbBox.lbWidth - this.lbBox.domWidth);
            if((0-this.lbBox.left +  (e.wheelDelta/2)) >= maxLeft && e.wheelDelta < 0){
                console.log('最大left')
                return;
            }
            if(this.lbBox.left +  (e.wheelDelta/2) >= 0 && e.wheelDelta > 0){
                this.lbBox.left = 0;
                console.log('最小left')
                return
            }
            this.lbBox.left = this.lbBox.left +  (e.wheelDelta/2);
            this.lbBox.pageNow = parseInt((0-this.lbBox.left)/this.lbBox.domWidth)+1;
        },

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值