Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scrollIntoViewIfNeeded 与 scrollIntoView #3

Open
justjavac opened this issue Jul 4, 2017 · 5 comments
Open

scrollIntoViewIfNeeded 与 scrollIntoView #3

justjavac opened this issue Jul 4, 2017 · 5 comments
Labels

Comments

@justjavac
Copy link
Owner

justjavac commented Jul 4, 2017

MDN 文档:

Element.scrollIntoView() 方法让当前的元素滚动到浏览器窗口的可视区域内。而 Element.scrollIntoViewIfNeeded()Element.scrollIntoView() 的变体,如果该元素已经在浏览器窗口的可见区域内,则不会发生滚动

  • 当元素已经在可视区域时,调用 Element.scrollIntoView(),无论设置什么参数,均发生滚动。
  • 当元素已经在可视区域时,调用 Element.scrollIntoViewIfNeeded(),无论设置什么参数,均发生滚动。
@justjavac justjavac mentioned this issue Jul 4, 2017
@justjavac justjavac mentioned this issue Jul 6, 2017
@Tao-Quixote
Copy link

Tao-Quixote commented Jul 7, 2017

根据MDN文档,
Element.scrollIntoView是一个实验性质的方法,提示截图如下:

image

Element.scrollIntoViewIfNeeded是一个非标准的特性,建议不要在生产环境中使用,提示截图如下:

image

其实这两个特性,我个人感觉应用最多的场景应该是在移动端,当表单元素获得焦点的时候滚动到视野中,避免软键盘遮挡元素。鉴于Element.scrollIntoView无论设置什么参数,且不论是否在视野中都会滚动的特点,Element.scrollIntoViewIfNeeded特性应该比Element.scrollIntoView体验好。

虽然MDN文档中说Element.scrollIntoViewIfNeeded是非标准特性,但是在移动端的支持还是非常好的,以下截图来自caniuse:

image

但是反观Element.scrollIntoView这个实验特性,在移动端的支持并不是特别好,以下截图来自caniuse:

image

根据上面的比较应该可以得出结论:在移动避免软键盘遮挡表单元素时,应该使用Element.scrollIntoViewIfNeeded特性来实现元素滚动到视野中,不论是体验还是浏览器支持,该特性都好于Element.scrollIntoView特性。

以上。

如有错误,敬请指正。

@justjavac
Copy link
Owner Author

@Tao-Quixote 是的,这个主要是用在移动端,而且推荐使用 scrollIntoViewIfNeeded

@Yangfan2016
Copy link

inline 参数是做什么用的,各个参数值试了一遍,没发现有什么区别
image

@troy351
Copy link

troy351 commented Mar 6, 2019

@Yangfan2016
image
作用于横向滚动条的

@Yangfan2016
Copy link

@Yangfan2016 image 作用于横向滚动条的

soga thx ~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants