整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

「动画消消乐|CSS」002.自定义按钮样式

「动画消消乐|CSS」002.自定义按钮样式

?

Hello!小伙伴!

首先非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出~

哈哈 自我介绍一下

昵称:海轰

标签:程序猿一只|C++选手|学生

简介:因C语言结识编程,随后转入计算机专业,有幸拿过国奖、省奖等,已保研。目前正在学习C++/Linux(真的真的太难了~)

学习经验:扎实基础 + 多做笔记 + 多敲代码 + 多思考 + 学好英语!

效果展示

思路

上面效果可以概括为:

  • 鼠标未停留时:红蓝(渐变)背景,正中文字为白色,button四角做了圆角处理
  • 鼠标停留时:button背景变成白色,文字变为蓝色,边框四条直线产生汇聚效果,同时每条直线会有一个阴影的产生。

根据效果图可以得出实现的一些思路:

  • 背景、文字的颜色变化使用hover就可以实现
  • 左/右两边的两条线可以使用button的::before/::after伪类,结合transition,当鼠标停留时,实现两条线的延展
  • 中间的文字使用span标签,需要使用span标签的伪类
  • 上下两条线利用span的伪类::before/::after实现,原理类似左右两边的直线

Demo代码

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <title>Document</title>
</head>
<body>
    <button class="btn"><span>Haihong Pro</span></button>
</body>
</html>

CSS

html,body{
  margin: 0;
  height: 100%;
}
body{
  display: flex;
  justify-content: center;
  align-items: center;
}
.btn{
  width: 390px;
  height: 120px;
  background: radial-gradient(circle,rgba(247,150,192,1) 0%,rgba(118,174,241,1) 100%);
  border: none;
  color: #fff;
  font-family: 'Lato',sans-serif;
  font-weight: 500;
  font-size: 48px;
  border-radius: 10px;
  cursor: pointer;
  position: relative;
  line-height: 120px;
}
.btn:hover{
  background: transparent;
  color: #71aef1;
}
.btn span{
  width: 100%;
  height: 100%;
  position: relative;
  display: block;
}
.btn::before,
.btn::after{
  content: '';
  position: absolute;
  width: 1px;
  height: 0px;
  box-shadow: -1px -1px 20px 0px rgba(255,255,255,1),
  -4px -4px 5px 0px rgba(255,255,255,1),
  10px 10px 20px 0px rgba(0, 0, 0 ,.4),
  6px 6px 5px 0px rgba(0, 0, 0 ,.3);
  padding: 0;
  transition: all 0.8s ease;
}
.btn::before{
  top: 0;
  right: 0;
}
.btn::after{
  bottom:0;
  left: 0;
}
.btn:hover::before,
.btn:hover::after{
  height: 100%;
}
.btn span::before,
.btn span::after{
  content: '';
  position: absolute;
  width: 0px;
  height: 1px;
  box-shadow: -1px -1px 20px 0px rgba(255,255,255,1),
  -4px -4px 5px 0px rgba(255,255,255,1),
  10px 10px 20px 0px rgba(0, 0, 0 ,.4),
  6px 6px 5px 0px rgba(0, 0, 0 ,.3);
  padding: 0;
  transition: all 0.8s ease;
}
.btn span::before{
  top:0;
  left: 0;
}
.btn span::after{
  bottom: 0;
  right: 0;
}
.btn span:hover::before,
.btn span:hover::after{
  width: 100%;
}

疑点详解

1.怎么实现下图中的效果呢?

其实这个利用boder-shadow就可以实现了,对每一条直线都添加一下阴影效果就可以了

 box-shadow:
    -1px -1px 20px 0px rgba(255, 255, 255, 1),
    -4px -4px 5px 0px rgba(255, 255, 255, 1),
    10px 10px 20px 0px rgba(0, 0, 0, .4),
    6px 6px 5px 0px rgba(0, 0, 0, .3);

2.伪类元素的位置

与上一篇文章的区别在于,这里button的两个伪类::before和::after的位置有所变化,分别位于左下和右上

初始width都为1px,height为0

注:这里为了演示,将width/heigth都设置为了10px,背景色为红色,便于观察

然后鼠标停留时,利用过渡transition,将height设置为100%,就可以实现左右两条线的效果了

上下两条直线就是利用span的两个伪元素实现的,原理也是一样的,这里就不再赘述了。

踩坑

1.忘了将span的position设置为relative

2.没有记得将display设置为block

因为span不是块级元素,这里需要的是块级元素,如果没有声明为块级元素,就会出现下面的结果:

注:这里使用了红色背景,以便观察

结语

希望对您有所帮助

如有错误欢迎小伙伴指正~

我是 海轰?(?ˊ?ˋ)?

如果您觉得写得可以的话

请点个赞吧

谢谢支持 ??

这里是云端源想IT,帮你轻松学IT”

嗨~ 今天的你过得还好吗?

幸福从来不曾远离我们

只是有时候它会用试探的方式

看我们还在不在意

- 2024.04.01 -

在CSS的世界里,有些选择器并不像它们的名字那样直接。今天,我们要探索的是两种特殊的选择器:伪类选择器和伪元素选择器。它们虽然名字相似,但功能和用途却大有不同。


下面就让我们一起来了解一下它们是如何在我们的页面布局中扮演着不可或缺的角色的吧。



一、伪类选择器

1、什么是伪类选择器

伪类选择器,顾名思义,是一种特殊的选择器,它用来选择DOM元素在特定状态下的样式。这些特定状态并不是由文档结构决定的,而是由用户行为(如点击、悬停)或元素的状态(如被访问、被禁用)来定义的。


例如,我们可以用伪类选择器来改变链接在不同状态下的颜色,从而给用户以视觉反馈。


2、伪类选择器的语法

selector:pseudo-class {
property: value;
}

a:link {
color: #FF0000;
}

input:focus {
background-color: yellow;
}

注意:伪类名称对大小写不敏感;



3、常用的伪类选择器

下面分别介绍一下比较常用几类伪类选择器:


3.1 动态伪类选择器

这类选择器主要用于描述用户与元素的交互状态。例如:

1):hover:当鼠标悬停在元素上时的样式。

代码示例:将链接的文本颜色改为红色

a:hover {
color: red;
}


2):active:当元素被用户激活(如点击)时的样式。

代码示例:将按钮的背景色改为蓝色

button:active {
background-color: blue;
}


3):focus:当元素获得焦点(如输入框被点击)时的样式。

代码示例:将输入框的边框颜色改为绿色

input:focus {
border-color: green;
}


4):visited:用于设置已访问链接的样式,通常与:link一起使用来区分未访问和已访问的链接。

代码示例:将已访问链接的颜色改为紫色

a:visited {
color: purple;
}


3.2 UI元素状态伪类选择器

这类选择器用于描述元素在用户界面中的状态。例如:


1):enabled和:disabled:用于表单元素,表示元素是否可用。

示例:将禁用的输入框的边框颜色改为灰色

input:disabled {
border-color: gray;
}

2):checked:用于单选框或复选框,表示元素是否被选中。

示例:将选中的单选框的背景色改为黄色

input[type="radio"]:checked {
background-color: yellow;
}


3.3 结构伪类选择器

这类选择器用于根据元素在文档结构中的位置来选择元素。例如:

1):first-child:选取父元素的第一个子元素。

示例:将列表中的第一个项目的背景色改为绿色:

li:first-child {
background-color: green;
}


2):last-child:选取父元素的最后一个子元素。

示例:将列表中的最后一个项目的背景色改为红色:

li:last-child {
background-color: red;
}


3):nth-child(n):选取父元素中第n个子元素。

示例:将列表中的奇数位置的项目的背景色改为蓝色:

li:nth-child(odd) {
background-color: blue;
}


3.4 否定伪类选择器

这类选择器用于排除符合特定条件的元素。例如:


:not(selector):选取不符合括号内选择器的所有元素。

示例:将不是段落的元素的背景色改为灰色:

*:not(p) {
background-color: gray;
}



4、常见应用

  • 设置鼠标悬停在元素上时的样式;
  • 为已访问和未访问链接设置不同的样式;
  • 设置元素获得焦点时的样式;
// 示例:a 标签的四种状态,分别对应 4 种伪类;

/* 未访问的链接 */
a:link {
color: blue;
}

/* 已访问的链接 */
a:visited {
color: red;
}

/* 鼠标悬停链接 */
a:hover {
color: orange;
}

/* 已选择的链接(鼠标点击但不放开时) */
a:active {
color: #0000FF;
}

注意:

  • a 标签的 4 个伪类(4种状态)必须按照一定顺序书写,否则将会失效;
  • a:hover 必须在 CSS 定义中的 a:link 和 a:visited 之后,才能生效;
  • a:active 必须在 CSS 定义中的 a:hover 之后才能生效;
  • 书写顺序为:a:link、a:visited、a:hover、a:active;
  • 记忆方法:love hate - “爱恨准则”;


二、伪元素选择器

1、什么是伪元素选择器

与伪类选择器不同,伪元素选择器是用来选择DOM元素的特定部分,而不是整个元素。它们通常用于处理那些不是由HTML标签直接表示的内容,比如首行文字、首字母或者生成的内容(如内容前面的编号)。


伪元素选择器允许我们对页面上的某些部分进行精确的样式控制,而这些部分在HTML结构中并不存在。



2、伪元素选择器语法

selector::pseudo-element {
property: value;
}


p::first-line {
color: #ff0000;
}

h1::before {
content: '?';
}


3、常用伪元素选择器

伪元素选择器并不是针对真正的元素使用的选择器,而是针对CSS中已经定义好的伪元素使用的选择器,CSS中有如下四种常用伪元素选择器:first-line、 first-letter、 before、after。


3.1 ::first-line

::first-line表示第一行(第一行内容根据屏幕大小来决定显示多少字),例如: p::first-line{}。


代码示例:

<style>
p::first-line{
color: blue;
}
</style>



3.2 ::first-letter

::first-letter表示第一个字母,例如:p::first-letter{}。


代码示例:

<style>
p::first-letter{
font-size: 30px;
color: blueviolet;
}
</style>


3.3 ::before和::after

::before表示元素的开始,::after表示元素的最后,before和after必须结合content属性来使用。


代码示例:

<style>
p::after{
content: "hahaha";
color: red;
}
p::before{
content: "hehehe";
color: coral;
}
</style>

注意:

  • before和after创建一个元素,但是属于行内元素。
  • 新创建的这个元素在文档中是找不到的,所以我们称为伪元素。
  • before在父元素内容的前面创建元素,after在父元素内容的后面插入元素。
  • 伪元素选择器和标签选择器一样,权重为1。

想要快速入门前端开发吗?推荐一个前端开发基础课程,这个老师讲的特别好,零基础学习无压力,知识点结合代码,边学边练,可以免费试看试学,还有各种辅助工具和资料,非常适合新手!点这里前往学习哦!云端源想

三、伪类与伪元素选择器的区别

CSS中的伪类选择器和伪元素选择器都是用来选取DOM中特定元素的选择器。具体区别如下:

  • 伪类的操作对象是文档树中已有的元素,而伪元素则创建了一个文档数外的元素。因此,伪类与伪元素的区别在于:有没有创建一个文档树之外的元素;
  • 伪类本质上是为了弥补常规CSS选择器的不足,以便获取到更多信息;
  • 伪元素本质上是创建了一个有内容的虚拟容器;
  • CSS3 中伪类和伪元素的语法不同;
  • 在 CSS3 中,已经明确规定了伪类用一个冒号来表示,而伪元素则用两个冒号来表示;
  • 可以同时使用多个伪类,而只能同时使用一个伪元素。



总的来说,伪类选择器关注的是元素在特定状态下的样式变化,而伪元素选择器则是通过创建新的元素来实现特定的样式效果。两者都是CSS中非常强大的工具,可以帮助开发者实现复杂的页面布局和动态效果。


伪类选择器和伪元素选择器虽然不是真正的元素,但它们在CSS中扮演着极其重要的角色。了解并熟练运用它们,可以让你的网页更加生动、互动性更强,同时也能更好地控制页面的布局和内容的表现。


我们下期再见!

END

文案编辑|云端学长

文案配图|云端学长

内容由:云端源想分享

adio、checkbox和switch应该是一个比较常用的html标签,尤其是在中后台ERP系统里面更为常见。不过浏览器自带的样式不怎么好看,而且不同浏览器效果也不一样。出于美化和统一视觉效果的需求,自定义样式就被提出来了。

实现思路

纯css实现的主要手段是利用label标签的模拟功能。label的for属性可以关联一个具体的input元素,即使这个input本身不可被用户可见,有个与它对应的label后,用户可以直接通过和label标签交互来替代原生的input——而这给我们的样式模拟留下了空间。简而言之就是:

隐藏原生input,样式定义的过程留给label (那为什么不直接改变checkbox的样式?因为checkbox作为浏览器默认组件,样式更改上并没有label那么方便,很多属性对checkbox都是不起作用的,比如background,而label在样式上基本和div一样'任人宰割')

而在选择事件上,由于css的“相邻选择符(E+F)”的存在,让我们可以直接利用html的默认checkbox,免去了js模拟选择的麻烦。

准备知识

DEMO的部分CSS3属性只写了webkit前缀,所以建议用webkit内核的浏览器查看本页案例,当然只要你给样式补上对应的浏览器前缀,就可以实现更多样式匹配

HTML代码:

 <!-- input的id必须有,这个是label进行元素匹配所必需的 -->
 <!-- 可以看到每个input的id和label的“for”属性对应同一字符串 -->
<input type="checkbox" id="checkbox01" />
<label for="checkbox01"></label>

<input type="checkbox" id="checkbox02" />
<label for="checkbox02"></label>

<input type="checkbox" id="checkbox03" />
<label for="checkbox03"></label>

<input type="checkbox" id="checkbox04" />
<label for="checkbox04"></label>

HTML构建完成,接下来是对应的css:

/* 隐藏所有checkbox */
input[type='checkbox'] {
 display: none;
}
/* 对label进行模拟.背景图片随便拼凑的,不要吐槽品味*/
/* transition效果是做个背景切换效果,这里单纯演示而已,实际上这个过渡不加更自然*/
label {
 display: inline-block;
 width: 60px;
 height: 60px;
 position: relative;
 background: url(//www.chitanda.me/images/blank.png);
 background-position: 0 0px;
 -webkit-transition: background 0.5s linear;
}
/* 利用相邻选择符和checkbox`:checked`的状态伪类来模拟默认选中效果(就是点击后那个勾号的效果) */
/*如果这段代码注释,点击后将没有任何反馈给用户*/
/*因为label本身是没有点击后被选中的状态的,checkbox被隐藏后,这个状态只能手动模拟*/
input[type='checkbox']:checked+label {
 background-position: 0 -60px;
}

上面代码看起来好像也可以了。不过仔细想想,貌似缺了点什么:选项对应的提示文字

对css不了解的新人可能这时候第一反应就是在label后面用p标签或者span标签来添加文字。不过这种方式都不怎么优雅。个人建议用css的::before和::after伪元素(::before和:before是一个东西。不过为了把“伪元素”和“伪类”区分出来,W3C建议的写法是伪元素用::而伪类用:)

/* 伪元素的生效很简单,定义`content`就好,其余的属性和普通div一样 */
label::after {
 content: attr(data-name);
 /*利用attr可以减少css代码量,data-name写在html部分的label属性里*/
 display: inline-block;
 position: relative;
 width: 120px;
 height: 60px;
 left: 100%;
 vertical-align: middle;
 margin: 10px;
}

当然既然可以用::after模拟label的文字,那也就可以用::before模拟label的checkbox样式,这里就不做解析了。

这里提一下伪类和伪元素的区分:

1)伪类:存在的意义是为了通过选择器找到那些不存在于DOM树中的信息以及不能被常规CSS选择器获取到的信息。 伪类由一个冒号:开头,冒号后面是伪类的名称和包含在圆括号中的可选参数。

常用的伪类:

:active 向被激活的元素添加样式。 
:focus 向拥有键盘输入焦点的元素添加样式。 
:hover 当鼠标悬浮在元素上方时,向元素添加样式。 
:link 向未被访问的链接添加样式。 
:visited 向已被访问的链接添加样式。 
:first-child 向元素的第一个子元素添加样式。 
:checked 向选中的控件元素添加样式

2)伪元素:伪元素在DOM树中创建了一些抽象元素,这些抽象元素是不存在于文档语言里的(可以理解为html源码);

注意: css3为了区分伪类和伪元素,规定伪类前面有一个冒号,伪元素前面有两个冒号

常用伪元素:

关于伪元素的讲解,可以参考CSS伪类与伪元素总是傻傻分不清,这份总结够面试用了

::before 为作用元素的第一个子节点插入dom中
::after 为作用元素的最后一个子节点插入dom中
  • 同:都是通过选择器为元素添加样式
  • 异:伪元素会创建一个元素,但不是真正的Html元素,伪类相当于为一个元素创建一个class样式

实例

自定义radio

html代码:

<input type="radio" id="radio">
<label for="radio"></label>

css代码:

input{
 display:none;
}
label {
 display: inline-block;
 width: 30px;
 height: 30px;
 border: 1px solid #333;
 border-radius: 50%;
 position: relative;
}
label::after {
 -webkit-transition: all .5s ease;
 -moz-transition: all .5s ease;
 -o-transition: all .5s ease;
 -ms-transition: all .5s ease;
 transition: all .5s ease;
 cursor: pointer;
 position: absolute;
 width: 16px;
 height: 16px;
 border-radius: 50%;
 top: 50%;
 left: 50%;
 margin-top:-8px;
 margin-left:-8px;
 z-index: 1;
 content: '';
 border:1px solid #333;
}
input:checked+label::after{
 background:red;
}

实现效果:

点击前和点击后:

自定义checkbox

漂亮的checkbox长这样的,看着就很可爱

我们可以不追求那么完美的样式,可以实现下面简单好看的样式就可以

html代码:

<input type="checkbox" id="checkbox">
<label for="checkbox"></label>

css代码:

input{
 display:none;
}
label {
 display: inline-block;
 width: 30px;
 height: 30px;
 border: 1px solid #333;
 position: relative;
}
label::after {
 -webkit-transition: opacity .5s ease;
 -moz-transition: opacity .5s ease;
 -o-transition: opacity .5s ease;
 -ms-transition: opacity .5s ease;
 transition: opacity .5s ease;
 cursor: pointer;
 position: absolute;
 content: '';
 opacity: 0;
}
input:checked+label::after{
 border: 2px solid #d73d32;
 border-top: none;
 border-right: none;
 -webkit-transform: rotate(-45deg);
 -ms-transform: rotate(-45deg);
 transform: rotate(-45deg);
 width:20px;
 height:10px;
 top:50%;
 margin-top:-8px;
 left:50%;
 margin-left:-10px;
 opacity: 1.0;
}

实现效果:

点击前和点击后:

自定义switch

继续分享一个iOS风格的switch开关按钮,样子也非常常见,如图:

主要是使用了<input ?type="checkbox">来模拟实现,具体的HTML:

html 代码:

<label><input class="mui-switch" type="checkbox"> 默认未选中</label>

<label><input class="mui-switch" type="checkbox" checked> 默认选中</label>

<label><input class="mui-switch mui-switch-animbg" type="checkbox"> 默认未选中,简单的背景过渡效果,加mui-switch-animbg类即可</label>

<label><input class="mui-switch mui-switch-animbg" type="checkbox" checked> 默认选中</label>

<label><input class="mui-switch mui-switch-anim" type="checkbox"> 默认未选中,过渡效果,加 mui-switch-anim
类即可</label>

<label><input class="mui-switch mui-switch-anim" type="checkbox" checked> 默认选中</label>

在实际的使用中后来又增加了两个过渡效果,分别加?mui-switch-animbg和mui-switch-anim?类即可,具体效果查看下面的demo页面。

CSS代码(SCSS导出的,排版有些奇怪):

css 代码:

剩下部分

这里给出具体的css,方便大家复制本地实现

<style>
 .mui-switch {
 width: 52px;
 height: 31px;
 position: relative;
 border: 1px solid #dfdfdf;
 background-color: #fdfdfd;
 box-shadow: #dfdfdf 0 0 0 0 inset;
 border-radius: 20px;
 border-top-left-radius: 20px;
 border-top-right-radius: 20px;
 border-bottom-left-radius: 20px;
 border-bottom-right-radius: 20px;
 background-clip: content-box;
 display: inline-block;
 -webkit-appearance: none;
 user-select: none;
 outline: none;
 }
 .mui-switch:before {
 content: '';
 width: 29px;
 height: 29px;
 position: absolute;
 top: 0px;
 left: 0;
 border-radius: 20px;
 border-top-left-radius: 20px;
 border-top-right-radius: 20px;
 border-bottom-left-radius: 20px;
 border-bottom-right-radius: 20px;
 background-color: #fff;
 box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
 }
 .mui-switch:checked {
 border-color: #64bd63;
 box-shadow: #64bd63 0 0 0 16px inset;
 background-color: #64bd63;
 }
 .mui-switch:checked:before {
 left: 21px;
 }
 .mui-switch.mui-switch-animbg {
 transition: background-color ease 0.4s;
 }
 .mui-switch.mui-switch-animbg:before {
 transition: left 0.3s;
 }
 .mui-switch.mui-switch-animbg:checked {
 box-shadow: #dfdfdf 0 0 0 0 inset;
 background-color: #64bd63;
 transition: border-color 0.4s, background-color ease 0.4s;
 }
 .mui-switch.mui-switch-animbg:checked:before {
 transition: left 0.3s;
 }
 .mui-switch.mui-switch-anim {
 transition: border cubic-bezier(0, 0, 0, 1) 0.4s, box-shadow cubic-bezier(0, 0, 0, 1) 0.4s;
 }
 .mui-switch.mui-switch-anim:before {
 transition: left 0.3s;
 }
 .mui-switch.mui-switch-anim:checked {
 box-shadow: #64bd63 0 0 0 16px inset;
 background-color: #64bd63;
 transition: border ease 0.4s, box-shadow ease 0.4s, background-color ease 1.2s;
 }
 .mui-switch.mui-switch-anim:checked:before {
 transition: left 0.3s;
 }
 /*# sourceMappingURL=mui-switch.css.map */
</style>

如果你喜欢scss,那么代码更加简洁

@mixin borderRadius($radius:20px) {
 border-radius: $radius;
 border-top-left-radius: $radius;
 border-top-right-radius: $radius;
 border-bottom-left-radius: $radius;
 border-bottom-right-radius: $radius;
 }
 $duration: .4s;
 $checkedColor: #64bd63;
 .mui-switch {
 width: 52px;
 height: 31px;
 position: relative;
 border: 1px solid #dfdfdf;
 background-color: #fdfdfd;
 box-shadow: #dfdfdf 0 0 0 0 inset;
 @include borderRadius();
 background-clip: content-box;
 display: inline-block;
 -webkit-appearance: none;
 user-select: none;
 outline: none;
 &:before {
 content: '';
 width: 29px;
 height: 29px;
 position: absolute;
 top: 0px;
 left: 0;
 @include borderRadius();
 background-color: #fff;
 box-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
 }
 &:checked {
 border-color: $checkedColor;
 box-shadow: $checkedColor 0 0 0 16px inset;
 background-color: $checkedColor;
 &:before {
 left: 21px;
 }
 }
 &.mui-switch-animbg {
 transition: background-color ease $duration;
 &:before {
 transition: left 0.3s;
 }
 &:checked {
 box-shadow: #dfdfdf 0 0 0 0 inset;
 background-color: $checkedColor;
 transition: border-color $duration, background-color ease $duration;
 &:before {
 transition: left 0.3s;
 }
 }
 }
 &.mui-switch-anim {
 transition: border cubic-bezier(0, 0, 0, 1) $duration, box-shadow cubic-bezier(0, 0, 0, 1) $duration;
 &:before {
 transition: left 0.3s;
 }
 &:checked {
 box-shadow: $checkedColor 0 0 0 16px inset;
 background-color: $checkedColor;
 transition: border ease $duration, box-shadow ease $duration, background-color ease $duration*3;
 &:before {
 transition: left 0.3s;
 }
 }
 }
 }

链接文章

https://www.html.cn/archives/9274

https://segmentfault.com/a/1190000003711140