【问题标题】:How to implement media query in React.js for full background image?如何在 React.js 中实现媒体查询以获得完整的背景图像?
【发布时间】:2019-01-19 18:52:33
【问题描述】:

有人可以帮我解决 React / css / 媒体查询问题吗?我正在尝试创建一个具有全宽和全高背景图像的首页。当我使用 const = bg {..} 在反应文件中创建样式时,它适用于桌面宽度。但是在使用 css 文件时我无法让它工作。媒体查询应将图像 url 切换为较小的屏幕尺寸。

import React from "react";
import { connectContext } from "react-connect-context";
import { Context } from "../context";
import Menu from "./Menu";
import Footer from "./Footer";
import { Header } from "semantic-ui-react";
import "./Frontpage.css";

const textStyle = {
  width: 220,
  height: 50,
  position: "absolute",
  top: 0,
  bottom: 0,
  left: 0,
  right: 0,
  margin: "auto"
};

class Frontpage extends React.PureComponent {
  render() {
    return (
      <React.Fragment>
        <Menu {...this.props} />
        <div className="bg">
          <Header
            as="h1"
            content="Evolve App"
            textAlign="center"
            inverted
            style={textStyle}
          />
        </div>
        <Footer />
      </React.Fragment>
    );
  }
}

export default connectContext(Context)(Frontpage);

Frontpage.css 文件如下所示:

bg {
  background-image: url(../img/Frontpage_desktop.jpeg);
  background-position: "center";
  background-repeat: "no-repeat";
  opacity: 0.6;
  height: "100vh";
}

@media only screen and (min-width: 768px) and (max-width: 991px) {
  bg {
    background-image: url(../img/Frontpage_tablet.jpg);
    background-position: "center";
    background-repeat: "no-repeat";
    opacity: 0.6;
    height: "100vh";
  }
}

@media only screen and (max-width: 767px) {
  bg {
    background-image: url(../img/Frontpage_mobile.jpg);
    background-position: "center";
    background-repeat: "no-repeat";
    opacity: 0.6;
    height: "100vh";
  }
}

【问题讨论】:

    标签: css reactjs media-queries


    【解决方案1】:

    您可以尝试从 React 组件设置宽度。试试这个,使用componentDidMount()生命周期方法,使用window.innerWidth函数设置图片的宽度,直接在组件中使用图片。

    class Frontpage extends React.PureComponent {
      state = {
      image_width: null,
      image_height: null
      }
    
      componentDidMount() {
        this.setImageSize()
      }
    
      setImageSize = () => {
       if(window.innerWidth < 768) {
           this.setState({image_width: 400, image_height: 400})
         } else {
           this.setState({image_width: 500, image_height: 500})
         }  
       }
    
      render() {
        return (
          <React.Fragment>
            <Menu {...this.props} />
            <img src="../img/Frontpage_tablet.jpg" width={this.state.image_width} height={this.state.image_height} /> 
              <Header
                as="h1"
                content="Evolve App"
                textAlign="center"
                inverted
                style={textStyle}
              />
            </div>
            <Footer />
          </React.Fragment>
        );
      }
    }
    

    您可能需要在图像上使用三元表达式

    { this.state.image_width
     ? <img ... /> 
     : null 
    } 
    

    我遇到了类似的问题,这应该对你有用。

    【讨论】:

    • 你会推荐这种方法而不是 css-media 查询吗?我有相同图像的三种不同尺寸。我也可以尝试根据 inner.Width 更改 url。 import CoverImageDesktop from "../img/Frontpage_desktop.jpeg"; import CoverImageTablet from "../img/Frontpage_tablet.jpg"; import CoverImageMobile from "../img/Frontpage_mobile.jpg";`setCoverImage = () => { if (window.innerWidth > 991) { this.setState({ setCoverImage: CoverImageDesktop }); } else if (window.innerWidth
    • 是的,就像我说的我遇到了类似的问题,在我的经验中,使用 React 的媒体查询并不总是很好。
    猜你喜欢
    • 1970-01-01
    • 2016-11-30
    • 1970-01-01
    • 2018-10-23
    • 2014-08-14
    • 2012-03-19
    • 2019-12-06
    • 2019-04-27
    • 1970-01-01
    相关资源
    最近更新 更多