位置: IT常识 - 正文

CSS: overflow-anchor 固定滚动到底部,随着页面内容增多滚动条自己滚动展示最新的内容

编辑:rootadmin
CSS: overflow-anchor 固定滚动到底部,随着页面内容增多滚动条自己滚动展示最新的内容

推荐整理分享CSS: overflow-anchor 固定滚动到底部,随着页面内容增多滚动条自己滚动展示最新的内容,希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:,内容如对您有帮助,希望把文章链接给更多的朋友!

overflow-anchorCSS中的属性相对较新,2017 年首次推出 Chrome ¹,2019 年推出 Firefox,现在 Edge在 2020 年推出 Chrome 过渡。幸运的是,它的使用在很大程度上是一种增强。这个想法是浏览器在默认情况下确实尝试不允许位置移动。然后,如果您不喜欢它的处理方式,您可以使用 将其关闭。所以一般来说,你从不碰它。overflow-anchor 但正如你可能怀疑的那样,我们可以利用这个小美来做一点 CSS 小把戏。即使我们添加新内容,我们也可以强制滚动元素保持固定在底部。

您需要检测何时使用 JavaScript 添加新内容并将滚动元素强制到底部。这是一种MutationObserver在 JavaScript 中使用的技术来监视新内容并强制滚动:

const scrollingElement = document.getElementById("scroller");const config = { childList: true };const callback = function (mutationsList, observer) { for (let mutation of mutationsList) { if (mutation.type === "childList") { window.scrollTo(0, document.body.scrollHeight); } }};const observer = new MutationObserver(callback);observer.observe(scrollingElement, config);

但我发现一个纯 CSS 的解决方案更有吸引力!上面的版本有一些我们稍后会介绍的用户体验缺陷。

这里的诀窍是浏览器默认已经做了滚动锚定。但是浏览器试图做的不是改变你的页面。因此,当添加可能会改变页面视觉位置的新内容时,他们会尝试防止这种情况发生。在这种不寻常的情况下,我们有点想要相反的东西。我们希望页面卡在页面底部并让视觉页面在视觉上移动,因为它被迫保持卡在底部。

CSS: overflow-anchor 固定滚动到底部,随着页面内容增多滚动条自己滚动展示最新的内容

下面是这个技巧的工作原理。首先是滚动父元素中的一些 HTML:

<div id="scroller"> <!-- new content dynamically inserted here --> <div id="anchor"></div></div>

所有元素自然都有一个overflow-anchor: auto;,这意味着当它们在屏幕上时,它们会试图阻止页面移动,但我们可以使用overflow-anchor: none;. 所以诀窍是针对所有动态插入的内容并将其关闭:

#scroller * { overflow-anchor: none;}

然后仅强制该锚元素具有滚动锚定,仅此而已:

#anchor { overflow-anchor: auto; height: 1px;}

现在,一旦该锚在页面上可见,浏览器将被迫将该滚动位置固定到它,并且由于它是该滚动元素中的最后一件事,因此保持固定在底部。

<div id="scroller"> <!-- new content dynamically inserted here --> <div id="anchor"></div></div><style>#scroller * { overflow-anchor: none;}#anchor { overflow-anchor: auto; height: 1px;}html { font-family: system-ui, sans-serif;}body { background-color: #7FDBFF; /* In case you need to kick off effect immediately, this plus JS */ /* height: 100.001vh; */}.message { padding: 0.5em; border-radius: 1em; margin: 0.5em; line-height: 1.1em; background-color: white;}</style><script>let scroller = document.querySelector('#scroller');let anchor = document.querySelector('#anchor');// https://ajaydsouza.com/42-phrases-a-lexophile-would-love/let messages = [ 'I wondered why the baseball was getting bigger. Then it hit me.', 'Police were called to a day care, where a three-year-old was resisting a rest.', 'Did you hear about the guy whose whole left side was cut off? He’s all right now.', 'The roundest knight at King Arthur’s round table was Sir Cumference.', 'To write with a broken pencil is pointless.', 'When fish are in schools they sometimes take debate.', 'The short fortune teller who escaped from prison was a small medium at large.', 'A thief who stole a calendar… got twelve months.', 'A thief fell and broke his leg in wet cement. He became a hardened criminal.', 'Thieves who steal corn from a garden could be charged with stalking.', 'When the smog lifts in Los Angeles , U. C. L. A.', 'The math professor went crazy with the blackboard. He did a number on it.', 'The professor discovered that his theory of earthquakes was on shaky ground.', 'The dead batteries were given out free of charge.', 'If you take a laptop computer for a run you could jog your memory.', 'A dentist and a manicurist fought tooth and nail.', 'A bicycle can’t stand alone; it is two tired.', 'A will is a dead giveaway.', 'Time flies like an arrow; fruit flies like a banana.', 'A backward poet writes inverse.', 'In a democracy it’s your vote that counts; in feudalism, it’s your Count that votes.', 'A chicken crossing the road: poultry in motion.', 'If you don’t pay your exorcist you can get repossessed.', 'With her marriage she got a new name and a dress.', 'Show me a piano falling down a mine shaft and I’ll show you A-flat miner.', 'When a clock is hungry it goes back four seconds.', 'The guy who fell onto an upholstery machine was fully recovered.', 'A grenade fell onto a kitchen floor in France and resulted in Linoleum Blownapart.', 'You are stuck with your debt if you can’t budge it.', 'Local Area Network in Australia : The LAN down under.', 'He broke into song because he couldn’t find the key.', 'A calendar’s days are numbered.',];function randomMessage() { return messages[(Math.random() * messages.length) | 0];}function appendChild() { let msg = document.createElement('div'); msg.className = 'message'; msg.innerText = randomMessage(); scroller.insertBefore(msg, anchor);}setInterval(appendChild, 1000);// To kick off effect immediately, this plus CSS// document.scrollingElement.scroll(0, 1);</script>

这里有两个小警告......

请注意,锚必须有一定的大小。我们在height这里使用以确保它不是没有大小的折叠/空元素,这会阻止它工作。为了使滚动锚定起作用,页面必须至少滚动一次才能开始。

第二个更难。一种选择是根本不处理它,您可以等待用户滚动到底部,效果从那里开始起作用。实际上这很好,因为如果他们从底部滚动,效果就会停止工作,这就是你想要的。在上面的 JavaScript 版本中,请注意即使您尝试向上滚动,它也会迫使您进入底部,这就是 Ryan 的意思,这并不是一件容易正确的事情。

如果您需要立即启动效果,诀窍是让滚动元素立即可滚动,例如:

body { height: 100.001vh;}

然后立即触发一个非常轻微的滚动:

document.scrollingElement.scroll(0, 1);

这应该可以解决问题。这些行可在上面的演示中取消注释并尝试。

本文链接地址:https://www.jiuchutong.com/zhishi/293130.html 转载请保留说明!

上一篇:GPT模型总结【模型结构及计算过程_详细说明】(gpt详解)

下一篇:swiper获取当前数组滑动的inex(vue)(swiper.js常用功能)

  • 查账征收纳税申报表
  • 其它综合收益影响因素
  • 融资租赁如何确认收入
  • 可转换债券含义
  • 建筑公司成本发票不够
  • 房屋租赁收入怎么做账
  • 个人住房契税2021年收费标准
  • 办理产权证费用明细
  • 文体活动费用计入什么科目
  • 滴滴客运服务费发票税率
  • 净利润亏损怎么结转
  • 开票码是什么意思
  • 在建工程领用外购材料会计分录
  • 工会工费缴纳标准
  • 往年的年报填错了怎么办
  • 收入与成本不匹配建议怎么写
  • 企业支付工商年费怎么查
  • 少数股东持股比例
  • 实收资本或股本账户属于企业的什么账
  • 修改hosts文件的软件
  • 圣米歇尔山 (© Leroy Francis/Getty Images)
  • mac系统安装出错
  • 股权处置的形式
  • 计提税金及附加的金额如何算
  • 公司的房租收入要交税吗
  • php xml转字符串
  • 用php做计算
  • 西安微信公众号开发
  • 公司买发票的费用怎么做账?
  • php使用什么开发工具
  • 承兑汇票无法兑付
  • Win11 Build 25336 预览版发布:Snap 窗口新增最近 20 个标签选项
  • 大数据找工作好找吗
  • php常用array函数
  • 如何在sql server中建立一个表
  • SQLSERVER 2005的ROW_NUMBER、RANK、DENSE_RANK的用法
  • 不含税单价和不含税合价的区别
  • 营改增后运输企业税收政策
  • 社保的账务处理分录
  • 息税前利润怎么理解
  • 公司向法人借款有税务风险吗
  • 不抵扣进项税额转出怎么做分录处理
  • 收到政府的资本公积可以投入子公司吗
  • 税局代开的专票能作废或红冲吗?
  • 房地产企业怎么交房产税
  • 存货的进口关税计入成本吗
  • 小企业汇算清缴补税会计分录
  • 生产车间包括什么
  • 收到赞助费如何开发票
  • 生产成本福利费如何分摊
  • 丢失了发票怎么处理
  • 来料加工费用价格表
  • 哪些福利费没有附加税
  • 以前年度多计提的社保费怎么冲回
  • mysql的外键
  • 没有光驱启动
  • mac怎么旋转窗口
  • windows8怎么进入bios
  • linux 系统文件
  • 重装win7旗舰版重启后黑屏
  • Win10 Mobile 10586.242累积更新上手视频评测
  • 跑酷游戏cs
  • 教你在heroku云平台上部署Node.js应用
  • linux shell $1
  • Interlnk、Intersvr、Qbasic命令的使用方法
  • unity游戏开发入门经典
  • 什么叫懒加载
  • node.js原生支持的编码格式
  • python基本入门
  • Python实现定时任务
  • nodejs 加解密
  • 安卓listview控件map的用法前面每行加图片
  • jquery动态生成页面
  • javascript中array数组对象的含义及常用方法
  • javascript Slip.js实现整屏滑动的手机网页
  • android 动画特效
  • 税务局稽查科有什么处理企业的办法
  • 销售农药化肥的经营范围
  • 印花税核算有两种情况,是如何处理的?
  • 宾馆如何申请税务发票
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设