weixin_40431771
盐焗海苔
采纳率100%
2018-11-02 05:49

vue使用bscroll左右菜单联动,左侧栏目无法跟随右边滑动切换active

已采纳

因为公司后端用其他框架,所以都是用cdn引用js写在一个页面了,现在只能达到点击左侧栏目、右侧到对应的栏目位置,但是右侧滑动的时候,左边栏目不跟随切换active

是右边li的索引值没有传到左边去吗?但是左边可以获取右边的索引了,点击也可以联动

html代码

<div class="content">

    <!--左边-->
    <div class="menu-wrapper" ref="menuWrapper">
        <ul>
            <li v-for="(food,index) in foods" @click="menuClick(index,$event)" :class="{'active': activeIndex == index}">
                {{food.class}}
                <i class="num"></i>
            </li>
        </ul>
    </div>

    <!--右边-->
    <div class="foods-wrapper" id="wrapper" ref="foodsWrapper">
        <ul>
            <li v-for="(food,index) in foods" :key="food.id" class="food-list-hook">
                <div class="class-title">
                    {{food.class}}
                </div>
                <div v-for="food in food.list">
                    <div class="item">
                        <div class="itemleft" @click="menuShow(food)">
                            <div class="item-img">
                                <img :src="food.imgs" alt="">
                            </div>
                        </div>
                        <div class="itemright">
                            <div class="title">
                                {{food.name}}
                            </div>
                            <div class="price">
                                <i class="fa fa-cny"></i>
                                <span>
                                    {{food.price}}
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
            </li>
        </ul>
    </div>

</div>

js代码

new Vue({
    el: '#app',
    data() {
        return {
            foods: [],
            listHeight: [],
            foodsScrollY: 0
        }
    },

    created() {
        axios.get('data.json').then(response = >{
            this.foods = response.data.foods;
            this.$nextTick(() = >{
                this._initScroll(); // 初始化scroll
                this._calculateHeight(); // 初始化列表高度列表
            })
        });
    },
    computed: {
        activeIndex() {
            for (let i = 0; i < this.listHeight.length; i++) {
                let topHeight = this.listHeight[i] let bottomHeight = this.listHeight[i + 1]
                if (!bottomHeight || (this.foodsScrollY >= topHeight && this.foodsScrollY < bottomHeight)) {
                    return i
                }
            }
            return 0
        }
    },
    methods: {
        _initScroll() {
            this.menuWrapper = new BScroll(this.$refs.menuWrapper, {
                click: true
            });
            this.foodsScroll = new BScroll(this.$refs.foodsWrapper, {
                probeType: 3
            });
            // 监控滚动事件
            this.foodsScroll.on('scroll', (pos) = >{
                this.foodsScrollY = Math.abs(Math.round(pos.y))
            })

        },
        _calculateHeight() {
            let foodList = this.$refs.foodsWrapper.getElementsByClassName('food-list-hook');
            let height = 0 this.listHeight.push(height) for (let i = 0; i < foodList.length; i++) {
                let item = foodList[i] height += item.clientHeight this.listHeight.push(height)
            }
        },
        menuClick(index, event) {
            if (!event._constructed) {
                return
            }
            this.foodsScroll.scrollTo(0, -this.listHeight[index], 300)
        }
    }

});


  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • weixin_40431771 盐焗海苔 3年前

    找到问题了,需要模拟点击的动作,才能同步联动

    点赞 评论 复制链接分享
  • weixin_44983701 weixin_44983701 1年前

    :class="{'current':currentIndex === i}"

    但我获取到currentIndex一直都是0,但是写在computed内照理说内部数据变化,会跟着变化;并且会报一个【[Vue warn]: Error in render: "TypeError: Cannot read property '1' of undefined"

    found in

    ---> at src/components/goods/goods.vue】的错误

    点赞 评论 复制链接分享

相关推荐