search
HomeWeChat AppletMini Program DevelopmentA brief analysis of how to make scroll-view scroll according to the specified position in the mini program

This article will introduce to you how to make the scroll-view scroll according to the specified position in the WeChat applet. You can get an excellent experience without writing additional js scripts. I hope it will be helpful to everyone!

A brief analysis of how to make scroll-view scroll according to the specified position in the mini program

The background is like this. The WeChat applet has a tab switching page, and the tab switching page has two content boxes. I use scroll-view to make it, and then when switching the tab page, the corresponding scroll bar in scroll-view needs to be on top. [Related learning recommendations: 小program development tutorial]

We can see that there is a scroll-into-view# in the official document description scroll-view ##Attribute, the description of this attribute is as follows

The value of scroll-into-view should be a certain sub-element id (the id cannot start with a number). Set which direction can be scrolled, then scroll to the element in which direction

Then we can make a fuss about this attribute, just put an id value in

scroll-view By setting a custom value, when switching tab, the corresponding content box scroll bars will automatically scroll to the top, as shown in the following code. The following code is what I use Taro The program framework demonstrates the same as the native one.

import Taro from '@tarojs/taro'
import { View } from '@tarojs/components'
import { AtTabs, AtTabsPane } from 'taro-ui'
export default class Index extends Taro.Component {
  constructor () {
    super(...arguments)
    this.state = {
      current: 0,
    }
  }
  handleClick (value) {
    this.setState({
      current: value
    })
  }
  render () {
    const tabList = [{ title: '标签第一页' }, { title: '标签第二页' }, { title: '标签第三页' }]
    return (
      <AtTabs current={this.state.current} tabList={tabList} onClick={this.handleClick.bind(this)}>
        <AtTabsPane current={this.state.current} index={0} >
          <ScrollView scrollY scrollIntoView=&#39;content-0&#39;>
          <View id=&#39;content-0&#39;></View>
          标签页一的内容
          </ScrollView>
        </AtTabsPane>
        <AtTabsPane current={this.state.current} index={1} >
          <ScrollView scrollY scrollIntoView=&#39;content-1&#39;>
          <View id=&#39;content-1&#39;></View>
          标签页二的内容
          </ScrollView>
        </AtTabsPane>
        <AtTabsPane current={this.state.current} index={2} >
          <ScrollView scrollY scrollIntoView=&#39;content-2&#39;>
          <View id=&#39;content-2&#39;></View>
          标签页三的内容
          </ScrollView>
        </AtTabsPane>
      </AtTabs>
    )
  }
}

For example, place a

view## with the ID value content-0 in the scroll-view of the first tab #, then when switching the tab page, scroll-view will be positioned on the id of the child element according to the scroll-into-view attribute we set, and reach The effect of the scroll bar automatically moving to the top<pre class='brush:php;toolbar:false;'>&lt;AtTabsPane current={this.state.current} index={0} &gt; &lt;ScrollView scrollY scrollIntoView=&amp;#39;content-0&amp;#39;&gt; &lt;View id=&amp;#39;content-0&amp;#39;&gt;&lt;/View&gt; 标签页一的内容 &lt;/ScrollView&gt; &lt;/AtTabsPane&gt;</pre>Similarly, if you need the scroll bar to scroll to the lowest position, just put the corresponding sub-element ID to the lowest position. For example, in some chat interfaces, you need to locate the latest one. Article

<AtTabsPane current={this.state.current} index={0} >
  <ScrollView scrollY scrollIntoView=&#39;content-0&#39;>
    标签页一的内容
    <View id=&#39;content-0&#39;></View>
  </ScrollView>
</AtTabsPane>

For more programming-related knowledge, please visit:

Programming Video

! !

The above is the detailed content of A brief analysis of how to make scroll-view scroll according to the specified position in the mini program. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金社区. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!