Vue+element ui realizes anchor positioning

Vue+element ui realizes anchor positioning

This article example shares the specific code of Vue + element UI to achieve anchor positioning for your reference. The specific content is as follows

vue

<el-row :gutter="20">
   <el-col :span="3">
    <!--Navigation selection event-->
    <el-menu :default-active="activeStep" @select="jump">
     <el-menu-item v-for="(item,index) in menuData" :index="`${index}`" :key="item.subjectId">
      <i class="el-icon-menu"></i>
      <span slot="title">{{item.subjectName}}</span>
     </el-menu-item>
    </el-menu>
   </el-col>
   <!--Binding scroll events requires listening-->
   <el-col :span="21" class="scroll_cls" @scroll="onScroll">
    <div v-for="(item,index) in tableObject" :key="index" style="height:500px">
     <div class="title scroll-item" :id="item.name">{{item.name}}</div>
     <el-table :data="item.rows" :key="index">
      <el-table-column label="Serial number" type="index" width="50"></el-table-column>
      <el-table-column prop="channelId" label="Channel/Team ID"></el-table-column>
      <el-table-column prop="channelName" label="Channel/Team"></el-table-column>
      <el-table-column prop="ruleCode" label="allocation plan id"></el-table-column>
      <el-table-column prop="ruleName" label="Allocation plan name"></el-table-column>
      <el-table-column prop="ruleVersion" label="version number"></el-table-column>
      <el-table-column prop="hierarchy" label="Level">
       <template slot-scope="scope">
        <span>{{scope.row.hierarchy == 1 ? 'Project' : 'Channel Team'}}</span>
       </template>
      </el-table-column>
      <el-table-column label="Validity period">
       <template slot-scope="scope">
        <span>{{scope.row.beginTime + '-' + scope.row.endTime}}</span>
       </template>
      </el-table-column>
      <el-table-column prop="creatorName" label="Operator"></el-table-column>
      <el-table-column prop="createTime" label="Operation time"></el-table-column>
      <el-table-column prop="orderCnt" label="Related Order">
       <template slot-scope="scope">
        <el-button
         @click="orderHandleClick(scope.row.orderCnt)"
         type="text"
         size="small"
        >{{scope.row.orderCnt}}</el-button>
       </template>
      </el-table-column>
      <el-table-column label="operation">
       <template slot-scope="scope">
        <el-button @click="settingHandleClick(scope.row)" type="text" size="small">Set allocation plan</el-button>
       </template>
      </el-table-column>
     </el-table>
     <el-pagination
      v-if="item.total > 5"
      style="margin-top: 15px"
      size="small"
      @size-change="handleSizeChange($event,index)"
      @current-change="handleCurrentChange($event,index)"
      :current-page="ruleForm.ageNum"
      :page-sizes="[10, 30, 50, 100]"
      :page-size="ruleForm.pageSize"
      layout="total, sizes, prev, pager, next"
      :total="item.total"
     ></el-pagination>
    </div>
   </el-col>
</el-row>

js

//Scroll trigger button highlight methods: {
  onScroll(e) {
            let scrollItems = document.querySelectorAll(".scroll-item");
            console.log(scrollItems)
            console.log(e)
   for (let i = scrollItems.length - 1; i >= 0; i--) {
    //Judge whether the scroll bar scroll distance is greater than the scrollable distance of the current scroll item let judge =
     e.target.scrollTop >=
     scrollItems[i].offsetTop - scrollItems[0].offsetTop;
    if (judge) {
                    console.log(i)
     this.activeStep = i.toString();
     break;
    }
   }
  },
  jump(index) {
            console.log(index)
   let target = document.querySelector(".scroll_cls");
   let scrollItems = document.querySelectorAll(".scroll-item");
   // Determine whether the scroll bar has scrolled to the bottom if (target.scrollHeight <= target.scrollTop + target.clientHeight) {
                console.log(index)
                console.log(typeof index)
    this.activeStep = index;
   }
            let total = scrollItems[index].offsetTop - scrollItems[0].offsetTop; // The distance between the anchor element and the top of its offsetParent (here is body) (the distance to be scrolled)
            console.log(total)
            let distance = document.querySelector(".scroll_cls").scrollTop; // The distance between the scroll bar and the top of the scroll area console.log(distance)
   // let distance = document.body.scrollTop || document.documentElement.scrollTop || window.pageYOffset // The distance between the scroll bar and the top of the scroll area (the scroll area is the window)
   // Scrolling animation implementation, use setTimeout recursion to achieve smooth scrolling, divide the distance into 50 small segments, and scroll once every 10ms // Calculate the distance of each small segment let step = total / 50;
   if (total > distance) {
    smoothDown(document.querySelector(".scroll_cls"));
   } else {
    let newTotal = distance - total;
    step = newTotal / 50;
    smoothUp(document.querySelector(".scroll_cls"));
   }

   // Parameter element is the scrolling area function smoothDown(element) {
    if (distance < total) {
     distance += step;
                    element.scrollTop = distance;
     setTimeout(smoothDown.bind(this, element), 10);
    } else {
     element.scrollTop = total;
    }
   }

   // Parameter element is the scrolling area function smoothUp(element) {
    if (distance > total) {
     distance -= step;
     element.scrollTop = distance;
     setTimeout(smoothUp.bind(this, element), 10);
    } else {
     element.scrollTop = total;
    }
   }

   document.querySelectorAll('.scroll-item').forEach((item, index1) => {
     if (index === index1) {
       item.scrollIntoView({
         block: 'start',
         behavior: 'smooth'
       })
     }
   })
  },
  },
 mounted() {
       this.$nextTick(() => {
           console.log(1)
           window.addEventListener('scroll', this.onScroll,true)
       })
    },

CSS

.scroll_cls {
    height: 500px;
 overflow:auto;
}

Reprinted from: Click here for the original link

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Vue implements anchor positioning function
  • Sample code for vue sliding ceiling and anchor positioning
  • Vue listens to scrolling to achieve anchor positioning (bidirectional) example
  • Alternative method for anchor positioning in Vue project
  • Detailed explanation of anchor positioning in Vue project

<<:  linux exa command (better file display experience than ls)

>>:  A brief talk about Mysql index and redis jump table

Recommend

How to use video.js in vue to play m3u8 format videos

Table of contents 1. Installation 2. Introducing ...

Detailed explanation of using Vue custom tree control

This article shares with you how to use the Vue c...

Install MySQL 5.7 on Ubuntu 18.04

This article is compiled with reference to the My...

Best Practices for Developing Amap Applications with Vue

Table of contents Preface Asynchronous loading Pa...

Detailed explanation of how to use the Vue license plate input component

A simple license plate input component (vue) for ...

25 Examples of News-Style Website Design

bmi Voyager Pitchfork Ulster Grocer Chow True/Sla...

MySQL5.7 master-slave configuration example analysis

MySQL5.7 master-slave configuration implementatio...

Practical method of deleting files from Linux command line

rm Command The rm command is a command that most ...

Instance method for mysql string concatenation and setting null value

#String concatenation concat(s1,s2); concatenate ...

How to handle spaces in CSS

1. Space rules Whitespace within HTML code is usu...

How to implement mask layer in HTML How to use mask layer in HTML

Using mask layers in web pages can prevent repeat...

Summary of commonly used operators and functions in MySQL

Let’s build the data table first. use test; creat...

A Brief Analysis of MySQL - MVCC

Version Chain In InnoDB engine tables, there are ...

MySQL independent index and joint index selection

There is often a lack of understanding of multi-c...

What to do if you forget the initial password when installing MySQL on Mac

Forgetting the password is a headache. What shoul...