简体   繁体   English

如何改变背景颜色?

[英]how to change color in background?

I looked at these codes on this page https://codepen.io/kevinhartwig/pen/zYWRjWq我在这个页面上查看了这些代码https://codepen.io/kevinhartwig/pen/zYWRjWq

I don't understand this line code:我不明白这一行代码:

entry.target.id === "secondSection" ? "bg-red" : "bg-light-pink";

if I want to add more color to the background page like this page: https://backstagetalks.com/ what can I edit?如果我想像这个页面一样为背景页面添加更多颜色: https://backstagetalks.com/我可以编辑什么? Thank you谢谢

The line:该行:

entry.target.id === "secondSection" ? "bg-red" : "bg-light-pink";

simply means if the target class is "secondSection" then apply the CSS class "bg-red" to it, if not then apply "bg-light-pink" instead.简单地说,如果目标 class 是“secondSection”,则应用 CSS class “bg-red”代替它,如果不是,则应用“bg-light-pink”。 The confusing thing is that "bg-red" class is not actually red.令人困惑的是,“bg-red” class 实际上并不是红色的。 It displays a dark blue color (#2d46b9 is a hexadecimal value that corresponds to a blue color).它显示深蓝色(#2d46b9 是对应于蓝色的十六进制值)。

Each section of the HTML has an ID assigned to it like so: HTML 的每个部分都有一个分配给它的 ID,如下所示:

<section id="secondSection">

This helps to identify the HTML element so that you can evaluate it in the JavaScript code.这有助于识别 HTML 元素,以便您可以在 JavaScript 代码中对其进行评估。 The simplest way to evaluate more colors to each section element is just to add a conditional that checks for which element you are scrolling over and assigning it another class.评估更多 colors 到每个部分元素的最简单方法是添加一个条件来检查您正在滚动的元素并将其分配另一个 class。 You could do this inside the callback (line 10 of your JS code).您可以在回调中执行此操作(JS 代码的第 10 行)。 Like so:像这样:

  let className = ""
  if (entry.target.id == "secondSection") {
    className = "bg-red"
  } else if(entry.target.id == "thirdSection") {
    className = "bg-green"
  } else if(entry.target.id == "fourthSection") {
    className = "bg-light-pink"
  }

Be sure to define a CSS class for "bg-green" or whatever class you choose to assign to that section element.请务必为“bg-green”或您选择分配给该部分元素的任何 class 定义 CSS class 。 You would do this the same way its defined for "bg-red" and the other classes.您可以按照为“bg-red”和其他类定义的相同方式执行此操作。

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

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