代码之家  ›  专栏  ›  技术社区  ›  HoCo_

当我切换滑动侧菜单时,如何使全身向右滑动

  •  0
  • HoCo_  · 技术社区  · 8 年前

    下面是我的情况演示: https://codesandbox.io/s/8zzr0jmm98

    下面是我的代码片段:

    import React from "react";
    import ReactDOM from "react-dom";
    
    import "./styles.css";
    
    class App extends React.Component {
      state = { menuDisplayed: false };
      toggleMenu = () => {
        console.log("toggleMenu reached");
        this.setState({
          menuDisplayed: !this.state.menuDisplayed
        });
        return;
      };
    
      render() {
        return (
          <div className="App">
            <div
              className={
                this.state.menuDisplayed ? "box_two_displayed" : "box_two_hidden"
              }
            >
              {" "}
              Box Two{" "}
            </div>
            <div
              className={
                this.state.menuDisplayed ? "box_one_displayed" : "box_one_hidden"
              }
            >
              {" "}
              Box One{" "}
            </div>
            <div onClick={this.toggleMenu} className="toggler">
              {" "}
              ToggleMenu{" "}
            </div>
          </div>
        );
      }
    }
    const rootElement = document.getElementById("root");
    ReactDOM.render(<App />, rootElement);
    

    这里是我的css代码片段:

    .App {
      font-family: sans-serif;
      text-align: center;
    }
    
    .box_one_displayed {
      /* position: fixed;*/
      left: 200px;
      /* height: 100vh;*/
      background-color: grey;
      transition: all 0.5s linear;
    }
    
    .box_one_hidden {
      /* position: fixed;*/
      left: 0;
      /* height: 100vh;*/
      background-color: grey;
      transition: all 0.5s linear;
    }
    
    .box_two_displayed {
      position: fixed;
      z-index: 500;
      width: 200px;
      left: 0;
      background-color: cyan;
      transition: all 0.5s linear;
    }
    
    .box_two_hidden {
      position: fixed;
      z-index: 500;
      width: 200px;
      left: -200px;
      background-color: cyan;
      transition: all 0.5s linear;
    }
    
    .toggler {
      position: fixed;
      top: 50px;
      left: 350px;
      cursor: pointer;
      background-color: pink;
    }
    

    任何暗示都很好, 谢谢

    1 回复  |  直到 8 年前
        1
  •  0
  •   Vladimir Mootin    8 年前

    您不需要position属性来执行此操作,只需使用flex即可 https://codepen.io/kotanbich/pen/aRYYJW .

    HTML格式:

    <div class="page">
      <div class="navigation">
        <button id="toggle">
          ☰
        </button>
        <ul>
          <li>
            cattle
          </li>
          <li>
            poultry
          </li>
        </ul>
      </div>
      <div class="content">
        <div>
        </div>
      </div>
    </div>
    

    CSS格式:

    .page{
        height: 100%;
        display: flex;
    }
    .navigation{
        width: 230px;
        flex-shrink: 0;
        background: green;
    }
    .navigation.hidden{
      width: 25px;
      overflow-x:hidden;
    }
    

    JS公司:

    const toggleButton =  document.getElementById('toggle');
    toggleButton.addEventListener('click',(event)=>{
      const navigation = document.querySelector('.navigation');
      navigation.classList.toggle('hidden');
    })
    

    主容器包含侧边栏和内容端,主容器应设置为flex。如果希望导航具有指定的宽度,而不是内容的宽度,则主容器侧边栏内部的flex-shrink应设置为0。