css - 在父级上使用倾斜会导致 sidenav 失去位置

标签 css position transform absolute skew

抱歉,我的措辞不太准确,我不确定如何表达。

我有一个标题和一个容器,其中包含侧导航和用于切换它的按钮。我试图倾斜标题,同时通过向相反方向倾斜来保持容器正常。然而,这样做会导致侧导航失去其高度:100%,并且不会粘在左侧。

如何在不影响侧翼的情况下倾斜背景?

这是 fiddle 和代码 https://jsfiddle.net/q0ddzw4v/

HTML

<body id="body">
    <header class="header">
        <div class="header__container">
            <div id="mySidenav" class="sidenav">
                <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
                <a href="#">About</a>
                <a href="#">Services</a>
                <a href="#">Clients</a>
                <a href="#">Contact</a>
            </div>

            <div id="main">
                <h2>Sidenav Push Example</h2>
                <p>Click on the element below to open the side navigation menu, and push this content to the right.</p>
                <span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; open</span>
            </div>
        </div>
    </header>

    <script>
        function openNav() {
            document.getElementById("mySidenav").style.width = "250px";
        }

        function closeNav() {
            document.getElementById("mySidenav").style.width = "0";
        }
    </script>
</body>

CSS

body {
    font-family: sans-serif;
}

.sidenav {
    height: 100%;
    width: 0;
    position: fixed;
    z-index: 1;
    top: 0;
    left: 0;
    background-color: #111;
    overflow-x: hidden;
    transition: 0.5s;
    padding-top: 60px;
}

.sidenav a {
    padding: 8px 8px 8px 32px;
    text-decoration: none;
    font-size: 25px;
    color: #818181;
    display: block;
}

.sidenav a:hover,
.offcanvas a:focus {
    color: #f1f1f1;
}

.sidenav .closebtn {
    position: absolute;
    top: 0;
    right: 25px;
    font-size: 36px;
    margin-left: 50px;
}

#main {
    padding: 16px;
}

.header {
    width: 100%;
    height: 100vh;
    background-color: #C18D8D;
    transform: skewY(-10deg);
}

.header__container {
    width: 80%;
    max-width: 71.25rem;
    margin: auto;
    display: flex;
    flex-direction: column;
    height: 100%;
    transform: skewY(10deg);
}

最佳答案

不要倾斜 .header 容器,而是添加一个伪元素并倾斜它:

body {
  font-family: "Lato", sans-serif;
  transition: margin-left 0.5s;
}

.sidenav {
  height: 100%;
  width: 0;
  position: fixed;
  z-index: 1;
  top: 0;
  left: 0;
  background-color: #111;
  overflow-x: hidden;
  transition: 0.5s;
  padding-top: 60px;
}

.sidenav a {
  padding: 8px 8px 8px 32px;
  text-decoration: none;
  font-size: 25px;
  color: #818181;
  display: block;
}

.sidenav a:hover,
.offcanvas a:focus {
  color: #f1f1f1;
}

.sidenav .closebtn {
  position: absolute;
  top: 0;
  right: 25px;
  font-size: 36px;
  margin-left: 50px;
}

#main {
  transition: margin-left .5s;
  padding: 16px;
}

.header {
  position: relative;
  width: 100%;
  height: 100vh;
}

.header::before {
  display: block;
  position: absolute;
  width: 100%;
  height: 100%;
  background-color: #C18D8D;
  transform: skewY(-10deg);
  content: '';
}

.header__container {
  position: relative;
  z-index: 0;
  width: 80%;
  max-width: 71.25rem;
  margin: auto;
  display: flex;
  flex-direction: column;
  height: 100%;
}
<header class="header">
  <div class="header__container">
    <div id="mySidenav" class="sidenav">
      <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
      <a href="#">About</a>
      <a href="#">Services</a>
      <a href="#">Clients</a>
      <a href="#">Contact</a>
    </div>

    <div id="main">
      <h2>Sidenav Push Example</h2>
      <p>Click on the element below to open the side navigation menu, and push this content to the right.</p>
      <span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; open</span>
    </div>
  </div>
</header>

<script>
  function openNav() {
    document.getElementById("mySidenav").style.width = "250px";
    // document.getElementById("body").style.marginLeft = "250px";
  }

  function closeNav() {
    document.getElementById("mySidenav").style.width = "0";
    // document.getElementById("body").style.marginLeft = "0";
  }
</script>

关于css - 在父级上使用倾斜会导致 sidenav 失去位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44433060/

相关文章:

javascript - 使用js单击时尝试更改div的位置

html - 如何居中 "position: relative"- 导航栏?

javascript - CSS3变换后获取div的实际像素坐标

css - 为移动访问者制作菜单棒?

javascript - 工具提示 CSS : Remove new line

javascript - AngularJS:动态设置背景颜色

css - vueJS 转换旋转样式内联

c# - 自动完成看起来不正确

r - ggplot2 - 如何并排绘制图表,只留下一个图例

java - 测量旋转字符串的像素高度Java