1. 如何用純 CSS 創(chuàng)作一把剪刀

        共 4614字,需瀏覽 10分鐘

         ·

        2021-03-27 08:06

        問:如何用純 CSS 創(chuàng)作一把剪刀


        動(dòng)態(tài)效果預(yù)覽


        復(fù)制跳轉(zhuǎn)下方連接可以去查看動(dòng)態(tài)效果

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

        源代碼下載

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

        代碼解讀

        定義 dom,容器中包含 2 個(gè) .half 元素,各表示剪刀的半邊,它的子元素 handle 表示刀柄,blade 表示刀,最后的 .joint 表示連接左右兩部分鉚釘:

        <figure class="scissors">
            <div class="half">
                <span class="handle"></span>
                <span class="blade"></span>
            </div>
            <div class="half">
                <span class="blade"></span>
                <span class="handle"></span>
            </div>
            <div class="joint"></div>
        </figure>

        居中顯示:

        body {
            margin0;
            height100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        定義容器尺寸,其中 outline 是輔助線:

        .scissors {
            width21em;
            height7em;
            outline1px dashed;
        }

        定義半邊剪刀的尺寸,其中 outline 是輔助線:

        .scissors {
            position: relative;
        }

        .half {
            position: absolute;
            width: inherit;
            height4em;
            outline1px dashed red;
        }

        畫出刀柄:

        .handle {
            position: absolute;
            box-sizing: border-box;
            width8em;
            height: inherit;
            border1em solid #333;
            border-radius2em;
        }

        畫出刀,用圓角屬性畫出了頂部的刀尖:

        .blade {
            position: absolute;
            width15em;
            height1em;
            background-color: silver;
            top3em;
            left6em;
            border-radius0 0 1em 0;
            z-index: -1;
        }

        用偽元素在刀的底部畫一個(gè)三角形,使刀與刀柄連接得更牢固:

        .blade::before {
            content'';
            position: absolute;
            border-style: solid;
            border-width0 1.8em 1em 1.8em;
            border-color: transparent transparent silver transparent;
            top: -1em;
            left0.2em;
        }

        使半邊刀傾斜:

        .half {
            transform-origin45% bottom;
            transformrotate(15deg);
        }

        利用 scale() 函數(shù)畫出剪刀的另一半:

        .half {
            transform-origin45% bottom;
            transformrotate(calc(15deg * var(--direction))) scaleY(var(--direction));
        }

        .half:nth-child(1) {
            --direction1;
            top0;
        }

        .half:nth-child(2) {
            --direction: -1;
            top: -1em;
        }

        畫出連接左右半邊的鉚釘:

        .joint {
            position: absolute;
            width0.7em;
            height0.7em;
            background-color#333;
            border-radius50%;
            topcalc(50% - 0.7em / 2);
            left45%;
        }

        增加動(dòng)畫鼠標(biāo)懸停時(shí)的動(dòng)畫效果:

        .scissors:hover .half {
            animation: cut 2s ease-out;
        }

        @keyframes cut {
            20%, 60% {
                transformrotate(calc(30deg * var(--direction))) scaleY(var(--direction));
            }

            40%, 80% {
                transformrotate(calc(5deg * var(--direction))) scaleY(var(--direction));
            }
        }

        最后,別忘了刪掉輔助線。

        原文出處:https://segmentfault.com/a/1190000016331561

        CSS真是神秘~

        針對(duì)這個(gè)題目,你的解決方案又是什么呢?

        不妨在下面的留言給出,學(xué)習(xí)共勉下~

        碼字不易,走過路過可否點(diǎn)個(gè) 在看,點(diǎn)個(gè)贊??先謝謝了!

        ε=ε=ε=┏(゜ロ゜;)┛

        最后

        ?

        有疑問的同學(xué) 歡迎 評(píng)論區(qū)討論,也歡迎大家加入我的前端技術(shù)交流群 來(lái)討論。搜索《前端陽(yáng)光》公眾號(hào),回復(fù)加群吧!


        瀏覽 73
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
          
          

            1. 蜜乳网站 | 五月丁香六 | 国模乳神张雪馨私拍视频 | 黄片AV在线 | 肏屄影片 |