zoukankan      html  css  js  c++  java
  • React点击操作自动定位到另外一个元素

     

    使用Ref

    方式一

    使用ScrollIntoView方法

    import React from 'react'
    
    export default class ScrollToElement extends React.Component {
        render() {
            return (
                <div>
                    <button onClick={() => {
                        this.refs.targetElement.scrollIntoView()
                    }}>点击定位
                    </button>
                    <div style={{height: '100vh', backgroundColor: 'red'}}>我的</div>
                    <input ref='targetElement'/>
                </div>
            )
        }
    }

    方式二

    使用window.scrollTo方法

    import React from 'react'
    
    export default class ScrollToElement extends React.Component {
        render() {
            return (
                <div>
                    <button onClick={() => {
                        {/*this.refs.targetElement.scrollIntoView()*/}
                        window.scrollTo(0, this.refs.targetElement.offsetTop)
                    }}>点击定位
                    </button>
                    <div style={{height: '100vh', backgroundColor: 'red'}}>我的</div>
                    <input ref='targetElement'/>
                </div>
            )
        }
    }
  • 相关阅读:
    discuz开发笔记
    响应式布局
    timedelta
    图片轮播
    性能
    事件捕获
    git
    css hacks
    AFNetworking 网络错误提示data转换字符串
    常见HTTP错误代码
  • 原文地址:https://www.cnblogs.com/xiaochengzi/p/10370189.html
Copyright © 2011-2022 走看看