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

MySQL method steps to determine whether it is a subset

Table of contents 1. Problem 2. Solution Option 1...

The implementation process of extracting oracle data to mysql database

In the migration of Oracle database to MySQL data...

Detailed explanation of MySQL backup and recovery practice of mysqlbackup

1. Introduction to mysqlbackup mysqlbackup is the...

Summary of Common Problems with Mysql Indexes

Q1: What indexes does the database have? What are...

How to implement horizontal bar chart with percentage in echarts

Table of contents Example Code Rendering Code Ana...

Learn more about using regular expressions in JavaScript

Table of contents 1. What is a regular expression...

Vue3.0+vite2 implements dynamic asynchronous component lazy loading

Table of contents Create a Vite project Creating ...

How to select all child elements and add styles to them in CSS

method: Take less in the actual project as an exa...

Detailed example of database operation object model in Spring jdbc

Detailed example of database operation object mod...

React implements a highly adaptive virtual list

Table of contents Before transformation: After tr...

How to use nginx to simulate blue-green deployment

This article introduces blue-green deployment and...