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

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

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

作者:鋼牙奧托 ? 更新時(shí)間: 2022-11-12 編程語(yǔ)言

1. 安裝sass

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

npm install --save-dev sass

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

更改app.css為app.scss,并刪除其中全部?jī)?nèi)容
使用如下代碼替換app.tsx中的內(nèi)容

import "./App.scss";

function App() {
  return (
    <div className="App">
      <div className="header">
        <ul>
          <li>導(dǎo)航1</li>
          <li>導(dǎo)航2</li>
          <li>導(dǎo)航3</li>
        </ul>
      </div>
      <div className="container">
        <div className="con_Item1">測(cè)試混入的內(nèi)容</div>
        <div className="con_Item2">測(cè)試傳參混入</div>
        <div className="con_Item3">剩余參數(shù)混入</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">頁(yè)腳</div>
    </div>
  );
}
export default App;

3. sass變量

sass變量使用$符號(hào)開頭 可以存儲(chǔ)字符串、數(shù)字、顏色值、布爾值、列表、null。

下方定義了 若干個(gè)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變量只能作用于當(dāng)前的層級(jí):

$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 中,我們可以使用嵌套屬性來(lái)編寫它們:

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

5. sass中的@import和Partials

@import可以讓我們導(dǎo)入其他文件中的內(nèi)容
包含文件時(shí)不需要指定文件后綴,Sass 會(huì)自動(dòng)添加后綴 .scss。

此外,你也可以導(dǎo)入 CSS 文件。

導(dǎo)入后我們就可以在主文件中使用導(dǎo)入文件等變量。

如在app.scss中導(dǎo)入 :

@import "reset";

reset.scss中的樣式會(huì)被編譯到app.scss中

Sass Partials

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

但是,在導(dǎo)入語(yǔ)句中我們不需要添加下劃線。

我們可以將之前定義的變量抽取出來(lái),粘貼到新建的_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;
}

注意:請(qǐng)不要將帶下劃線與不帶下劃線的同名文件放置在同一個(gè)目錄下,比如,_colors.scss 和 colors.scss 不能同時(shí)存在于同一個(gè)目錄下,否則帶下劃線的文件將會(huì)被忽略。

6. Sass中的 @mixin 與 @include

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

@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;
}
//接收參數(shù)的混入
@mixin borderInvert($color: white, $width: 2px) {
  border: $width solid $color;
}
//剩余參數(shù)混入
@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

//包含基礎(chǔ)混入
.con_Item1 {
  //sass包含
  @include important-redBgWhiteFt;
  border-radius: 5px;
}
//包含帶參數(shù)的混入
.con_Item2 {
  //sass包含
  @include borderInvert(blue, 5px);
  height: 50px;
  background-color: green;
}
//包含剩余參數(shù)的混入
.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 一個(gè)選擇器的樣式從另一選擇器繼承。

如果一個(gè)樣式與另外一個(gè)樣式幾乎相同,只有少量的區(qū)別,則使用 @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;
  }
}
//繼承基礎(chǔ)樣式的按鈕1
.reportBtn{
  @extend .baseBtn;
  background-color: red;
}
//繼承基礎(chǔ)樣式的按鈕2
.submitBtn{
  @extend .baseBtn;
  border-radius: 5px;
}

8. 源代碼

app.tsx:

import "./App.scss";

function App() {
  return (
    <div className="App">
      <div className="header">
        <ul>
          <li>導(dǎo)航1</li>
          <li>導(dǎo)航2</li>
          <li>導(dǎo)航3</li>
        </ul>
      </div>
      <div className="container">
        <div className="con_Item1">測(cè)試混入的內(nèi)容</div>
        <div className="con_Item2">測(cè)試傳參混入</div>
        <div className="con_Item3">剩余參數(shù)混入</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">頁(yè)腳</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;
}
//接收參數(shù)的混入
@mixin borderInvert($color: white, $width: 2px) {
  border: $width solid $color;
}
//剩余參數(shù)混入
@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;
}
//包含基礎(chǔ)混入
.con_Item1 {
  //sass包含
  @include important-redBgWhiteFt;
  border-radius: 5px;
}
//包含帶參數(shù)的混入
.con_Item2 {
  //sass包含
  @include borderInvert(blue, 5px);
  height: 50px;
  background-color: green;
}
//包含剩余參數(shù)的混入
.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;
  }
}
//繼承基礎(chǔ)樣式的按鈕1
.reportBtn{
  @extend .baseBtn;
  background-color: red;
}
//繼承基礎(chǔ)樣式的按鈕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

欄目分類
最近更新