首页 > web前端 > css教程 > 正文

如何使用纯CSS实现一把剪刀的效果(附源码)

不言
发布: 2018-09-10 15:38:56
原创
2375 人浏览过

本篇文章给大家带来的内容是关于如何使用纯CSS实现一把剪刀的效果(附源码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

效果预览

3878475719-5b95ba3809143_articlex.gif

源代码下载

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

代码解读

定义 dom,容器中包含 2 个 .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 {
    margin: 0;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
}
登录后复制

定义容器尺寸,其中 outline 是辅助线:

.scissors {
    width: 21em;
    height: 7em;
    outline: 1px dashed;
}
登录后复制

定义半边剪刀的尺寸,其中 outline 是辅助线:

.scissors {
    position: relative;
}

.half {
    position: absolute;
    width: inherit;
    height: 4em;
    outline: 1px dashed red;
}
登录后复制

画出刀柄:

.handle {
    position: absolute;
    box-sizing: border-box;
    width: 8em;
    height: inherit;
    border: 1em solid #333;
    border-radius: 2em;
}
登录后复制

画出刀,用圆角属性画出了顶部的刀尖:

.blade {
    position: absolute;
    width: 15em;
    height: 1em;
    background-color: silver;
    top: 3em;
    left: 6em;
    border-radius: 0 0 1em 0;
    z-index: -1;
}
登录后复制

用伪元素在刀的底部画一个三角形,使刀与刀柄连接得更牢固:

.blade::before {
    content: '';
    position: absolute;
    border-style: solid;
    border-width: 0 1.8em 1em 1.8em;
    border-color: transparent transparent silver transparent;
    top: -1em;
    left: 0.2em;
}
登录后复制

使半边刀倾斜:

.half {
    transform-origin: 45% bottom;
    transform: rotate(15deg);
}
登录后复制

利用 scale() 函数画出剪刀的另一半:

.half {
    transform-origin: 45% bottom;
    transform: rotate(calc(15deg * var(--direction))) scaleY(var(--direction));
}

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

.half:nth-child(2) {
    --direction: -1;
    top: -1em;
}
登录后复制

画出连接左右半边的铆钉:

.joint {
    position: absolute;
    width: 0.7em;
    height: 0.7em;
    background-color: #333;
    border-radius: 50%;
    top: calc(50% - 0.7em / 2);
    left: 45%;
}
登录后复制

增加动画鼠标悬停时的动画效果:

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

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

    40%, 80% {
        transform: rotate(calc(5deg * var(--direction))) scaleY(var(--direction));
    }
}
登录后复制

最后,别忘了删掉辅助线。

大功告成!

相关推荐:

如何使用纯CSS实现条纹错觉的动画效果(附源码)

如何使用css实现中国结的效果(代码)


以上是如何使用纯CSS实现一把剪刀的效果(附源码)的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!