美文网首页
横向列表内点击某个元素滑动到视图中心

横向列表内点击某个元素滑动到视图中心

作者: 学无止境_cheer_up | 来源:发表于2024-04-15 00:46 被阅读0次

在微信小程序中,要实现点击某个元素后使另一个元素滚动到视图中间,可以按照以下步骤进行:

效果图: image.png

html

<scroll-view
                :scroll-x="true"
                :enhanced="true"
                id="scrollContainer"
                :scroll-with-animation="isAnimation"
                :scroll-left="targetScrollLeft"
                class="equity-list"
                @scroll="scrollMove"
            >
                <image
                    class="equity-item"
                    v-for="(item, index) in equityList"
                    :src="item?.headImage"
                    :key="index"
                    :id="`equity-${index}`"
                    :style="`opacity:${currentIndex === index ? 1 : 0.5} ;`"
                    :data-index="index"
                    @tap="handleChangeEquity(item, index)"
                />
            </scroll-view>

css

.equity-list {
            display: block;
            width: unset;
            white-space: nowrap;
            margin-top: 52px;

            .swiper-item1 {
                width: 144px;
                height: 136px;
            }

            &::-webkit-scrollbar {
                width: 0;
                height: 0;
                color: transparent;
            }

            .equity-item {
                width: 144px;
                height: 136px;
                margin-right: 36px;

                &:last-child {
                    margin-right: 0;
                }
            }
        }

js

/**
 * 动画标识
 */
isAnimation: boolean = false;

/**
 * 滑动距离
 */
targetScrollLeft;

/**
 * 移动信息
 */
    moveParams: any = {};

/**
 * 当前项坐标
 */
currentIndex: number = 0;

 /**
  * 页面初始化
  */
async init(option: Record<string, string>): Promise<void> {
        super.init(option);
        if (option?.id && option?.id !== "undefined") {
            this.id = option.id;
        }
        if (option?.currentLevel && option?.currentLevel !== "undefined") {
            this.currentLevel = Number(option.currentLevel);
        }
        if (option?.actCode && option?.actCode !== "undefined") {
            this.actCode = option.actCode;
        }
        await this.getLevelInterestDetailLists();
        await this.getMemberCouponList();
        Taro.nextTick(() => {
            this.getRect("#equity-" + this.currentIndex);
        });
    }
    /**
     * 获取权益列表
     */
    async getLevelInterestDetailLists() {
        let res = await EquityService.getLevelInterestDetailLists(this.currentLevel);
        if (res) {
            const index = res.findIndex((i) => i.interestsNo === this.id);
            for (let i = 0; i < 30; i++) {
                let copiedArray = res.slice();
                this.equityList.push(...copiedArray);
            }
            let item = res[index];
            this.remark = item.remark;
            this.id = item.interestsNo;
            this.currentIndex = index + res.length * 15;
        }
    }

    /**
     * 权益切换
     */
    handleChangeEquity(item, index1) {
            this.isAnimation = true;
            this.id = item.interestsNo;
            const index = this.equityList.findIndex((i) => i.interestsNo === this.id);
            this.remark = this.equityList[index].remark;
            this.currentIndex = index1;
            this.actCode = "";
            this.getMemberCouponList();
        Taro.nextTick(() => {
            this.getRect("#equity-" + index1);
        });
    }

    /**
     * 当前权益信息
     */
getRect(ele) {
        let that = this;
        //节点查询
        Taro.createSelectorQuery()
            .select(ele)
            .boundingClientRect((rect) => {
                console.log("rect---->", rect);
                let moveParams = that.moveParams;
                moveParams.subLeft = rect.left;
                moveParams.subHalfWidth = rect.width / 2;
                that.moveTo();
            })
            .exec();
    }

/**
 * 标签移动
 */
moveTo() {
        let subLeft = this.moveParams.subLeft;
        const { windowWidth } = Taro.getSystemInfoSync();
        let subHalfWidth = this.moveParams.subHalfWidth;
        let scrollLeft = this.moveParams.scrollLeft || 0;
        let distance = subLeft - windowWidth / 2 + subHalfWidth;
        scrollLeft = scrollLeft + distance;
        this.targetScrollLeft = scrollLeft;
    }

/**
 * 组件滚动事件
 */
scrollMove(e) {
        console.log("e----->", e);
        this.moveParams.scrollLeft = e.detail.scrollLeft;
    }

相关文章

  • UI自动化07 屏幕动作ActionChains

    点击操作单击、双击、右键 鼠标移动移动到某个控件、移动到某个位置、移动到某个元素的某个位置 拖拽点击、移动、放松 ...

  • 无标题文章

    1.UICollectionView横向滚动到某个item [_collectionViewscrollToIte...

  • redis-API-列表

    文档地址 列表类型有两个特点: 第一、列表中的元素是有序的,这就意味着可以通过索引下标获取某个元素或者某个范围内的...

  • 【Python基础】8. 列表 list

    本篇笔记知识点:列表内的元素的增,删,改,查列表复制判断列表内的元素列表内元素排序 列表的基本概念 列表让你能够在...

  • Sketch一键折叠所有画板并排序

    点击视图,点击图层列表,点击折叠所有编组。即可折叠所有画板。

  • 将第一个自然段设置为首行缩进两个汉字

    点击菜单:视图——标尺 把标尺起始点拖动到数字2 完成!

  • 7作 列表

    1.已知1个列表,求列表中心元素。 中心元素是:5和562.已知1个列表,求所有元素和。 3.已知1个列表,输出所...

  • 最新godaddy配置域名解析(2018.04)

    1.登录godaddy,选择个人账户中心并且点击管理域名 2. 在域名列表点击某个域名进入详情 3. 点击管理DN...

  • godadday配置域名解析

    1.登录godaddy,选择个人账户中心并且点击管理域名 2. 在域名列表点击某个域名进入详情 3. 点击管理DN...

  • RN SectionList通讯录定位筛选

    需求: 类似通讯录的右侧A-Z或者其他的筛选文案,点击或者滑动到某个文案的时候,弹出toast,并通知列表 注意:...

网友评论

      本文标题:横向列表内点击某个元素滑动到视图中心

      本文链接:https://www.haomeiwen.com/subject/hsxdxjtx.html