效果

show more 是很常被使用的效果, 因为空间总是不够的丫.

比起 scroll, show more 的体验通常会好一些, 尤其在手机, 它有更好的引导.

实现思路

 

1. 卡片需要一个 max-height 限制内容, 同时 overflow: hidden

2. 做一个 overlay show more 绝对定位到最底部.

3. overlay 内部除了 button 以外还要有一个渐变 gradient 隐约阻挡内容, 这样就起到了引导作用.

4. show more 点击后解除 max-height 限制, 这里需要 JS 来实现.

注意: 如果内容没有超过 max-height show more 不应该出现. 这个也需要 JS 实现.

搭场景

HTML

<div class="container">
<div class="card">
<p>
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Fuga nulla
doloremque totam id ipsam illum amet cum ipsum officia itaque magnam
earum suscipit vero, dicta, quibusdam maxime at incidunt, commodi
delectus quisquam. Quis, amet animi vel harum consequuntur eos nihil
quibusdam quam, nemo dignissimos minus maiores distinctio ea facilis
sapiente sed alias eveniet et totam tenetur? Qui facilis esse ad! Non
velit vitae delectus! Unde numquam iste ipsam. Enim, provident
pariatur rerum debitis libero ut reprehenderit nostrum? A voluptatem
fugiat consequuntur ratione natus maxime odio aliquam porro tempora
doloremque eius quaerat repudiandae molestias eos consectetur, esse
nemo. Impedit, praesentium assumenda.
</p>
</div>

一张卡片, 内容, 做简单的演示就好了

CSS Style

.container {
display: grid;
place-content: center;
height: 100vh;
.card {
width: 350px;
max-height: 350px;
border: 1px solid black;
p {
line-height: 1.5;
font-size: 1.5rem;
}
}
}

给点样式美化一下

效果

上样式

添加 show more HTML

添加 show more overlay HTML 到 card 里面

<div class="show-more-overlay show">
<div class="gradient"><span>Show More</span></div>
<button class="btn">Show More</button>
</div>

gradient 的高度为了和 buttom 一样, 所以也写了字撑高

限制高度

.container {
.card {
overflow: hidden;
}
}

定位 show more overlay

.card {
position: relative;
.show-more-overlay {
display: none;
// js 会负责给 .show 的 class
&.show {
display: revert;
} position: absolute;
left: 0;
bottom: 0;
width: 100%;
}
}

一开始是 display none 的, JS 会依据内容是否超过了 max-height 决定 show

button 和 gradient 样式

.card {
.show-more-overlay {
.gradient,
.btn {
padding: 1rem;
}
.gradient {
background-image: linear-gradient(
to top,
hsla(0, 0%, 95%, 1),
hsla(0, 0%, 95%, 0.01)
);
span {
visibility: hidden;
}
}
.btn {
border-width: 0; // reset CSS
width: 100%;
background-color: hsl(0, 0%, 95%);
}
}
}

gradient 的高度和颜色需要配合 button 哦

pointer-event

这里需要注意一下, button 是可以点击的, gradient 是要穿透的.

gradient 要穿透, 那么它的 parent 也必须要穿透, 要不 overlay 也会当着内容.

而 pointer-event 的规则是, 默认所以元素都是不穿透的, 但如果 parent pointer-event: none, 那么所有子孙就可穿透了. 子孙可以通过 pointer-event:auto override 回去.

所以这里的做法是让 overlay pointer-event none, 然后 button pointer-event:auto

.card {
.show-more-overlay {
pointer-events: none;
.btn {
cursor: pointer;
pointer-events: auto;
}
}
}

上 Javascript

const card = document.querySelector(".card");
if (card.scrollHeight > card.offsetHeight) {
const overlay = card.querySelector(".show-more-overlay");
overlay.classList.add("show");
const btn = overlay.querySelector(".btn");
btn.addEventListener("click", () => {
card.style.maxHeight = "initial";
overlay.classList.remove("show");
});
}

检查 height, 显示 show more

监听点击, hide show more, 解开 max-height 限制.

需要注意的是, 如果内容有图片这类的, 可能因为加载慢而计算错误哦, 可以写一个 resize observer 解决.

Final Core

HTML

<div class="container">
<div class="card">
<p>
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Fuga nulla
doloremque totam id ipsam illum amet cum ipsum officia itaque magnam
earum suscipit vero, dicta, quibusdam maxime at incidunt, commodi
delectus quisquam. Quis, amet animi vel harum consequuntur eos nihil
quibusdam quam, nemo dignissimos minus maiores distinctio ea facilis
sapiente sed alias eveniet et totam tenetur? Qui facilis esse ad! Non
velit vitae delectus! Unde numquam iste ipsam. Enim, provident
pariatur rerum debitis libero ut reprehenderit nostrum? A voluptatem
fugiat consequuntur ratione natus maxime odio aliquam porro tempora
doloremque eius quaerat repudiandae molestias eos consectetur, esse
nemo. Impedit, praesentium assumenda.
</p>
<div class="show-more-overlay">
<div class="gradient"><span>Show More</span></div>
<button class="btn">Show More</button>
</div>
</div>
</div>

CSS Style

.container {
display: grid;
place-content: center;
height: 100vh;
.card {
width: 350px;
max-height: 350px;
border: 1px solid black;
p {
line-height: 1.5;
font-size: 1.5rem;
} overflow: hidden; position: relative;
.show-more-overlay {
display: none;
// js 会负责给 .show 的 class
&.show {
display: revert;
} position: absolute;
left: 0;
bottom: 0;
width: 100%; pointer-events: none;
.gradient,
.btn {
padding: 1rem;
}
.gradient {
background-image: linear-gradient(
to top,
hsla(0, 0%, 95%, 1),
hsla(0, 0%, 95%, 0.01)
);
span {
visibility: hidden;
}
}
.btn {
border-width: 0; // reset CSS
width: 100%;
background-color: hsl(0, 0%, 95%);
cursor: pointer;
pointer-events: auto;
}
}
}
}

JavaScript

const card = document.querySelector(".card");
if (card.scrollHeight > card.offsetHeight) {
const overlay = card.querySelector(".show-more-overlay");
overlay.classList.add("show");
const btn = overlay.querySelector(".btn");
btn.addEventListener("click", () => {
card.style.maxHeight = "initial";
overlay.classList.remove("show");
});
}

效果

CSS & JS Effect – Show More的更多相关文章

  1. CSS & JS 制作滚动幻灯片

    ==================纯CSS方式==================== <!DOCTYPE html> <html> <head> <met ...

  2. 【转】Maven Jetty 插件的问题(css/js等目录死锁)的解决

    Maven Jetty 插件的问题(css/js等目录死锁,不能自动刷新)的解决:   1. 打开下面的目录:C:\Users\用户名\.m2\repository\org\eclipse\jetty ...

  3. Css Js Loader For Zencart

    Css Js Loader 描述:这个插件很早就出来了,可能知道人非常少 这个插件的功能是整合所有的网站的CSS和JS内容到一个文件里边. 因为CSS和JS文件到了一个文件,加快了程序的运行 在配合其 ...

  4. 购物车数字加减按钮HTML+CSS+JS(有需要嫌麻烦的小伙伴拿走不谢)

    之前在写详情页的时候,如下图 因为自己嫌麻烦,就去看其他网站是怎么写的,想直接拿来用,后来看来看去觉得写得很麻烦,于是最后还是决定自己写,附上HTML+CSS+JS代码,一条龙一站式贴心服务2333 ...

  5. vs合并压缩css,js插件——Bundler & Minifier

    之前做了一个大转盘的抽奖活动,因为比较火,部分用户反馈看不到页面的情况,我怀疑js加载请求过慢导致,所以今天针对之前的一个页面进行调试优化. 首先想到的是对页面的js和css进行压缩优化,百度了下vs ...

  6. nginx资源定向 css js路径问题

    今天玩玩项目,学学nginx发现还不错,速度还可以,但是CSS JS确无法使用,原来Iginx配置时需要对不同类型的文件配置规则,真是很郁闷,不过想想也还是很有道理.闲暇之际,把配置贴上来.#user ...

  7. IIS7的集成模式下如何让自定义的HttpModule不处理静态文件(.html .css .js .jpeg等)请求

    今天将开发好的ASP.NET站点部署到客户的服务器上后,发现了一个非常头疼的问题,那么就是IIS7的应用程序池是集成模式的话,ASP.NET项目中自定义的HttpModule会处理静态文件(.html ...

  8. 网站加载css/js/img等静态文件失败

    网站加载css/js/img等静态文件失败,报网站http服务器内部500错误.而服务器中静态文件存在且权限正常. 从浏览器中直接访问文件,出来乱码.这种问题原因在于iis中该网站mime配置报错,不 ...

  9. 【前端】Sublime text3 插件HTML/CSS/JS prettify 格式化代码

    1.首先安装插件 菜单的preference->packages control,然后输入install .. 回车,再输入HTML/CSS/JS prettify 再回车,重启后就可以了. 2 ...

  10. CSS+JS实现兼容性很好的无限级下拉菜单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DT ...

随机推荐

  1. oeasy教您玩转vim - 78 - # 操作系统文件格式 fileformat

    ​ 文件系统换行格式 fileformat 回忆保留环境的细节 上次我们了解了viminfo 他能够保存 命令行历史 标记 寄存器 把他和 :mksession 一起使用就可以完美复原环境了 还有什么 ...

  2. WPF使用AppBar实现窗口停靠,适配缩放、全屏响应和多窗口并列(附封装好即开即用的附加属性)

    在吕毅大佬的文章中已经详细介绍了什么是AppBar: WPF 使用 AppBar 将窗口停靠在桌面上,让其他程序不占用此窗口的空间(附我封装的附加属性) - walterlv 即让窗口固定在屏幕某一边 ...

  3. JMeter Sampler-http请求之KeepAlive使用总结

    Sampler-http请求之KeepAlive使用总结 测试环境 apache-jmeter-2.13 KeepAlive使用介绍 说明: 1.Use KeepAlive 勾上,则表示为求连接设置请 ...

  4. MySQL 递归查询实践总结

    MySQL复杂查询使用实例 By:授客 QQ:1033553122   表结构设计 SELECT id, `name`, parent_id FROM `tb_testcase_suite` 说明: ...

  5. 兼容sentry协议的轻量级监控,glitchtip

    前言 上一篇文章说了重启 sentry 的事 因为过程太折腾了,一度想过放弃 sentry 换成其他比较轻量级的开源监控系统 这不就给我找到了另外俩个 https://glitchtip.com/ h ...

  6. 1分钟了解HashSet的使用

    前言:刷leetcode的时候体验到hashset有多厉害了,用了他剪枝之后直接不爆超时了.速度大大滴快 使用方法 1.创建set对象Set<Integer>set=new HashSet ...

  7. Mysql函数12-DATE_FORMAT

    DATE_FORMAT函数用于日期格式的转换. 1.sql查询出一列create_time select create_time from goods where id=65 2.让create_ti ...

  8. 【Vue】14 UI库

    PC端: 第一梯队:基于JQuery实现的Dom操作,和一些简单CSS样式组成 Layui Bootstrap EasyUI 第二梯队:基于Vue2.0开发的UI库,组件化开发 ElementUI A ...

  9. 【Uni-APP】02 FLEX 弹性布局

    新建一个项目: 注释所有内容: <template> <!-- <view class="content"> <image class=&quo ...

  10. 【转载】 Do's and Don'ts of using t-SNE to Understand Vision Models —— t-SNE 作者写的使用指南(PPT版本)

    <Do's and Don'ts of using t-SNE to Understand Vision Models> 作者的PPT视频:https://www.youtube.com/ ...