0

这是来自 github 的 demo0-simple-transition 的代码。链接 如何向移动的 div 元素添加内容。希望使用它的变体来运行飞出菜单,但似乎无法弄清楚如何将内部内容放入其中。谢谢

import React from 'react';
import {Motion, spring} from '../../src/react-motion';

const Demo = React.createClass({
  getInitialState() {
return {open: false};
  },

  handleMouseDown() {
this.setState({open: !this.state.open});
  },

  handleTouchStart(e) {
e.preventDefault();
this.handleMouseDown();
  },

  render() {
return (
  <div>
    <button
      onMouseDown={this.handleMouseDown}
      onTouchStart={this.handleTouchStart}>
      Toggle
    </button>

    <Motion style={{x: spring(this.state.open ? 400 : 0)}}>
      {({x}) =>
        // children is a callback which should accept the current value of
        // `style`
        <div className="demo0">
          <div className="demo0-block" style={{
            WebkitTransform: `translate3d(${x}px, 0, 0)`,
            transform: `translate3d(${x}px, 0, 0)`,
          }} />
        </div>
      }
    </Motion>
  </div>
);
  },
});

export default Demo;
4

2 回答 2

1

So this is what is being returned from your React Motion component -

<div className="demo0">
  <div className="demo0-block" style={{
        WebkitTransform: `translate3d(${x}px, 0, 0)`,
        transform: `translate3d(${x}px, 0, 0)`,
      }} />
    </div>

In React, a <div> can be written as <div/> if it has no children. To insert children, turn it into a normal HTML div format: <div>{children}</div>

In your case, that would be:

<div className="demo0">
       <div className="demo0-block" style={{
        WebkitTransform: `translate3d(${x}px, 0, 0)`,
        transform: `translate3d(${x}px, 0, 0)`,
      }} >
  {/* Children elements here */}
  </div>
</div>
于 2016-09-19T18:45:48.347 回答
0

汤姆再次感谢。您的回答 99% 正确,但不需要大括号{}。您只需要关闭打开的 div 标签 >。并正常关闭div。并开始在下面添加元素。(也许这就是你真正的意思,大括号只是为了演示。如果这是真的 - 100% 正确)

<Motion style={{x: spring(this.state.open ? 400 : 0)}}>
  {({x}) =>
    // children is a callback which should accept the current value of
    // `style`
    <div className="demo0">
      <div className="demo0-block" style={{
        WebkitTransform: `translate3d(${x}px, 0, 0)`,
        transform: `translate3d(${x}px, 0, 0)`,
      }}>
               <div><h1>Lots of stuff can go in here      
                              here now!</h1></div>
          </div>
  }
</Motion>
于 2016-09-21T14:03:18.037 回答