前端每日實戰:64# 視頻演示如何用純 CSS 繪製一個足球場

圖片描述

效果預覽

按下右側的「點擊預覽」按鈕能夠在當前頁面預覽,點擊連接能夠全屏預覽。css

https://codepen.io/comehope/pen/BVqXwJhtml

可交互視頻

此視頻是能夠交互的,你能夠隨時暫停視頻,編輯視頻中的代碼。前端

請用 chrome, safari, edge 打開觀看。git

https://scrimba.com/p/pEgDAM/cKbw4Tggithub

源代碼下載

每日前端實戰系列的所有源代碼請從 github 下載:chrome

https://github.com/comehope/front-end-daily-challengesdom

代碼解讀

定義 dom,容器中包含場地,場地中再包含中線、中點、中圈、禁區、罰球點、罰球弧、球門區、角球區等元素:flex

<div class="container">
    <div class="field">
        <span class="halfway-line"></span>
        <span class="centre-circle"></span>
        <span class="centre-mark"></span>
        <span class="penalty-area"></span>
        <span class="penalty-mark"></span>
        <span class="penalty-arc"></span>
        <span class="goal-area"></span>
        <span class="corner-arc"></span>
    </div>
</div>

居中顯示:spa

body {
    margin: 0;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: radial-gradient(sandybrown, maroon);
}

定義容器尺寸:code

.container {
    width: 120em;
    height: 80em;
    background-color: green;
    font-size: 5px;
}

.container span {
    display: block;
}

定義線型:

.container {
    --line: 0.3em solid white;
}

畫出場地邊線:

.container {
    padding: 5em;
}

.field {
    width: inherit;
    height: inherit;
    border: var(--line);
}

畫出中線:

.halfway-line {
    width: calc(120em / 2);
    height: 80em;
    border-right: var(--line);
}

畫出中圈:

.field {
    position: relative;
}

.centre-circle {
    width: 20em;
    height: 20em;
    border: var(--line);
    border-radius: 50%;
    position: absolute;
    top: calc((80em - 20em) / 2);
    left: calc((120em - 20em - 0.3em) / 2);
}

畫出中點:

.centre-mark {
    width: 2em;
    height: 2em;
    background-color: white;
    border-radius: 50%;
    position: absolute;
    top: calc(80em / 2 - 1em);
    left: calc(120em / 2 - 1em + 0.3em / 2);
}

畫出禁區:

.penalty-area {
    width: 18em;
    height: 44em;
    border: var(--line);
    position: absolute;
    top: calc((80em - 44em) / 2);
    left: -0.3em;
}

畫出罰球點:

.penalty-mark {
    width: 2em;
    height: 2em;
    background-color: white;
    border-radius: 50%;
    position: absolute;
    top: calc(80em / 2 - 1em);
    left: calc(12em - 1em);
}

畫出罰球弧:

.penalty-arc {
    width: 20em;
    height: 20em;
    border: var(--line);
    border-radius: 50%;
    position: absolute;
    top: calc((80em - 20em) / 2);
    left: calc(12em - 20em / 2);
}

隱藏罰球弧左側弧線,只留右側弧線:

.field {
    z-index: 1;
}

.penalty-area {
    background-color: green;
}

.penalty-arc {
    z-index: -1;
}

畫出球門區:

.goal-area {
    width: 6em;
    height: 20em;
    border: var(--line);
    position: absolute;
    top: calc((80em - 20em) / 2);
    left: -0.3em;
}

畫出角球區:

.field {
    overflow: hidden;
}

.corner-arc::before,
.corner-arc::after {
    content: '';
    position: absolute;
    width: 5em;
    height: 5em;
    border: 0.3em solid white;
    border-radius: 50%;
    --offset: calc(-5em / 2 - 0.3em);
    left: var(--offset);
}

.corner-arc::before {
    top: var(--offset);
}

.corner-arc::after {
    bottom: var(--offset);
}

把 dom 中的子元素複製出一份,左右兩側各一份:

<div class="container">
    <div class="field">
        <div class="left">
            <span class="halfway-line"></span>
            <span class="centre-circle"></span>
            <span class="centre-mark"></span>
            <span class="penalty-area"></span>
            <span class="penalty-mark"></span>
            <span class="penalty-arc"></span>
            <span class="goal-area"></span>
            <span class="corner-arc"></span>
        </div>
        <div class="right">
            <span class="halfway-line"></span>
            <span class="centre-circle"></span>
            <span class="centre-mark"></span>
            <span class="penalty-area"></span>
            <span class="penalty-mark"></span>
            <span class="penalty-arc"></span>
            <span class="goal-area"></span>
            <span class="corner-arc"></span>
        </div>
    </div>
</div>

右側的樣式與左側相同,只須要水平翻轉便可:

.right {
    position: absolute;
    top: 0;
    left: 50%;
    transform: rotateY(180deg);
}

大功告成!

相關文章
相關標籤/搜索