Translate

2020年7月17日 星期五

background

網頁上要秀一張圖,第一個想到的是 <img src=xxx> 這個方式,後來才發現有個更好用的,叫做 background-image , 雖然知道有些特別的用法,卻一直沒有去深入瞭解, 所以在這做個小筆記!


<style>
/*
   background 可以設定很多張圖片,只需用逗號分隔, 
   上下層是由上而下,第一個圖片會在最上方
*/
.a07171_aa
{
  width:20em;
  height:10em;
  background-color: #ff0;
  background-image: url(https://yiharng.github.io/bomb.png)
                   ,url(https://yiharng.github.io/bird.jpg);
  background-position: 0em 0em,2em 0em;
  background-size: 5em 5em,16em 10em;
  background-repeat: repeat, no-repeat;
}
/*
   可以用下面這個方式合併來寫
*/
.a07171_bb
{
  width:20em;
  height:10em;
  background:url(https://yiharng.github.io/bomb.png) 0em 0em/5em 5em repeat
      , #ff0 url(https://yiharng.github.io/bird.jpg) 2em 0em/16em 10em no-repeat;

  /*
      注意那個 #ff0 背景顏色設定,必須在最後一個才能設定
  */
}
</style>

<div class="a07171_aa">
</div>
<br>
<div class="a07171_bb">
</div>


背景圖片不一定要用圖片,也可以用 repeating-linear-gradient 和 repeating-radial-gradient 來產生線性漸層或圓形漸層的背景, 如果再控制好大小和座標,基本上可以用來畫圖了


<style>
.a07172_c1
{
  width:20em;
  height:10em;
  background-image: 
    repeating-linear-gradient(0deg, 
      #00000088,#00000055 1em, 
      transparent 1em,transparent 3em)
    ,repeating-linear-gradient(90deg, 
      #00000088,#00000055 1em, 
      transparent 1em,transparent 3em)
    ,repeating-radial-gradient(
      #f00, #ff0 10%, #0f0 15%,#f00 20%);
  background-position:0em 0em,0em 0em,10em 1em;
  background-size:auto auto,auto auto,5em 5em;
  background-repeat:no-repeat;
}
</style>

<div class="a07172_c1">
</div>

repeating-linear-gradient 很好玩的地方就是, 他自帶 repeat 功能,可以很容易實現尺規的繪製


<style>
.a07173_c1
{
  width:6.1em;
  height:1em;
  background: 
    repeating-linear-gradient(90deg, #000,#000 0.1em,#0000 0.1em,#0000 1em) 0em 0em,
    repeating-linear-gradient(90deg, #000,#000 0.05em,#0000 0.05em,#0000 1em) 0.525em 0.5em/auto 1em,
    repeating-linear-gradient(90deg, #000,#000 0.02em,#0000 0.02em,#0000 0.1em) 0.04em 0.7em/auto 1em
    ;
  background-repeat:no-repeat;
  font-size:3em;
}
</style>

<div class="a07173_c1">
</div>

repeating-radial-gradient 可以用來畫圓,加上 ::before 及 ::after 用邊框來畫三角形,就可以畫出之前靠邊框做不到的箭頭


<style>
.a07174_c1
{
  width:10em;
  height:10em;
  background: 
    repeating-linear-gradient(0deg, #0000,#0000 40%,#fff 40%,#fff 60%) 0em 0em,
    repeating-radial-gradient(#0000, #0000 40%, #0bb 0%, #0bb 60%) 0em 0em;
  background-repeat:no-repeat;
  background-color:#fff;
  position:relative;
  border-radius:100em;
  animation:a07174_a1 3s 0s linear infinite;
}
.a07174_c1::before
{
  content:"";
  position:absolute;
  width:0;
  height:0;
  border:#000 solid 1.3em;
  border-color:#0000 #0bb #0bb #0000;
  left:68%;
  top:20%;
  transform:rotate(30deg);
}
.a07174_c1::after
{
  content:"";
  position:absolute;
  width:0;
  height:0;
  border:#000 solid 1.3em;
  border-color:#0bb #0000 #0000 #0bb;
  left:6%;
  top:53%;
  transform:rotate(30deg);
}
@keyframes a07174_a1
{
  100%
  {
    transform:rotate(360deg);
  }
}
</style>

<div class="a07174_c1">
</div>

repeating-radial-gradient 的詳細說明

參考網址:https://www.runoob.com/cssref/func-repeating-radial-gradient.html

repeating-radial-gradient(shape size at position, start-color, ..., last-color);

shape : ellipse (預設), circle
size : farthest-corner (預設), closest-side, closest-corner, farthest-side

可以利用這個來晝半圓


<style>
.a07175_c1
{
  width:10em;
  height:10em;
  background: 
/*
    完整寫法 :
    repeating-radial-gradient(ellipse farthest-corner at 50% 100%
*/      
    repeating-radial-gradient(at 50% 100%
      , #0000
      , #0000 40%
      , #a0f  40%
      , #0ff  48%
      , #0f0  50%
      , #ff0  52%
      , #f00  60%
    ) 0em 0em/10em 5em;
  background-repeat:no-repeat;
  background-color:#9ff;
}
</style>

<div class="a07175_c1">
</div>

background-attachment

scroll - 預設, 就是像牆壁的背景,不會跟著捲動而移動
local - 背景圖片會隨著捲動而移動
fixed - 背景圖片的位置是固定在螢幕上的,不會隨著 div 移動而移動

在 iPhone 上似忽只有 scroll 其它的不支援

background-attachment





This is a background test.
This is a background test.
This is a background test.
 
fixed

This is a background test.
 
scroll/local

This is a background test.
This is a background test.
This is a background test.
This is a background test.
This is a background test.

background-clip

背景的裁切,最好玩的地方是可以用文字裁切,可以做文字的特效

在 iPhone 和 Mac 的 safari 對於 text 的支援有點不一樣

background-clip





This is a background test.

沒有留言:

張貼留言