javascript - 损坏的代码?为什么它停止工作

标签 javascript reactjs function react-props

当页面最初加载时,标题不应该是可见的,但是当用户开始滚动时,标题应该出现。

我希望看到什么?

在用户开始滚动之前标题不应该可见

我现在看到了什么

标题在页面首次加载时显示,它不应该显示。 它只是在 Chrome 中没有按预期工作,而在 Safari 中工作正常

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

  handleHover = () => {
    this.setState({
      hide: true
    });
  };

  componentDidMount() {
    window.addEventListener("scroll", this.handleHover);
  }
  componentWillUnmount() {
    window.removeEventListener("scroll", this.handleHover);
  }

  render() {
    return (
      <div>
        {this.state.hide ? (
          <div
            style={{
              width: "100%",
              height: "120px",
              margin: "0 auto",
              position: "absolute"
            }}
          >
            <div
              style={{
                zIndex: "0",
                position: "absolute",
                marginLeft: "120px",
                marginTop: "6px"
              }}
            >
              <img className="Logo" src={Logo} alt="logo" />
            </div>
            <div className="menuContainer">
              <nav>
                <Link to="/" className="linkTitle" href="">
                  Home
                </Link>
                <Link to="/shop" className="linkTitle" href="">
                  Shop
                </Link>
                <a className="linkTitle" href="#aboutus">
                  About
                </a>
                <a className="linkTitle" href="">
                  Contact Us
                </a>
                <a className="linkTitle" href="">
                  As seen
                </a>
              </nav>
            </div>
          </div>
        ) : null}

        <div className="picContainer">
          <div
            style={{ backgroundImage: `url(${hairImage})` }}
            className="picSection one"
          />
          <div className="picSection two" />
          <div className="picSection three" />
          <div className="picSection four" />
          <div className="picSectionL five" />
        </div>
      </div>
    );
  }
}

最佳答案

https://codesandbox.io/s/zlyqyvjzjl

给你。确保你真的可以滚动。如果您的 div 太小而无法滚动,则不会触发该事件。

{this.state.hide ? null : <div>some item</div>}

完整代码如下:

从“ react ”导入 react ; 从“react-dom”导入 ReactDOM;

导入“./styles.css”;

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      hide: true
    };
  }

  handleHover = () => {
    console.log("hello");
    this.setState({
      hide: false
    });
  };

  componentDidMount() {
    window.addEventListener("scroll", this.handleHover);
  }
  componentWillUnmount() {
    window.removeEventListener("scroll", this.handleHover);
  }

  render() {
    return (
      <div style={{ height: "2000px" }}>
        {this.state.hide ? null : (
          <div
            style={{
              width: "100%",
              height: "120px",
              margin: "0 auto",
              position: "absolute"
            }}
          >
            <div
              style={{
                zIndex: "0",
                position: "absolute",
                marginLeft: "120px",
                marginTop: "6px"
              }}
            >
              logo
            </div>
          </div>
        )}

        <div className="picContainer">hello</div>
      </div>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

关于javascript - 损坏的代码?为什么它停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51508219/

相关文章:

reactjs - 为什么 React 的 API 很小,但文件却这么大?

c - 使用递归查找具有相同数量的 0's and 1' 的最大子数组的长度

javascript - 如何从函数中获取选定的元素并将其作为另一个函数的参数传递

javascript - 从 PHP 渲染 React 组件

javascript - 如何检测页面是否在顶部?

javascript - 当使用 React 的 material-ui Appbar 上存在多个菜单时,如何分配打开 onClick 的 MenuItem?

c - C函数中数组的X,Y传递大小

javascript - 如何避免模态对背景内容的影响?

javascript - 使用 .load() 加载后 Div 保持为空

javascript - 如何使用javascript更新css过滤器中的明亮值而不删除其他过滤器属性。?