Vue js更改背景颜色很困难。

7

我试图将着陆页面的背景颜色设置为橙色,但问题在于当前的设置会使得所有页面都变成橙色。

我尝试给着陆页面添加scoped,这样它就只会对该页面进行样式设置,但这么做之后整个页面都不再是橙色了。

最终目标是只影响着陆页面。

我已经尝试过使用 HTML 和 Body 代替 ' * ',但它们在这种情况下也无法正常工作。

landingpage.vue

<template>
    <div class="container">
        <div class=landing>
            <h1>Hello.</h1>
            <router-link to="/work">
                <button id="work" type="button">See my work</button>
            </router-link>
            <router-link to="/home">
                <button id="home" type="button">Go home</button>
            </router-link>
        </div>
    </div>
</template>

<script>
export default {
  name: 'Landing',
};
</script>

<style scoped>
* {
    background: orange;
}

h1 {
    margin-top: 100px;
    text-align: center;
    font-size: 80px;
    color: green;
}
#work {
    color: green;
    border: none;
    font-size: 25px;
    text-decoration: none;
    margin: 0;
    position: absolute;
    top: 50%;
    left: 50%;
    -ms-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
}
#work:hover {
    color: white;
}
#home{
    color: green;
    border: none;
    font-size: 15px;
    text-decoration: none;
    margin: 0;
    position: absolute;
    top: 70%;
    left: 50%;
    -ms-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
}
#home:hover {
    color: white;
}
</style>
4个回答

5

HTMLbody位于应用程序之外(+ Vue Router = SPA => 当您从page-1转到page-2时,body/html不会重新渲染)。

"问题一" - SPA

单文件组件- bodyhtml样式仅在页面刷新时应用(例如,从page-1转到page-2并点击F5):

<!-- page_2 -->
<style>
   body{
    background: orange; /* not apply if you go from page-1 to page-2 */
   }
</style>

"问题二" - "超出作用域"

scoped == CSS 仅适用于当前组件的元素。 body 不属于 当前作用域组件的一部分。

Hello World 解决方案

解决此问题最基本的 "hello world" 非动态想法是使用生命周期钩子 - 在 created 中通过简单的 JS 更改 body 的背景色, 在 destroyed 中删除该背景色。

<script>
export default {
  name: "orange-page",
  created: function () {
    document.body.style.backgroundColor = "orange";
  },
  destroyed: function () {
    document.body.style.backgroundColor = null;
  },
};
</script>

"min:100vh app"

一个更好的想法是在你的#app中添加“wrapper”+样式min:100vh(#app将覆盖整个body/html ***设置:body{margin:0;})。

相关示例:https://www.pluralsight.com/guides/change-page-background-color-each-route

更多想法:

在Vue路由器中更改body样式

CSS

通常使用:

body{
   background: orange;
}

不是 (* ==> 选择所有元素):

2
“null”是“backgroundColor”的无效值。有趣的是,它实际上可以工作(因为它是无效的,浏览器会默认应用其余规则或其默认值“transparent”)。但是,通过传递无效的CSS值,您确实会导致一个静默错误。理论上,正确的方法是清除当前“backgroundColor”规则的样式对象。 - tao
@tao 很好的发现。我认为将值设置为白色或者初始值可能会更好。 - Ezra Siton
@mike,你可能在切换时修复了另一个小错误,而没有意识到(也许是缺少逗号或分号!?)。created: function () { ... }VueConstructor 中的确等同于 created() { ... }。我个人更喜欢第二种语法,因为它更短,但由于它们都产生相同的结果,我很少推荐其中任何一种。这两种语法对于任何 hookcomputedmethod 都是有效的(包括 computed 中的 setter/getter 函数)。 - tao
@EzraSiton,在destroyed中将值设置为任何内容(“white”,“transparent”或“initial”)在技术上是错误的。假设某些CSS应用了bodybackground-color的属性为red,您的组件在mounted中将其设置为orange,然后在destroyed中将其设置为transparentbody现在具有与以前不同的背景颜色。您的组件应仅取消应用的规则。最清洁的方法是在组件内部添加一个类,并将其样式化,然后在destroyed中删除该类。否则,在某些情况下,您可能无法恢复body的CSS初始状态。 - tao
@tao 去掉/添加类的想法不错 - 同意(非常干净)。 - Ezra Siton
显示剩余4条评论

0

使用新的组合 API,代码看起来像这样:

setup() {
  var color = "#343E3D";
  onMounted(() => {
    console.log("mounted!");
    document.body.style.backgroundColor = color;
  });
  onUnmounted(() => {
    console.log("unmounted!");
    document.body.style.backgroundColor = null;
  });
  return {};
},

这两个生命周期钩子将在组件加载/卸载时更改背景。我曾经遇到一个类似的问题,一个页面需要与网站的其他部分不同的背景。


0
你还可以动态绑定类,使其仅适用于着陆页路由。
因此,在 App.vue 或充当应用程序外壳的任何上层组件中,您可以执行以下操作:
/* ==== in App.vue (or Landing Page parent Component) ==== */
<template>
  <div :class={orangeBackground: $route.path.includes('/landingPagePath')}>
     // other stuff could be here - like the router view or whatever
     <router-view> </router-view>
  </div>   
</template>
<script>
</script>
<style>
.orangeBackground {
    background: orange;
}
</style>

基本思路是整个DOM都包含在父组件中,但只有当浏览器位于着陆页路由时,我们才应用.orangeBackground类。

0

不是

* {
    background: orange;
}

html {
    background: orange;
}

或者

body {
    background: orange;
}

在“*”之前,我尝试了几种不同的方法。我尝试了body和html,但两者都使我的整个页面变成了白色。 - user14875201

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接