Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the effect of tab ceiling in react.js

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to achieve tab suction effect in react.js". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to achieve tab suction effect in react.js" can help you solve the problem.

One requirement in react project development is that when the page scrolls to the location of the tab, the tab is fixed at the top.

The idea of implementation is actually very simple, which is to judge that when the scrolling distance scrollTop is greater than the tab distance offsetTop at the top of the page, the position of tab will be changed into fixed.

In react, I set a navTop property in state, switch the value of this property to true or false, and then the tab tag uses the method classnames () to add the class name fixed with the value of navTop.

This is what I wrote at first:

Import cs from 'classnames';class FixedTab extends React.Component {constructor (props) {super (props); this.state = {navTop: false} this.$tab = null; this.offsetTop = 0;} componentDidMount () {this.$tab = this.refs.tab; if (this.$tab) {this.offsetTop = this.$tab.offsetTop; window.addEventListener (' scroll',this.handleScroll);}} handleScroll () {let sTop = document.documentElement.scrollTop | window.pageYOffset | | document.body.scrollTop If (sTop > = this.offsetTop) {this.setState ({navTop: true})} if (sTop)

< this.offsetTop){ this.setState({ navTop:false }) } } render(){ return( ) } } 然后我发现这样写有问题,当我滚动条滚动距离达到条件时,tab是出现了吸顶的效果,但一瞬间又恢复了,滚动条也回弹了,一直无法继续下拉。 我一开始以为判断逻辑有问题,但一直找不到解决的办法,后来我怀疑是state值改变的时间差导致的,好像陷入了一个死循环,然后我就在判断滚动距离之前加了一个判断navTop的状态。 修改的主要代码如下: handleScroll(){ let sTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop; if(!this.state.navTop && sTop >

= this.offsetTop) {this.setState ({navTop: true})} if (sTop < this.offsetTop) {this.setState ({navTop:false})}}

In this way, the top suction effect will be normal after modification.

I think it's the setState method that causes the problem. SetState itself is an asynchronous method, and it also has a callback function as an argument.

This is the end of the introduction on "how to achieve the tab suction effect in react.js". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report