复制动态元素大小

时间:2018-02-10 02:00:45

标签: javascript jquery html css reactjs

我正在创建一个包含大量图像的反应js的网站。我想要做的是包括一些标准的背景div /矩形,它们与每个相关图像的大小完全相同,这样这些图像就可以在它们之上做一些动画。请参阅以下代码段。但是我不知道如何动态告诉css每个图像的正确大小是什么,以便复制。我一直在玩创建第二个精确图像,然后创建某种重叠的入口边框,但它不保持图像的相同尺寸。如何才能最好地创建一个与我提供的图像大小相同的背景矩形?谢谢!

class App extends React.Component {
    constructor(props) {
    super();
    this.state = {
      onClik: false
    }
    this.handleClick = this.handleClick.bind(this);
  }

  handleClick()  {
    this.setState({onClick: !this.state.onClcik });
  }

  render() {
    return (
      <div className="parent-div">
        <button onClick={this.handleClick}>Click Me!</button>
       <div className="child-div">
        <img className={this.state.onClick ? "on" : "off"} src="https://imgflip.com/s/meme/Cute-Cat.jpg" />
        <div className="background-div"></div>
       </div>
       <div className="child-div">
        <img className={this.state.onClick ? "on" : "off"} src="http://www.readersdigest.ca/wp-content/uploads/2011/01/4-ways-cheer-up-depressed-cat.jpg" />
      <div className="background-div"></div>
        </div>
      </div>
    );
  }
}

ReactDOM.render(<App />, document.getElementById('app'));
img {
  width: 25%;
}

.on {
  transform: translateX(90px);
}

.background-div {
  background-color: red;
  height: 100px;
  width: 80px;
  display: inline-block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="app"></div>

1 个答案:

答案 0 :(得分:0)

不确定这是否与您所寻找的相同,但div与图像大小相同。

请参阅以下更新代码:

class App extends React.Component {
    constructor(props) {
        super();
        this.state = {
            onClick: false
        }
        this.handleClick = this.handleClick.bind(this);
    }

    handleClick() {
        this.setState({ onClick: !this.state.onClick });
    }

    render() {
        return (
            <div className="parent-div">
                <button onClick={this.handleClick}>Click Me!</button>
                <div className="child-div">
                    <img className={this.state.onClick ? "on" : "off"} src="https://imgflip.com/s/meme/Cute-Cat.jpg" />
                </div>
                <div className="child-div">
                    <img className={this.state.onClick ? "on" : "off"} src="http://www.readersdigest.ca/wp-content/uploads/2011/01/4-ways-cheer-up-depressed-cat.jpg" />
                </div>
            </div>
        );
    }
}

ReactDOM.render(<App />, document.getElementById('app'));
img {
  width: 25%;
}


.on {
  transform: translateX(80px);
}

.child-div {
  height: 100px;
  width: 80px;
  position: relative;
}

.child-div img {
  width: 100%;
  height: 100%;
}

.child-div:after {
  content:"";
  position:absolute;
  width:100%;
  height:100%;
  left: 0;
  background: red;
  z-index: -1;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="app"></div>