日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

sass在react中的基本使用(實例詳解)_React

作者:鋼牙奧托 ? 更新時間: 2022-11-12 編程語言

1. 安裝sass

較新的版本不需要配置sass-loader等一系列插件,安裝即用。

npm install --save-dev sass

2. 編寫App.tsx中的基本DOM

更改app.css為app.scss,并刪除其中全部內容
使用如下代碼替換app.tsx中的內容

import "./App.scss";

function App() {
  return (
    <div className="App">
      <div className="header">
        <ul>
          <li>導航1</li>
          <li>導航2</li>
          <li>導航3</li>
        </ul>
      </div>
      <div className="container">
        <div className="con_Item1">測試混入的內容</div>
        <div className="con_Item2">測試傳參混入</div>
        <div className="con_Item3">剩余參數混入</div>
        <div className="con_Item4">瀏覽器前綴混入</div>
        <div style={{marginTop:20}}>
          <div className="baseBtn">基類</div>
          <div className="reportBtn">繼承1</div>
          <div className="submitBtn">繼承2</div>
        </div>
      </div>
      <div className="footer">頁腳</div>
    </div>
  );
}
export default App;

3. sass變量

sass變量使用$符號開頭 可以存儲字符串、數字、顏色值、布爾值、列表、null。

下方定義了 若干個sass變量:

$maxWidth: 100%;
$maxHeight: 100%;
$myColor: red;
$myFont: Helvetica, sans-serif;
$myFontSize: 20px;
$headerHight: 49px;

//使用
body,
html {
  width: $maxWidth;
  height: $maxHeight;
  margin: 0;
  padding: 0;
}
#root {
  width: $maxWidth;
  height: $maxHeight;
}
.App {
  width: $maxWidth;
  height: $maxHeight;
  display: flex;
  flex-direction: column;
}

sass變量只能作用于當前的層級:

$myColor:red;

p{
    $myColor:green; //只在p中有效果
    color:$myColor; //為綠色
}
span{
    color:$myColor;//為紅色
}

4. sass中的選擇器嵌套和屬性嵌套

選擇器嵌套

.header {
  width: $maxWidth;
  height: $headerHight;
  background-color: gold;

  ul {
    margin: 0;
    padding: 0;
    height: $maxHeight;
    list-style: none;
    display: flex;
    justify-content: space-around;
  }
  li {
    display: block;
    flex: 1;
    height: 100%;
    line-height: $headerHight;
    background-color: gold;
  }
}

屬性嵌套
很多 CSS 屬性都有同樣的前綴,例如:font-family, font-size 和 font-weight , text-align, text-transform 和 text-overflow。

在 Sass 中,我們可以使用嵌套屬性來編寫它們:

//屬性嵌套
font: {
    family: $myFont;
    size: $myFontSize;
    weight: bold;
}
text: {
    align: center;
    transform: lowercase;
    overflow: hidden;
}

5. sass中的@import和Partials

@import可以讓我們導入其他文件中的內容
包含文件時不需要指定文件后綴,Sass 會自動添加后綴 .scss。

此外,你也可以導入 CSS 文件。

導入后我們就可以在主文件中使用導入文件等變量。

如在app.scss中導入 :

@import "reset";

reset.scss中的樣式會被編譯到app.scss中

Sass Partials

如果你不希望將一個 Sass 的代碼文件編譯到一個 CSS 文件,你可以在文件名的開頭添加一個下劃線。這將告訴 Sass 不要將其編譯到 CSS 文件。

但是,在導入語句中我們不需要添加下劃線。

我們可以將之前定義的變量抽取出來,粘貼到新建的_globals.scss中

$maxWidth: 100%;
$maxHeight: 100%;
$myColor: red;
$myFont: Helvetica, sans-serif;
$myFontSize: 20px;
$headerHight: 49px;

然后再app.scss中引入:

@import "./assets/globals";

#root {
  width: $maxWidth;
  height: $maxHeight;
}

注意:請不要將帶下劃線與不帶下劃線的同名文件放置在同一個目錄下,比如,_colors.scss 和 colors.scss 不能同時存在于同一個目錄下,否則帶下劃線的文件將會被忽略。

6. Sass中的 @mixin 與 @include

@mixin 指令允許我們定義一個可以在整個樣式表中重復使用的樣式。

@include 指令可以將混入(mixin)引入到文檔中。

_globals.scss

//sass 混入
@mixin important-redBgWhiteFt {
  margin: 10px 0;
  height: 50px;
  line-height: 50px;
  text-align: center;
  background-color: red;
  color: white;
  border: 3px solid green;
}
//接收參數的混入
@mixin borderInvert($color: white, $width: 2px) {
  border: $width solid $color;
}
//剩余參數混入
@mixin box-shadow($shadows...) {
  -moz-box-shadow: $shadows;
  -webkit-box-shadow: $shadows;
  box-shadow: $shadows;
}
//瀏覽器前綴使用混入
@mixin transformMix($data) {
    -webkit-transform: $data;
    -ms-transform: $data;
    transform: $data;
}

使用:

app.scss

//包含基礎混入
.con_Item1 {
  //sass包含
  @include important-redBgWhiteFt;
  border-radius: 5px;
}
//包含帶參數的混入
.con_Item2 {
  //sass包含
  @include borderInvert(blue, 5px);
  height: 50px;
  background-color: green;
}
//包含剩余參數的混入
.con_Item3 {
  margin-top: 10px;
  height: 50px;
  @include box-shadow(0px 4px 5px #666, 2px 6px 10px #999);
}
//包含瀏覽器前綴的混入
.con_Item4 {
  @include borderInvert(gold, 2px);
  @include transformMix(rotate(180deg));
  margin-top: 10px;
  height: 50px;
}

7.sass中的繼承 @extend

@extend 指令告訴 Sass 一個選擇器的樣式從另一選擇器繼承。

如果一個樣式與另外一個樣式幾乎相同,只有少量的區別,則使用 @extend 就顯得很有用。

//樣式繼承
//被繼承的基類
.baseBtn {
  @include borderInvert(blue, 1px);
  margin-top: 20px;
  display: block;
  width: 80px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  cursor: pointer;
  
  &:hover{
    background-color: gray;
  }
}
//繼承基礎樣式的按鈕1
.reportBtn{
  @extend .baseBtn;
  background-color: red;
}
//繼承基礎樣式的按鈕2
.submitBtn{
  @extend .baseBtn;
  border-radius: 5px;
}

8. 源代碼

app.tsx:

import "./App.scss";

function App() {
  return (
    <div className="App">
      <div className="header">
        <ul>
          <li>導航1</li>
          <li>導航2</li>
          <li>導航3</li>
        </ul>
      </div>
      <div className="container">
        <div className="con_Item1">測試混入的內容</div>
        <div className="con_Item2">測試傳參混入</div>
        <div className="con_Item3">剩余參數混入</div>
        <div className="con_Item4">瀏覽器前綴混入</div>
        <div style={{marginTop:20}}>
          <div className="baseBtn">基類</div>
          <div className="reportBtn">繼承1</div>
          <div className="submitBtn">繼承2</div>
        </div>
      </div>
      <div className="footer">頁腳</div>
    </div>
  );
}

export default App;

_globals.scss:

$maxWidth: 100%;
$maxHeight: 100%;
$myColor: red;
$myFont: Helvetica, sans-serif;
$myFontSize: 20px;
$headerHight: 49px;

//sass 混入
@mixin important-redBgWhiteFt {
  margin: 10px 0;
  height: 50px;
  line-height: 50px;
  text-align: center;
  background-color: red;
  color: white;
  border: 3px solid green;
}
//接收參數的混入
@mixin borderInvert($color: white, $width: 2px) {
  border: $width solid $color;
}
//剩余參數混入
@mixin box-shadow($shadows...) {
  -moz-box-shadow: $shadows;
  -webkit-box-shadow: $shadows;
  box-shadow: $shadows;
}
//瀏覽器前綴使用混入
@mixin transformMix($data) {
    -webkit-transform: $data;
    -ms-transform: $data;
    transform: $data;
}

app.scss:

@import "./assets/globals";

body,
html {
  width: $maxWidth;
  height: $maxHeight;
  margin: 0;
  padding: 0;
}
#root {
  width: $maxWidth;
  height: $maxHeight;
}
.App {
  width: $maxWidth;
  height: $maxHeight;
  display: flex;
  flex-direction: column;
}

// 選擇器嵌套
.header {
  width: $maxWidth;
  height: $headerHight;
  background-color: gold;

  ul {
    margin: 0;
    padding: 0;
    height: $maxHeight;
    list-style: none;
    display: flex;
    justify-content: space-around;
  }
  li {
    display: block;
    flex: 1;
    height: 100%;
    line-height: $headerHight;
    background-color: gold;

    //屬性嵌套
    font: {
      family: $myFont;
      size: $myFontSize;
      weight: bold;
    }
    text: {
      align: center;
      transform: lowercase;
      overflow: hidden;
    }
  }
}

.container {
  flex: 1;
  background-color: pink;
}
//包含基礎混入
.con_Item1 {
  //sass包含
  @include important-redBgWhiteFt;
  border-radius: 5px;
}
//包含帶參數的混入
.con_Item2 {
  //sass包含
  @include borderInvert(blue, 5px);
  height: 50px;
  background-color: green;
}
//包含剩余參數的混入
.con_Item3 {
  margin-top: 10px;
  height: 50px;
  @include box-shadow(0px 4px 5px #666, 2px 6px 10px #999);
}
//包含瀏覽器前綴的混入
.con_Item4 {
  @include borderInvert(gold, 2px);
  @include transformMix(rotate(180deg));
  margin-top: 10px;
  height: 50px;
}

//樣式繼承
//被繼承的基類
.baseBtn {
  @include borderInvert(blue, 1px);
  margin-top: 20px;
  display: block;
  width: 80px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  cursor: pointer;
  
  &:hover{
    background-color: gray;
  }
}
//繼承基礎樣式的按鈕1
.reportBtn{
  @extend .baseBtn;
  background-color: red;
}
//繼承基礎樣式的按鈕2
.submitBtn{
  @extend .baseBtn;
  border-radius: 5px;
}

.footer {
  width: $maxWidth;
  height: 49px;
  text-align: center;
  line-height: 49px;
}

原文鏈接:https://www.cnblogs.com/sunyan97/p/16715974.html

欄目分類
最近更新