每日CSS_霓虹灯按钮悬停效果

2020_12_20

1. 代码解析

1.1 html 代码片段解析

		<a href="#">
<span></span>
<span></span>
<span></span>
<span></span>
霓虹灯 按钮
</a>

这四个 span 标签用来模拟上下左右四根线

1.2 CSS 代码片段解析

  • body 代码

    body{
    margin: 0;
    padding: 0;
    /* flex 布局 */
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background: #031321;
    font-family: 华文琥珀;
    }

    应用到了 flex 布局, 将内容显示在屏幕中央

  • a 标签

    a{
    /* 为绝对定位做参考点 */
    position: relative;
    display: inline-block;
    /* 将文字与线分割开 */
    padding: 15px 30px;
    color: #2196f3;
    /* 文字间隔 */
    letter-spacing: 4px;
    text-decoration: none;
    font-size: 24px;
    /* 隐藏越界的线 */
    overflow: hidden;
    /* 附在 a 上的动画, 触发时和回退时均有效 */
    transition: 0.2s;
    }

    需要注意的是 transition, 放在 a 标签而不是 a:hover 标签里的原因是, 放在 a 标签, 移入移出均有效果, 而放在 a:hover 中, 移入有效果, 移出没效果

  • a:hover

    a:hover{
    color: #255784;
    background: #2196f3;
    /* 多个阴影模拟霓虹灯 */
    box-shadow:
    0 0 10px #2196f3,0 0 40px #2196f3,0 0 10px #2196f3,0 0 80px #2196f3;
    /* 有延迟 */
    transition-delay: 1s;
    }

    这里有三个动画, color, backgroud 和 box-shadow, background 由透明变为 #2196f3, 效果对比如下

  • 最重要的移动线条

    a span{
    position: absolute;
    display: block;
    }
    a span:nth-child(1){
    top: 0;
    left: -100%;
    width: 100%;
    height: 2px;
    background: linear-gradient(90deg, transparent, #2196f3);
    }
    a:hover span:nth-child(1){
    left: 100%;
    transition: 1s;
    }

    首先, 将所有的 span 设为绝对定位, a 为相对定位. 将上方的线从左边移动到右边, 在 a 选择器中设置了 overflow: hidden, 因此越界的线被隐藏了. 将 left 固定为 0 的效果如下.

    当left从 -100% 到 100% 是就有了闪过的效果,

  • 再介绍一个右边的线条, 其余的一样

    a span:nth-child(2){
    right: 0;
    top: -100%;
    width: 2px;
    height: 100%;
    background: linear-gradient(180deg, transparent, #2196f3);
    }
    a:hover span:nth-child(2){
    top: 100%;
    transition: 1s .25s;
    }

    注意, background, 是一根竖线, 宽 2px, 高 100%, 若 top 一直为 0 时效果如下

2. 源码如下

  1. html

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet" href="2020_12_20.css">
    </head>
    <body>
    <a href="#">
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    霓虹灯 按钮
    </a>
    </body>
    </html>
  2. css

    body{
    margin: 0;
    padding: 0;
    /* flex 布局 */
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background: #031321;
    font-family: 华文琥珀;
    }
    a{
    /* 为绝对定位做参考点 */
    position: relative;
    display: inline-block;
    /* 将文字与线分割开 */
    padding: 15px 30px;
    color: #2196f3;
    /* 文字间隔 */
    letter-spacing: 4px;
    text-decoration: none;
    font-size: 24px;
    overflow: hidden;
    /* 附在 a 上的动画, 触发时和回退时均有效 */
    transition: 0.2s;
    }
    a:hover{
    color: #255784;
    background: #2196f3;
    /* 多个阴影模拟霓虹灯 */
    box-shadow:
    0 0 10px #2196f3,0 0 40px #2196f3,0 0 10px #2196f3,0 0 80px #2196f3;
    /* 有延迟 */
    transition-delay: 1s;
    }
    a span{
    position: absolute;
    display: block;
    }
    a span:nth-child(1){
    top: 0;
    left: -100%;
    width: 100%;
    height: 2px;
    background: linear-gradient(90deg, transparent, #2196f3);
    }
    a:hover span:nth-child(1){
    left: -100%;
    transition: 1s;
    }
    a span:nth-child(3){
    bottom: 0;
    left: 100%;
    width: 100%;
    height: 2px;
    background: linear-gradient(270deg, transparent, #2196f3);
    }
    a:hover span:nth-child(3){
    left: -100%;
    transition: 1s .5s;
    }
    a span:nth-child(2){
    right: 0;
    top: -100%;
    width: 2px;
    height: 100%;
    background: linear-gradient(180deg, transparent, #2196f3);
    }
    a:hover span:nth-child(2){
    top: 100%;
    transition: 1s .25s;
    }
    a span:nth-child(4){
    left: 0;
    top: 100%;
    width: 2px;
    height: 100%;
    background: linear-gradient(360deg, transparent, #2196f3);
    }
    a:hover span:nth-child(4){
    top: -100%;
    transition: 1s .75s;
    }

每日CSS_霓虹灯按钮悬停效果的更多相关文章

  1. 前端每日实战:126# 视频演示如何用纯 CSS 创作小球变矩形背景的按钮悬停效果

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/yxbEzJ 可交互视频 此视频是可 ...

  2. 每日CSS_滚动页面动画效果

    每日CSS_滚动页面动画效果 2021_1_13 源码链接 1. 代码解析 1.1 html 代码片段 <section> <h2>开 始 滑 动</h2> < ...

  3. MFC 使用位图按钮,并且设置按钮的鼠标悬停效果

    系统环境:Windows 10软件环境:Visual C++ 2013 SP1本次目的:使用位图按钮,并且设置按钮的鼠标悬停效果 在用MFC开发时,界面是比较不好开发的一块.VC中自带了CBitmap ...

  4. 每日CSS_实时时钟效果

    每日CSS_实时时钟效果 2020_12_22 源码链接 1. 代码解析 1.1 html 代码片段 <div class="clock"> <div class ...

  5. 每日CSS_发光文本效果

    每日CSS_发光文本效果 2020_12_22 源码 1. 代码解析 1.1 html 代码片段 <h1> <span>今</span> <span>天 ...

  6. css 按钮悬停效霓虹灯特效

    css 按钮悬停效霓虹灯特效 <!DOCTYPE html> <html lang="en"> <head> <meta charset=

  7. Hover.css:一组超实用的 CSS3 悬停效果和动画

    Hover.css 是一套基于 CSS3 的鼠标悬停效果和动画,这些可以非常轻松的被应用到按钮.LOGO 以及图片等元素.所有这些效果都是只需要单一的标签,必要的时候使用 before 和 after ...

  8. 每日CSS_仿苹果平滑开关按钮

    每日CSS_仿苹果平滑开关按钮 2020_12_24 源码 1. 代码解析 1.1 html 代码解析 <div class="checkbox"> <div c ...

  9. 每日CSS_纯CSS制作进度条

    每日CSS_纯CSS制作进度条 2020_12_26 源码 1. 代码解析 1.1 html 代码解析 设置整个容器 <div class="container"> . ...

随机推荐

  1. Mac升级资料丢失怎么办?EasyRecovery能恢复嘛?

    随着越来越多的用户选择性能更高的mac笔记本来工作,一般情况下,为了保证用户有一个很好的使用体验,Mac系统会在一定的时间内进行系统的更新,弥补前一个版本的不足.结果就有一些用户反应Mac升级后,电脑 ...

  2. python中操作excel数据 封装成一个类

    本文用python中openpyxl库,封装成excel数据的读写方法 from openpyxl import load_workbook from openpyxl.worksheet.works ...

  3. Linux中配置环境变量

    Linux中环境变量的搭建(推荐用法) 第一步:进入到/etc/profile.d文件夹下 cd /etc/profile.d 第二步:创建并编辑一个my_env.sh文件 vim my_env.sh ...

  4. 关于Linux虚拟机连接不上网络的问题

    前阵子自学Linux(版本是CentOS6 -VMware ),因为连不上网的问题搁置了一段时间,昨天又重新拾起来,花了一下午时间终于搞定.下面说几点,给自己学习历程一个记录,也希望能帮到其他初学者. ...

  5. http host头攻击漏洞

    原文地址: https://www.zhuyilong.fun/tech/handel_httphost_attack.html 漏洞描述 为了方便的获得网站域名,开发人员一般依赖于HTTP Host ...

  6. php进阶学习-单例设计模式

    什么是单例模式(singleton)? 在整个应用程序的生命周期中,任何一个时刻,单例类的实例都只存在一个,同时这个类还必须提供一个访问该类的全局访问点. 单例模式的特点 一个类只有一个实例 私有克隆 ...

  7. win10 下安装 ubuntu 子系统的完全指北

    最近在搞 C++ 相关的东西,因为在 Linux 下开发会比较流畅舒适,而公司配的电脑都是 windows 的,之前都是在 vmware 中安装个 ubuntu 虚拟机,但这种有时候比有点卡顿.所以今 ...

  8. 01_Activity生命周期及传递数据

    1. Activity的生命周期: 2. Activity启动另一个Activity,并传递数据: package com.example.activitydemo; import android.a ...

  9. 20200427_ls_正在读取目录_输入/输出错误

    环境: 在Centos7.2上挂载了一个2T的移动硬盘, 使用vim 在移动硬盘中编辑 .sh文件, wq的时候提示出错, 然后清空的文件, 可以正常wq出来 [root@localhost yido ...

  10. PyQt(Python+Qt)学习随笔:QToolBox工具箱的currentItemName和tabSpacing属性

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 在Designer中,toolBox的属性中有currentItemName和tabSpacing属 ...