最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I apply page specific Full screen background image using React? - Stack Overflow

programmeradmin2浏览0评论

I have a component called admin-login.jsx and below is the code for the same:

// AdminLogin Component
class AdminLogin extends Component {

  componentDidMount() {
    let elem = document.querySelector('body');
    addClass(elem, 'admin-login-page');
  }

  componentWillUnmount() {
    let elem = document.querySelector('body');
    removeClass(elem, 'admin-login-page');
  }

  render() {

    return(
      <div className="admin-login">
        <LoginModule
          submit={handleSubmit(this.onLogin.bind(this))}
          email={email} password={password}
          loginFailed={this.state.loginFailed}
          disableSubmit={this.state.isLoading}
        />
      </div>
    );
  }
}

admin.login.scss

@import "styles/site-mixins";

.admin-login-page {
  background: url(../images/admin.login.bg.jpg) no-repeat top center fixed;
  @include prefix(background-size, cover, webkit moz ms);
}

routes.js

import App from 'components/app';
import Admin from './admin/admin';

const rootRoute = {
  'path': '/',
  'component': App,
  'childRoutes': [
    Admin
  ]
};

export default rootRoute;

routes/admin/admin.js

export default {
  'path': 'admin-login',
  'indexRoute': {
    getComponent(location, cb) {
      require.ensure([], (require) => {
        cb(null, require('components/admin/login/admin-login').default);
      }, 'admin_login');
    }
  },
  getComponent(location, cb) {
    require.ensure([], (require) => {
      cb(null, require('components/admin/admin').default);
    }, 'admin');
  }
};

I have stripped the code that our inessential for this question. What I am trying to do is apply the class admin-login-page to the body when this component mounts and then remove the class when the component unmounts.

But, I am seeing a very weird behavior. Even when the class gets removed on unmount and the route changes, the background image stays on the page.

I'll add the image for more clarity.

When I route to localhost:8080/admin-login:

When I route to the root url i.e localhost:8080 by clicking the logo on localhost:8080/admin-login using react-routers tag:

Note that, everything happens without refresh. Also, I can do this by getting the value of the height of the screen and then applying it as a property to one of the class present in the component so that when component unmounts the background disappears. But, I'd like a solution where I can apply a full screen background image using the body tag.

Thanks in anticipation.

I have a component called admin-login.jsx and below is the code for the same:

// AdminLogin Component
class AdminLogin extends Component {

  componentDidMount() {
    let elem = document.querySelector('body');
    addClass(elem, 'admin-login-page');
  }

  componentWillUnmount() {
    let elem = document.querySelector('body');
    removeClass(elem, 'admin-login-page');
  }

  render() {

    return(
      <div className="admin-login">
        <LoginModule
          submit={handleSubmit(this.onLogin.bind(this))}
          email={email} password={password}
          loginFailed={this.state.loginFailed}
          disableSubmit={this.state.isLoading}
        />
      </div>
    );
  }
}

admin.login.scss

@import "styles/site-mixins";

.admin-login-page {
  background: url(../images/admin.login.bg.jpg) no-repeat top center fixed;
  @include prefix(background-size, cover, webkit moz ms);
}

routes.js

import App from 'components/app';
import Admin from './admin/admin';

const rootRoute = {
  'path': '/',
  'component': App,
  'childRoutes': [
    Admin
  ]
};

export default rootRoute;

routes/admin/admin.js

export default {
  'path': 'admin-login',
  'indexRoute': {
    getComponent(location, cb) {
      require.ensure([], (require) => {
        cb(null, require('components/admin/login/admin-login').default);
      }, 'admin_login');
    }
  },
  getComponent(location, cb) {
    require.ensure([], (require) => {
      cb(null, require('components/admin/admin').default);
    }, 'admin');
  }
};

I have stripped the code that our inessential for this question. What I am trying to do is apply the class admin-login-page to the body when this component mounts and then remove the class when the component unmounts.

But, I am seeing a very weird behavior. Even when the class gets removed on unmount and the route changes, the background image stays on the page.

I'll add the image for more clarity.

When I route to localhost:8080/admin-login:

When I route to the root url i.e localhost:8080 by clicking the logo on localhost:8080/admin-login using react-routers tag:

Note that, everything happens without refresh. Also, I can do this by getting the value of the height of the screen and then applying it as a property to one of the class present in the component so that when component unmounts the background disappears. But, I'd like a solution where I can apply a full screen background image using the body tag.

Thanks in anticipation.

Share Improve this question edited Mar 22, 2016 at 11:32 shet_tayyy asked Mar 22, 2016 at 9:08 shet_tayyyshet_tayyy 5,75512 gold badges53 silver badges92 bronze badges 2
  • Can you show us your routes setup? I've handled this before by not adding/removing a class to the body, but instead using a different component (layout) for that specific route – Mark Commented Mar 22, 2016 at 10:37
  • @Mark I have added the routes setup above. – shet_tayyy Commented Mar 22, 2016 at 11:33
Add a comment  | 

2 Answers 2

Reset to default 12

I followed the below given post:

CSS-Only Technique #1

I used

<img src={ImgPathVar} alt="bg" class="bg"> for pages with bg images

and

<div className="bg-color"></div>

After that, I applied the below given css:

// Full page responsive background image
img.bg {
  /* Set rules to fill background */
  min-height: 100%;
  min-width: 1024px;

  /* Set up proportionate scaling */
  width: 100%;
  height: auto;

  /* Set up positioning */
  position: fixed;
  top: 0;
  left: 0;
  z-index: -1;
}

// Full page background color
div.admin-bg {
  /* Set rules to fill background */
  min-height: 100%;
  min-width: 1024px;

  /* Set up proportionate scaling */
  width: 100%;
  height: auto;

  /* Set up positioning */
  position: fixed;
  top: 0;
  left: 0;
  z-index: -1;
  background-color: $admin-bg;
}

@media screen and (max-width: 1024px) { /* Specific to this particular image */
  img.bg {
    left: 50%;
    margin-left: -512px;   /* 50% */
  }

  div.admin-bg {
    left: 50%;
    margin-left: -512px;   /* 50% */
  }
}

Snippet of my actual code:

class AdminDashboard extends Component {
  render() {
    return (
      <div className="admin-dashboard">
        <div className="admin-bg"></div>
        <AdminHeader />
        <Link to="/staff-login">Staff Login</Link>
        {this.props.children}
      </div>
    );
  }
}



class AdminLogin extends Component {
  render() {
    return (
      <div className="admin-dashboard">
        <img src={ImgVar} className="bg" />
        <AdminHeader />
        <Link to="/staff-login">Staff Login</Link>
        {this.props.children}
      </div>
    );
  }
}

Not knowing more about your app, I'd first ask if this should really be part of the same React app -- "admin" sounds like a separate thing that you don't want regular users to have to download.

Assuming you want to keep it, I would actually consider this background as part of the login component, as in add <div className="cover" /> to the existing component styled as:

position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 9999;
background: #F00;

Depending on how you're dealing with scrolling, you could just apply those styles to the whole component as a container in which to center everything.

发布评论

评论列表(0)

  1. 暂无评论