简体   繁体   English

meteor js iron router:路由更改时应用CSS更改

[英]meteor js iron router: apply CSS change whenever route changes

I have homepage, contact page, and several other product related pages in my app. 我的应用程序中有主页,联系页面和其他几个与产品相关的页面。

The goal is to apply a background image to ONLY specifc routes: /homepage and /contact . 目标是将背景图像应用于ONLY特定路线: /homepage/contact If user navigates away from either route, apply some css change. 如果用户离开任一路线,请应用一些css更改。

I am hacking this together now with a helper on my homepage, like so: 我现在正在我的主页上用帮助器一起攻击这个,如下所示:

Template.homepage.rendered = function () {

    var route = Router.current();

    if ( route.path == '/' ) {

        document.body.className = "showBackgroundImage";

    }
};

Partial win here, since this will activate the css, but I need to deactivate when route changes. 部分胜利在这里,因为这将激活css,但我需要在路线改变时停用。 I have also tried the following within my router.js : 我在router.js也尝试了以下router.js

this.route('homepage', {
    path: '/', 
    onAfterAction: function  (argument) {
       // add a class name to body
       document.body.className = "showBackgroundImage";
    }
  });

And CSS in the background standard: 和CSS在后台标准:

.showBackgroundImage { 
  background: url(bgImage.jpg) no-repeat center center fixed; 
}

This is easily done using iron:router layouts and applying a different class to each pages via routing. 这很容易使用iron:router布局并通过路由为每个页面应用不同的类。

First you need to define a main-layout such as : 首先,您需要定义主布局,例如:

<template name="mainLayout">
  <!-- optional navbar yield -->
  {{> yield region="navbar"}}
  <div class="{{currentRouteName}}-page">
    {{> yield}}
  </div>
  <!-- optional footer yield -->
  {{> yield region="footer"}}
</template>

The currentRouteName helper should look something like : currentRouteName助手应该类似于:

UI.registerHelper("currentRouteName",function(){
  return Router.current()?Router.current().route.getName():"";
});

Then I recommend associating a RouteController to your main-layout that will serve as the base class for all of your RouteController s. 然后我建议将RouteController与main-layout相关联,该main-layout将作为所有RouteController的基类。

MainController=RouteController.extend({
  layoutTemplate:"mainLayout",
  // yield navbar and footer templates to navbar and footer regions respectively
  yieldTemplates:{
    "navbar":{
      to:"navbar"
    },
    "footer":{
      to:"footer"
    }
  }
});

Next you need to make sure that your routes use a controller which is derived from MainController . 接下来,您需要确保您的路由使用从MainController派生的控制器。

HomeController=MainController.extend({
  template:"home"
});

Router.map(function(){
  this.route("home",{
    path:"/",
    // optional, by default iron:router is smart enough to guess the controller name,
    // by camel-casing the route name and appending "Controller"
    controller:"HomeController"
  });
});

So now your home route page is surrounded by a div having an "home-page" class, so you can style it in plain CSS (or better yet, using LESS) : 所以现在你的家庭路由页面被一个带有“主页”类的div包围,所以你可以用普通的CSS设置它(或者更好,使用LESS):

.home-page{
  /* your css goes here */
}

If you define other routes, this will work seamlessly, just inherit from MainController and you'll have a page with route-name-page class automatically. 如果你定义了其他路由,这将无缝地工作,只需从MainController继承,你就会自动拥有一个带有route-name-page类的页面。

Of course, you can use the same style for multiple pages, just specify it in CSS : 当然,您可以为多个页面使用相同的样式,只需在CSS中指定它:

.home-page, .contact-page{
  /* your css goes here */
}

You can do really nice stuff with layouts, I highly encourage using them. 你可以用布局做很好的事情,我非常鼓励使用它们。

I've done this exact thing using iron-router and jQuery . 我使用iron-routerjQuery做了这个确切的事情。 Here's what I did. 这就是我做的。

/**
 * Add a background image for certain routes.
 */
var setBackground = function () {
  var route = this.route.name;
  var routes = ['homepage', 'contact'];

  if (_.contains(routes, route)) {
    $('body').addClass('showBackgroundImage');
  } else {
    $('body').removeClass('showBackgroundImage');
  }
};

Router.onBeforeAction(setBackground);

Using Meteor 1.2 and iron-router , here's what worked really easy for me: 使用Meteor 1.2铁路由器 ,这对我来说非常简单:

Router.onBeforeAction(function() {
  $('body').addClass(this.route.options.template);
  this.next();
});

That's it! 而已!

This will take the name from the template your are using and assign it to the body. 这将从您正在使用的模板中获取名称并将其分配给正文。

How easy and convenient!! 多么简单方便!!

If you want to assign a specific name instead of the template name, just replace this.route.options.template with this.route.getName() and give a name to your route. 如果要分配特定名称而不是模板名称,只需将this.route.options.template替换为this.route.getName()并为路径指定名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM