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

scrollInToView方法 #8

Open
Liqihan opened this issue Oct 12, 2017 · 0 comments
Open

scrollInToView方法 #8

Liqihan opened this issue Oct 12, 2017 · 0 comments

Comments

@Liqihan
Copy link
Owner

Liqihan commented Oct 12, 2017

移动端input获取焦点的时候,弹出的软键盘可能会挡住input输入框,相当影响体验,查了一下用scrollInToView能解决这个问题,让输入框不被挡住,使用方法如下

element.scrollIntoView(alignToTop); // Boolean parameter

scrollIntoView()可以在所有的HTML元素上调用,通过滚动浏览器窗口或某个容器元素,调用元素就可以出现在视窗中。如果给该方法传入true作为参数,或者不传入任何参数,那么窗口滚动之后会让调动元素顶部和视窗顶部尽可能齐平。如果传入false作为参数,调用元素会尽可能全部出现在视口中(可能的话,调用元素的底部会与视口的顶部齐平。)不过顶部不一定齐平

Demo

<html>  
    <head>  
        <title>HTML5_ScrollInToView方法</title>  
        <meta  charset="utf-8">  
        <script type="text/javascript">  
            window.onload = function(){  
                document.querySelector("#roll1").onclick = function(){  
                    document.querySelector("#roll_top").scrollIntoView(false);  
                }  
                document.querySelector("#roll2").onclick = function(){  
                    document.querySelector("#roll_top").scrollIntoView(true);  
                }  
            }  
        </script>   
        <style type="text/css">  
            #myDiv{  
                height:900px;  
                background-color:gray;  
                  
            }  
            #roll_top{  
                height:900px;  
                background-color:green;  
                color:#FFF;  
                font-size:50px;  
                position:relative;  
            }  
            #bottom{  
                position:absolute;  
                display:block;  
                left:0;bottom:0;  
            }  
        </style>  
    </head>  
    <body>  
        <button id="roll1">scrollIntoView(false)</button>  
        <button id="roll2">scrollIntoView(true)</button>  
        <div id="myDiv"></div>  
        <div id="roll_top">  
            scrollIntoView(ture)元素上边框与视窗顶部齐平  
            <span id="bottom">scrollIntoView(false)元素下边框与视窗底部齐平</span>  
        </div>   
    </body>  
</html>  

支持该方法的浏览器有 IE、Firefox、Safari和Opera。

@Liqihan Liqihan added the blog label Oct 12, 2017
@Liqihan Liqihan added the JS label Jan 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant