Table of Contents
html
css
js
2. Scroll monitoring
Home Web Front-end H5 Tutorial Detailed explanation of scrollIntoView usage in h5

Detailed explanation of scrollIntoView usage in h5

Jun 25, 2017 am 10:03 AM
html5 ie element control scroll page

If the page is scrolled, it is also a problem that DOM has not solved. In order to solve this problem, browsers have implemented a method to facilitate developers to better control the scrolling of the page. Among various proprietary methods, HTML5 chose scrollIntoView() as the standard method. scrollIntoView() can be called on all HTML elements. By scrolling the browser window or a container element, the calling element can appear in the window. If true is passed as a parameter to this method, or no parameters are passed in, then the top of the adjusted element and the top of the window will be as flush as possible after the window is scrolled. If false is passed in as a parameter, the calling element will appear as fully as possible in the viewport (if possible, the bottom of the calling element will be flush with the top of the viewport.) However, the top is not necessarily flush.

1. scrollIntoView

html

<div><h2>scrollIntoView</h2><button id="roll1">scrollIntoView(false)</button><button id="roll2">scrollIntoView(true)</button><div><div id="myDiv"></div><div id="roll_top">scrollIntoView(ture)元素上边框与视窗顶部齐平<span id="bottom">scrollIntoView(false)元素下边框与视窗底部齐平</span></div></div></div>
Copy after login

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;
        }
Copy after login

js

  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);
        };
    }
Copy after login

2. Scroll monitoring

html

<div><h2>scroll</h2><div id="nav"><div class="f1">floor1</div><div class="f2">floor2</div><div class="f3">floor3</div><div class="f4">floor4</div><div class="f5">floor5</div></div><p>页面结构</p><div class="main"><div id="f1">测试1</div><div id="f2">测试2</div><div id="f3">测试3</div><div id="f4">测试4</div><div id="f5">测试5</div></div></div>
Copy after login

css

      .main div {height: 1000px;width: 300px;margin: 20px;background-color: #C0C0C0;
        }#nav {position: fixed;width: 100px;height: 200px;top: 40%;right: 10px;
        }#nav div {cursor: pointer;text-align: center;

        }
Copy after login

js

    $(function () {
        $(window).scroll(function () {//为页面添加页面滚动监听事件var wst = $(window).scrollTop(); //滚动条距离顶端值for (var i = 1; i < 6; i++) {             //加循环if ($("#f" + i).offset().top <= wst + 10) { //判断滚动条位置$(&#39;#nav div&#39;).css("background-color", "white");
                    $(".f" + i).css("background-color", "red");
                }
            }
        });
        $(&#39;#nav div&#39;).click(function () {
            $(&#39;html,body&#39;).animate({scrollTop: $("#" + this.className).offset().top}, 500);//          $("#" + this.className)[0].scrollIntoView(true);//h5 scrollIntoView()});
    });
Copy after login

All Code

h5之scrollIntoView控制页面元素滚动<div><h2>scrollIntoView</h2><button id="roll1">scrollIntoView(false)</button><button id="roll2">scrollIntoView(true)</button><div><div id="myDiv"></div><div id="roll_top">scrollIntoView(ture)元素上边框与视窗顶部齐平<span id="bottom">scrollIntoView(false)元素下边框与视窗底部齐平</span></div></div></div><div><h2>scroll</h2><div id="nav"><div class="f1">floor1</div><div class="f2">floor2</div><div class="f3">floor3</div><div class="f4">floor4</div><div class="f5">floor5</div></div><p>页面结构</p><div class="main"><div id="f1">测试1</div><div id="f2">测试2</div><div id="f3">测试3</div><div id="f4">测试4</div><div id="f5">测试5</div></div></div><script>window.onload = function () {/* 如果滚动页面也是DOM没有解决的一个问题。为了解决这个问题,浏览器实现了一下方法,以方便开发人员如何更好的控制页面的滚动。
         在各种专有方法中,HTML5选择了scrollIntoView()作为标准方法。scrollIntoView()可以在所有的HTML元素上调用,
         通过滚动浏览器窗口或某个容器元素,调用元素就可以出现在视窗中。如果给该方法传入true作为参数,或者不传入任何参数,
         那么窗口滚动之后会让调动元素顶部和视窗顶部尽可能齐平。如果传入false作为参数,调用元素会尽可能全部出现在视口中(可能的话,调用元素的底部会与视口的顶部齐平。)不过顶部不一定齐平.         */document.querySelector("#roll1").onclick = function () {
            document.querySelector("#roll_top").scrollIntoView(false);
        };
        document.querySelector("#roll2").onclick = function () {
            document.querySelector("#roll_top").scrollIntoView(true);
        };
    }</script><script>$(function () {
        $(window).scroll(function () {//为页面添加页面滚动监听事件var wst = $(window).scrollTop(); //滚动条距离顶端值for (var i = 1; i < 6; i++) {             //加循环if ($("#f" + i).offset().top <= wst + 10) { //判断滚动条位置                    $('#nav div').css("background-color", "white");
                    $(".f" + i).css("background-color", "red");
                }
            }
        });
        $('#nav div').click(function () {
            $('html,body').animate({scrollTop: $("#" + this.className).offset().top}, 500);//          $("#" + this.className)[0].scrollIntoView(true);//h5 scrollIntoView()});
    });</script>
Copy after login
View Code

The above is the detailed content of Detailed explanation of scrollIntoView usage in h5. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles