【问题标题】:How to move an Image on click in React Native如何在 React Native 中单击时移动图像
【发布时间】:2017-08-25 00:14:24
【问题描述】:

我最近一直在学习 React Native,但还没有真正找到操作 DOM 的方法。

我一直在努力做到这一点,如果我点击 TouchableHighlight,我的图像会向下移动几个 px,但我还没有设法让它移动,老实说,我不知道如何从在这里。

我的 onclick 函数有效,因为每次单击按钮时它都会返回日志。

到目前为止,我有这个:

export default class MainBody extends Component {

  onclick = () => {
    console.log('On click works')
  };

  render() {

    return (

      <ScrollView showsHorizontalScrollIndicator={false} style={Style.horList} horizontal={true}>

        <View >

          {/*i need to move this Image down!*/}
          <Image source={require("./img/example.png")}/>
          <View>
            <Text style={Style.peopleInvited}>This is text</Text>
          </View>

          {/*When clicked on this touchable highlight!*/}
          <TouchableHighlight onPress={this.onclick}}>
            <Image source={require('./img/moveImg.png')}/>
          </TouchableHighlight>
        </View>

      </ScrollView>
}

如果有人可以帮助我克服这个问题,将不胜感激。 非常感谢您的参与!

【问题讨论】:

    标签: javascript dom react-native dom-manipulation


    【解决方案1】:

    有很多方法可以做到这一点,但也许最简单的方法是使用 states

    class MainBody extends Component {
        constructor(props) {
            super(props);
            this.state = {
                top: 0 // Initial value
            };
        }
      onclick = () => {
        console.log('On click works')
        this.setState( { top: this.state.top + 5 }) // 5 is value of change.
    };
    
    
      render() {
    
        return (
    
          <ScrollView showsHorizontalScrollIndicator={false} horizontal={true}>
    
            <View >
    
              {/*i need to move this Image down!*/}
              <Image style={{top: this.state.top}}source={require("./img/example.png")}/>
              <View>
                <Text>This is text</Text>
              </View>
    
              {/*When clicked on this touchable highlight!*/}
              <TouchableHighlight onPress={this.onclick}>
                <Image source={require("./img/moveImg.png")}/>
              </TouchableHighlight>
            </View>
    
          </ScrollView>
      );
      }
    }
    

    【讨论】:

    • 这似乎是一种非常简单和干净的方法,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2023-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多