【问题标题】:Arrange 2 divs diagonally inside a parent div在父 div 内对角排列 2 个 div
【发布时间】:2019-01-05 11:52:15
【问题描述】:

我正在尝试在父 div 中排列 2 个 div,这样看起来父 div 被对角分成 2 个部分。下图将显示需要什么

这是我尝试过的代码。

App.js

import React, { Component } from "react";
import "./App.css";

class InnerMainDiv extends Component {
  constructor() {
    super();
    this.section = React.createRef();
  }
  componentDidMount() {
    this.handleResize();
    window.addEventListener("resize", this.handleResize);
  }
  componentWillUnmount() {
    window.addEventListener("resize", null);
  }
  handleResize = (WindowSize, event) => {
    let h = this.section.current.clientHeight;
    let w = this.section.current.clientWidth;
    let angle = Math.atan(h / w) * 57.29577;
    let rotateProperty = "rotate(" + angle + "deg)";
    this.section.current.style.webkitTransform = rotateProperty;
    this.section.current.style.transform = rotateProperty;
    this.section.current.style.mozTransform = rotateProperty;
  };
  render() {
    return (
      <div className="maindiv">
        <section ref={this.section}>
          <div href="#1" />
        </section>
        <section ref={this.section}>
          <div href="#2" />
        </section>
      </div>
    );
  }
}

export default InnerMainDiv;

App.css

html,
body,
div {
  height: 100%;
  width: 100%;
  padding: 0;
  margin: 0;
}

div {
  overflow: hidden;
  position: relative;
}

section {
  position: absolute;
  top: -100%;
  height: 5000vw;
  width: 5000vh;
  background: #ccc;
  -webkit-transform-origin: 0 0;
  -moz-transform-origin: 0 0;
  transform-origin: 0 0;
}

section + section {
  background: #666;
  top: 0%;
}

section div {
  display: block;
  width: 100%;
  height: 100%;
  cursor: pointer;
}

关于如何实现这一目标的任何想法或建议?

【问题讨论】:

    标签: html css reactjs css-grid


    【解决方案1】:

    您可以使用clip-path 来实现:

    .container {
      width: 200px;
      height: 200px;
      position: relative;
    }
    
    .container > * {
      height: 100%;
      background: red;
    }
    
    .container :last-child {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      background: blue;
      -webkit-clip-path: polygon(0 0, 100% 0%, 100% 100%);
      clip-path: polygon(0 0, 100% 0%, 100% 100%);
    }
    <div class="container">
      <div></div>
      <div></div>
    </div>

    但如果你想要更多的浏览器支持,你可以像这样使用旋转:

    .container {
      width: 200px;
      height: 200px;
      position: relative;
      overflow:hidden;
    }
    
    .container > * {
      height: 100%;
      background: red;
    }
    
    .container :last-child {
      position: absolute;
      top: 0;
      left: 0;
      width: 141%; /* = 1.41 * 100% --> 1.41 = sqrt(2) */
      height: 141%;
      background: blue;
      transform-origin:top left;
      transform:rotate(-45deg);
    }
    <div class="container">
      <div></div>
      <div></div>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-08
      • 2017-08-14
      • 1970-01-01
      • 2013-03-11
      • 2015-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多