Vue implements anchor positioning function

Vue implements anchor positioning function

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

Here we mainly implement a simple scroll-triggered anchor point highlighting and click the anchor point to trigger the scrolling function. If you want to get the scroll height of the browser, each browser is different. Use the following methods:

Chrome: document.body.scrollTop
Firefox: document.documentElement.scrollTop
Safari: window.pageYOffset

I am scrolling a local element here, so it is slightly different. First attach the html and css code blocks:

scroll-content is the scrolling area, and operation-btn is the button that controls the behavior of the anchor point.

<template>
  <div class="anchor-point">
    <!-- Scrolling area -->
    <div class="scroll-content" @scroll="onScroll">
      <div class="scroll-item" style="height: 500px;background: #3a8ee6;">One ​​layer</div>
      <div class="scroll-item" style="height: 500px;background: red;">Second floor</div>
      <div class="scroll-item" style="height: 500px;background: #42b983">Three layers</div>
      <div class="scroll-item" style="height: 1000px;background: yellow;">Four layers</div>
    </div>
    <!-- Button -->
    <div class="operation-btn">
      <div v-for="(item, index) in ['First floor','Second floor','Third floor','Fourth floor']" :key="index" @click="jump(index)"
           :style="{background: activeStep === index ? '#eeeeee' : '#ffffff'}">{{item}}
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
  .anchor-point {
    flex-basis: 100%;
    display: flex;
    overflow: hidden;
    .scroll-content {
      height: 100%;
      width: 90%;
      overflow: scroll;
    }
    .operation-btn {
      width: 10%;
      height: 100%;
    }
  }
</style>

By listening to scroll events, highlight the anchor button

Here, by traversing the scroll items, it is determined whether the scroll bar scroll distance is greater than the scrollable distance of the current item (that is, the distance from the top of its offsetParent, which is body here)

//Scroll trigger button highlight onScroll (e) {
  let scrollItems = document.querySelectorAll('.scroll-item')
  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) {
      this.activeStep = i
      break
    }
  }
},

Add a click event, scroll to the corresponding area according to the anchor point and achieve smooth scrolling

Here we refer to the method on the Internet, subdivide the scrolling distance into multiple small segments, and consider the upward and downward scrolling to achieve the scrolling transition animation.
Originally, I planned to use scrollIntoView to implement scrolling animation. scrollIntoView has good support in various browsers, but ScrollIntoViewOptions still has problems with browser compatibility, so I used the following distance division method instead.

// Click to switch anchor point jump (index) {
 let target = document.querySelector('.scroll-content')
 let scrollItems = document.querySelectorAll('.scroll-item')
 // Determine whether the scroll bar has scrolled to the bottom if (target.scrollHeight <= target.scrollTop + target.clientHeight) {
   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)
 let distance = document.querySelector('.scroll-content').scrollTop // The distance between the scroll bar and the top of the scroll area // 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, subdivide 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-content'))
 } else {
   let newTotal = distance - total
   step = newTotal / 50
   smoothUp(document.querySelector('.scroll-content'))
 }

 // 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'
 // })
 // }
 // })
}

Here is the effect diagram:

This is the first time I have implemented the effects of anchor point positioning and scrolling highlight anchor points. There are some shortcomings. Please correct me if you have any questions or suggestions.
I am very grateful for the inspiration brought by this article, and I got a new knowledge point.

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:
  • 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
  • Vue+element ui realizes anchor positioning

<<:  Several scenarios for using the Nginx Rewrite module

>>:  MySql learning day03: connection and query details between data tables

Recommend

Quick solution for forgetting MySQL8 password

Preface When we forget the MySQL database passwor...

Detailed explanation of Vue mixin usage and option merging

Table of contents 1. Use in components 2. Option ...

Detailed explanation of the principles and usage of MySQL stored procedures

This article uses examples to explain the princip...

How to implement Hover drop-down menu with CSS

As usual, today I will talk about a very practica...

JavaScript data flattening detailed explanation

Table of contents What is Flattening recursion to...

Three Ways to Find the Longest Word in a String in JavaScript (Recommended)

This article is based on the Free Code Camp Basic...

Detailed explanation of zabbix executing scripts or instructions on remote hosts

Scenario Requirements 1. We can use the script fu...

JavaScript to achieve the effect of tab bar switching

Tab bar: Click different tabs to display differen...

How to generate Hive table creation statement comment script in MySQL metadata

Preface This article mainly introduces the releva...

Mobile front-end adaptation solution (summary)

I searched online and found that many interviews ...

A brief discussion on HTML doctype and encoding

DOCTYPE Doctype is used to tell the browser which...

How to use libudev in Linux to get USB device VID and PID

In this article, we will use the libudev library ...

WeChat applet implements waterfall flow paging scrolling loading

This article shares the specific code for WeChat ...