我们已经习惯用 JavaScript 实现常见的 UI 功能组件,如手风琴、工具提示、文本截断等。但是随着 HTML 和 CSS 新特性的推出,不用再支持旧浏览器,我们可以越来越少用 JavaScript 来创建 UI 组件,更多地集中在代码的逻辑部分(验证、数据处理等)。

有些实现方案确实感觉有点剑走偏锋,也不太灵活,但它们对于小型项目里的单例组件还是有用的。为什么非得在网站里用 JavaScript(或者怀旧点用 jQuery)实现一个只用一次的手风琴组件?这就是我在给自己的移动端个人网站页脚添加手风琴组件时的思考过程。

以下就是一些可以零 JavaScript 实现的元素示例。

响应式文本截断

CSS 文本截断很容易实现,性能也不错,因为我们无须编辑文本的 HTML 内容,只是渲染结果。单行文本截断在旧浏览器上支持良好,而多行文本截断只在较新的浏览器上获得支持。

HTML:

<section class="flex">
<h2>用 Flexbox </h2>
<article class="wrapper">
<p class="text--truncated">
永和九年,岁在癸丑,暮春之初,会于会稽山阴之兰亭,修禊事也。群贤毕至,少长咸集。此地有崇山峻岭,茂林修竹;又有清流激湍,映带左右,引以为流觞曲水,列坐其次。虽无丝竹管弦之盛,一觞一咏,亦足以畅叙幽情。
</p>
</article>
</section> <section class="table">
<h2>用 Table </h2>
<article class="wrapper">
<p class="text--truncated">
永和九年,岁在癸丑,暮春之初,会于会稽山阴之兰亭,修禊事也。群贤毕至,少长咸集。此地有崇山峻岭,茂林修竹;又有清流激湍,映带左右,引以为流觞曲水,列坐其次。虽无丝竹管弦之盛,一觞一咏,亦足以畅叙幽情。
</p>
</article>
</section> <section class="multiline">
<h2>多行文本</h2>
<div class="wrapper">
<p class="text--truncated">
永和九年,岁在癸丑,暮春之初,会于会稽山阴之兰亭,修禊事也。群贤毕至,少长咸集。此地有崇山峻岭,茂林修竹;又有清流激湍,映带左右,引以为流觞曲水,列坐其次。虽无丝竹管弦之盛,一觞一咏,亦足以畅叙幽情。
</p>
</div>
</section> 复制代码

CSS:

/* WITH FLEXBOX */
.flex .wrapper {
display:flex;
} .flex .text--truncated {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
} /* WITH TABLE */
.table .wrapper {
display: table;
table-layout: fixed;
width: 100%;
} .table .text--truncated {
display: table-cell;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
} /* MULTI-LINE */
.multiline .text--truncated {
overflow: hidden;
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
}
复制代码

星级评分

星级评分组件是大部分调查表单的必备元素。有多种使用 CSS 来实现的方法:用背景图、JavaScript、图标等等。最方便的方法是使用图标和原生单选框。

这种实现方式的缺点是,HTML 单选按钮是倒序的(分数值从5到1),因为我们要选择被勾选的单选按钮以及它后面的所有单选按钮,不倒序的话 CSS 没法实现。~ 选择器只能选择元素后面的兄弟元素,所以只能倒过来。

这种实现非常灵活,很容易自定义。

HTML:

<div class="rating">
<input class="rating__input hidden--visually" type="radio" id="5-star" name="rating" value="5" required />
<label class="rating__label" for="5-star" title="5 out of 5 rating"><span class="rating__icon" aria-hidden="true"></span><span class="hidden--visually">5 out of 5 rating</span></label>
<input class="rating__input hidden--visually" type="radio" id="4-star" name="rating" value="4" />
<label class="rating__label" for="4-star" title="4 out of 5 rating"><span class="rating__icon" aria-hidden="true"></span><span class="hidden--visually">4 out of 5 rating</span></label>
<input class="rating__input hidden--visually" type="radio" id="3-star" name="rating" value="3" />
<label class="rating__label" for="3-star" title="3 out of 5 rating"><span class="rating__icon" aria-hidden="true"></span><span class="hidden--visually">3 out of 5 rating</span></label>
<input class="rating__input hidden--visually" type="radio" id="2-star" name="rating" value="2" />
<label class="rating__label" for="2-star" title="2 out of 5 rating"><span class="rating__icon" aria-hidden="true"></span><span class="hidden--visually">2 out of 5 rating</span></label>
<input class="rating__input hidden--visually" type="radio" id="1-star" name="rating" value="1" />
<label class="rating__label" for="1-star" title="1 out of 5 rating"><span class="rating__icon" aria-hidden="true"></span><span class="hidden--visually">1 out of 5 rating</span></label>
</div>
复制代码

CSS:

/* --- Required CSS (不可自定义) --- */

.rating {
display: inline-flex;
flex-direction: row-reverse;
} /* Hiding elements in an accessible way */
.hidden--visually {
border: 0;
clip: rect(1px 1px 1px 1px);
clip: rect(1px, 1px, 1px, 1px);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
} /* --- Required CSS (可自定义) --- */ .rating__label {
cursor: pointer;
color: gray;
padding-left: 0.25rem;
padding-right: 0.25rem;
} .rating__icon::before {
content: "";
} .rating__input:hover ~ .rating__label {
color: lightgray;
} .rating__input:checked ~ .rating__label {
color: goldenrod;
} 复制代码

Tooltip / 下拉菜单

这是个非常灵活的元素,它的 CSS 逻辑可以同时用于 tooltips 和下拉菜单,因为两者的工作方式类似,都支持鼠标悬停和点击(或者触碰)。

这种实现方式存在的问题是,由于它的focus 样式,点击后tooltip(或者下拉菜单)会一直处于打开状态,直到用户在元素外部点击。

HTML:

<div>This is an example of a <a href="#" class="tooltip" aria-label="The tooltip or a hint is a common GUI element that describes the item it's related to.">Tooltip</a>. Click on it to learn more.</div>
复制代码

CSS:

/* --- Required CSS (not customizable) --- */

.tooltip:focus::after,
.tooltip:hover::after {
content: attr(aria-label);
display: block;
} /* --- Required CSS (customizable) --- */ .tooltip:focus::after,
.tooltip:hover::after {
position: absolute;
top: 100%;
font-size: 1.2rem;
background-color: #f2f2f2;
border-radius: 0.5rem;
color: initial;
padding: 1rem;
width: 13rem;
margin-top: 0.5rem;
text-align: left;
} .tooltip {
position: relative;
color: goldenrod;
display: inline-block;
} .tooltip:hover::before {
top: 100%;
right: 0;
left: 0;
margin: -1rem auto 0;
display: block;
border: solid transparent;
content: "";
height: 0;
width: 0;
position: absolute;
pointer-events: none;
border-bottom-color: #f2f2f2;
border-width: 1rem;
} 复制代码

模态对话框

这个实现就有点 hacky 了,它完全依赖于URL里的查询字符串。URL 里的 Id 必须跟要打开的模态对话框元素匹配。

源码:codepen.io/AdrianBece/…

输入框标签文字浮动

源码:codepen.io/AdrianBece/…

手风琴效果

最近,HTML 可以通过 <details> 和 <summary> 元素实现原生的手风琴效果了,但它的缺点是没有太多的样式选择,因此开发人员还是要自己去实现。所幸,通过利用复选框和单选框逻辑,我们可以不依赖 JavaScript 实现手风琴组件了。

这种实现方案的缺陷是,它依赖于input 元素,并且它的逻辑需要额外的 HTML 代码,但另一方面它的可访问性较好。

源码:codepen.io/AdrianBece/…

总结

如你所见,纯 CSS 实现方案依赖于 CSS 选择器,比如 :focus 和 :placeholder-shown ,来替代 JavaScript 逻辑代码。其中有些 CSS 方案被认为是比较 hacky 的,但是性能好,比较灵活,不依赖 JavaScript。

我在项目里使用了部一些 CSS 实现方案,避免了完全利用 JavaScript 实现视觉效果。

当然了,还有很多纯 CSS 实现方案,我只是列举了觉得比较有意思的几个。如果你还有其他方案,欢迎在评论里留言分享!

.

CSS实现常用组件特效(不依赖JS)的更多相关文章

  1. amazeui学习笔记--css(常用组件12)--面板Panel

    amazeui学习笔记--css(常用组件12)--面板Panel 一.总结 1.面板基本样式:默认的 .am-panel 提供基本的阴影和边距,默认边框添加 .am-panel-default,内容 ...

  2. amazeui学习笔记--css(常用组件9)--导航nav

    amazeui学习笔记--css(常用组件9)--导航nav 一.总结 1.导航基本使用:<ul> 添加 .am-nav class 以后就是一个基本的垂直导航.默认样式中并没有限定导航的 ...

  3. amazeui学习笔记--css(常用组件7)--输入框组Input-group

    amazeui学习笔记--css(常用组件7)--输入框组Input-group 一.总结 1.使用:Input group 基于 Form 组件和 Button 组件扩展,依赖这两个组件.在容器上添 ...

  4. amazeui学习笔记--css(常用组件16)--文章页Article

    amazeui学习笔记--css(常用组件16)--文章页Article 一.总结 1.基本使用:文章内容页的排版样式,包括标题.文章元信息.分隔线等样式. .am-article 文章内容容器 .a ...

  5. amazeui学习笔记--css(常用组件15)--CSS动画Animation

    amazeui学习笔记--css(常用组件15)--CSS动画Animation 一.总结 1.css3动画封装:CSS3 动画封装,浏览器需支持 CSS3 动画. Class 描述 .am-anim ...

  6. amazeui学习笔记--css(常用组件14)--缩略图Thumbnail

    amazeui学习笔记--css(常用组件14)--缩略图Thumbnail 一.总结 1.基本样式:在 <img> 添加 .am-thumbnail 类:也可以在 <img> ...

  7. amazeui学习笔记--css(常用组件13)--进度条Progress

    amazeui学习笔记--css(常用组件13)--进度条Progress 一.总结 1.进度条基本使用:进度条组件,.am-progress 为容器,.am-progress-bar 为进度显示信息 ...

  8. amazeui学习笔记--css(常用组件11)--分页Pagination

    amazeui学习笔记--css(常用组件11)--分页Pagination 一.总结 1.分页使用:还是ul包li的形式: 分页组件,<ul> / <ol> 添加 .am-p ...

  9. amazeui学习笔记--css(常用组件10)--导航条Topbar

    amazeui学习笔记--css(常用组件10)--导航条Topbar 一.总结 1. 导航条:就是页面最顶端的导航条:在容器上添加 .am-topbar class,然后按照示例组织所需内容.< ...

随机推荐

  1. nowcoder911J 异或的路径

    题目链接 题意 给出一棵树,每条边有边权.求\(\sum\limits_{i=1}^n{f(i,j)}\),\(f(i,j)\)表示从i到j路径的异或和. 思路 \(g_i\)表示从根到\(i\)的异 ...

  2. zookeeper图形化的客户端工具(ZooInspector)

    1.ZooInspector下载地址 https://issues.apache.org/jira/secure/attachment/12436620/ZooInspector.zip 2.解压压缩 ...

  3. [LeetCode] 350. Intersection of Two Arrays II 两个数组相交之二

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  4. Spring容器与SpringMVC容器的区别与联系

    在spring整体框架的核心概念中,容器的核心思想是管理Bean的整个生命周期.但在一个项目中,Spring容器往往不止一个,最常见的场景就是在一个项目中引入Spring和SpringMVC这两个框架 ...

  5. 软件推荐【Windows】

    随时更新...链接为官网,自用保证安全(不信任可以把鼠标放在超链接上预览一下)        如有备注,下载链接均为最新(因为都是官方自动更新的下载链) 首先: 推荐一个软件管家(毕竟有时外网不稳定, ...

  6. 第02组 Alpha事后诸葛亮

    目录 1. 组长博客(2分) 2. 总结思考(27分) 2.1. 设想和目标(2分) 2.2. 计划(5分) 2.3. 资源(3分) 2.4. 变更管理(4分) 2.5. 设计/实现(4分) 2.6. ...

  7. .NetCore 使用k8s部署服务的过程中需要注意的地方以及遇到的问题

    这里开始我准备了3台测试服务器,这里我使用了JumpServer管理起来了,这里我们来看下: Master :192.168.0.236 Node1:192.168.0.237 Node2:192.1 ...

  8. 一次线上Redis类转换异常排查引发的思考

    之前同事反馈说线上遇到Redis反序列化异常问题,异常如下: XxxClass1 cannot be cast to XxxClass2 已知信息如下: 该异常不是必现的,偶尔才会出现: 出现该异常后 ...

  9. Hibernate 连接MySQL/SQLServer/Oracle数据库的hibernate.cfg.xml文件

    用Hibernate配置连接数据库可以方便我们对POJO的操作,节省了很多时间和代码.下面就分别说明连接不同数据库需要在hibernate.cfg.xml做的配置. 需要数据库驱动包可以点击这里下载: ...

  10. rust下根据protobuf的消息名创建对象实例

    在C++里面, 我们可以根据一个消息的名称, 动态的创建一个实例 google::protobuf::Descriptor* desc = google::protobuf::DescriptorPo ...