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

VMware12.0 installation Ubuntu14.04 LTS tutorial

I have installed various images under virtual mac...

An elegant way to handle WeChat applet authorization login

Preface When the WeChat mini program project invo...

CSS controls the spacing between words through the letter-spacing property

letter-spacing property : Increase or decrease th...

Two ways to achieve horizontal arrangement of ul and li using CSS

Because li is a block-level element and occupies ...

MySQL log settings and viewing methods

MySQL has the following logs: Error log: -log-err...

How to install and configure Redis in CentOS7

Introduction There is no need to introduce Redis ...

Detailed explanation of MySQL user rights management

Table of contents Preface: 1. Introduction to Use...

Pure CSS to achieve candle melting (water droplets) sample code

Achieve results Implementation ideas The melting ...

Three commonly used MySQL data types

Defining the type of data fields in MySQL is very...

How to remove inline styles defined by the style attribute (element.style)

When modifying Magento frequently, you may encount...

Mysql Chinese sorting rules description

When using MySQL, we often sort and query a field...

A brief discussion on the differences between FTP, FTPS and SFTP

Table of contents Introduction to FTP, FTPS and S...

Docker modifies the configuration information of an unstarted container

When I first used docker, I didn't use docker...

A simple way to change the password in MySQL 5.7

This is an official screenshot. After MySQL 5.7 i...

Execute initialization sql when docker mysql starts

1. Pull the Mysql image docker pull mysql:5.7 2. ...