当前位置:网站首页>详细介绍scrollIntoView()方法属性
详细介绍scrollIntoView()方法属性
2022-07-02 14:42:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
因为工作中用到了锚点设置,常用的总是出问题,后来扒拉出了这个属性,详细研究了下方便日后使用
介绍scrollIntoView()的详细属性
简介
该scrollIntoView()
方法将调用它的元素滚动到浏览器窗口的可见区域。
PS:根据其他元素的布局,元素可能无法完全滚动到顶部或底部。
TIPS:页面(容器)可滚动时才有用!
语法
element.scrollIntoView(); // 等同于element.scrollIntoView(true)
element.scrollIntoView(alignToTop); //布尔参数
element.scrollIntoView(scrollIntoViewOptions); //对象参数
语法参数
alignToTop | [可选],目前之前这个参数得到了良好的支持 |
---|---|
true | 元素的顶部将对齐到可滚动祖先的可见区域的顶部。对应于scrollIntoViewOptions: {block: "start", inline: "nearest"}。这是默认值 |
false | 元素的底部将与可滚动祖先的可见区域的底部对齐。对应于scrollIntoViewOptions: {block: "end", inline: "nearest"}。 |
scrollIntoViewOptions | [可选],目前这个参数浏览器对它的支持并不好,可以查看下文兼容性详情 |
---|---|
behavior | [可选]定义过渡动画。"auto","instant"或"smooth"。默认为"auto"。 |
block | [可选] "start","center","end"或"nearest"。默认为"center"。 |
inline | [可选] "start","center","end"或"nearest"。默认为"nearest"。 |
示例
var element = document.getElementById("box");
element.scrollIntoView();
element.scrollIntoView(false);
element.scrollIntoView({block: "end"});
element.scrollIntoView({behavior: "instant", block: "end", inline: "nearest"});
应用场景
URL中hash标记的进化
- 聊天窗口滚动显示最新的消息
- 往一个列表添加item后滚动显示最新的添加的item
- 回到顶部(#)
- 滚动到指定位置(#xxx)
浏览器兼容性
特征 | Chrome | Firefox | Safari | Edge | IE | Opera |
---|---|---|---|---|---|---|
基本支持( alignToTop ) | yes | yes | yes | yes | yes | yes |
scrollIntoViewOptions | yes | yes | 5.1[1] | 12[1] | 9[1] | 48[2] |
注意:不支持"smooth"
行为或"center"
选项。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/148024.html原文链接:https://javaforall.cn
边栏推荐
猜你喜欢
【Leetcode】13. Roman numeral to integer
TCP congestion control details | 2 background
ETH数据集下载及相关问题
Error when uploading code to remote warehouse: remote origin already exists
[cloud native] briefly talk about the understanding of flume, a massive data collection component
Green bamboo biological sprint Hong Kong stocks: loss of more than 500million during the year, tiger medicine and Beijing Yizhuang are shareholders
[essay solicitation activity] Dear developer, RT thread community calls you to contribute
Baobab's gem IPO was terminated: Tang Guangyu once planned to raise 1.8 billion to control 47% of the equity
【Leetcode】14. Longest Common Prefix
剑指 Offer 25. 合并两个排序的链表
随机推荐
Configure ARP table entry restrictions and port security based on the interface (restrict users' private access to fool switches or illegal host access)
C语言中sprintf()函数的用法
2022 interview questions
[leetcode] 14. Préfixe public le plus long
PhD battle-11 preview | review and prospect backdoor attack and defense of neural network
ThreadLocal
福元医药上交所上市:市值105亿 胡柏藩身价超40亿
移动应用性能工具探索之路
绿竹生物冲刺港股:年期内亏损超5亿 泰格医药与北京亦庄是股东
相信自己,这次一把搞定JVM面试
剑指 Offer 22. 链表中倒数第k个节点
关于举办科技期刊青年编辑沙龙——新时代青年编辑应具备的能力及提升策略的通知...
Xiaopeng P7 had an accident on rainy days, and the airbag did not pop up. Official response: the impact strength did not meet the ejection requirements
traceroute命令讲解
远程办公对我们的各方面影响心得 | 社区征文
QWebEngineView崩溃及替代方案
剑指 Offer 25. 合并两个排序的链表
Listing of chaozhuo Aviation Technology Co., Ltd.: raising 900million yuan, with a market value of more than 6billion yuan, becoming the first science and technology innovation board enterprise in Xia
深度学习图像数据自动标注[通俗易懂]
深度之眼(二)——矩阵及其基本运算