Detailed explanation of scrollIntoView usage in h5
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 id="scrollIntoView">scrollIntoView</h2> <button>scrollIntoView(false)</button><button>scrollIntoView(true)</button><div> <div></div> <div>scrollIntoView(ture)元素上边框与视窗顶部齐平<span>scrollIntoView(false)元素下边框与视窗底部齐平</span> </div> </div> </div>
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;
}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);
};
}2. Scroll monitoring
html
<div> <h2 id="scroll">scroll</h2> <div> <div>floor1</div> <div>floor2</div> <div>floor3</div> <div>floor4</div> <div>floor5</div> </div> <p>页面结构</p> <div> <div>测试1</div> <div>测试2</div> <div>测试3</div> <div>测试4</div> <div>测试5</div> </div> </div>
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;
}js
$(function () {
$(window).scroll(function () {//为页面添加页面滚动监听事件var wst = $(window).scrollTop(); //滚动条距离顶端值for (var i = 1; i All Code


nbsp;html>h5之scrollIntoView控制页面元素滚动 <div> <h2 id="scrollIntoView">scrollIntoView</h2> <button>scrollIntoView(false)</button><button>scrollIntoView(true)</button><div> <div></div> <div>scrollIntoView(ture)元素上边框与视窗顶部齐平<span>scrollIntoView(false)元素下边框与视窗底部齐平</span> </div> </div> </div><div> <h2 id="scroll">scroll</h2> <div> <div>floor1</div> <div>floor2</div> <div>floor3</div> <div>floor4</div> <div>floor5</div> </div> <p>页面结构</p> <div> <div>测试1</div> <div>测试2</div> <div>测试3</div> <div>测试4</div> <div>测试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></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>
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!
H5: The Evolution of Web Standards and TechnologiesApr 15, 2025 am 12:12 AMWeb standards and technologies have evolved from HTML4, CSS2 and simple JavaScript to date and have undergone significant developments. 1) HTML5 introduces APIs such as Canvas and WebStorage, which enhances the complexity and interactivity of web applications. 2) CSS3 adds animation and transition functions to make the page more effective. 3) JavaScript improves development efficiency and code readability through modern syntax of Node.js and ES6, such as arrow functions and classes. These changes have promoted the development of performance optimization and best practices of web applications.
Is H5 a Shorthand for HTML5? Exploring the DetailsApr 14, 2025 am 12:05 AMH5 is not just the abbreviation of HTML5, it represents a wider modern web development technology ecosystem: 1. H5 includes HTML5, CSS3, JavaScript and related APIs and technologies; 2. It provides a richer, interactive and smooth user experience, and can run seamlessly on multiple devices; 3. Using the H5 technology stack, you can create responsive web pages and complex interactive functions.
H5 and HTML5: Commonly Used Terms in Web DevelopmentApr 13, 2025 am 12:01 AMH5 and HTML5 refer to the same thing, namely HTML5. HTML5 is the fifth version of HTML, bringing new features such as semantic tags, multimedia support, canvas and graphics, offline storage and local storage, improving the expressiveness and interactivity of web pages.
What Does H5 Refer To? Exploring the ContextApr 12, 2025 am 12:03 AMH5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo
H5: Tools, Frameworks, and Best PracticesApr 11, 2025 am 12:11 AMThe tools and frameworks that need to be mastered in H5 development include Vue.js, React and Webpack. 1.Vue.js is suitable for building user interfaces and supports component development. 2.React optimizes page rendering through virtual DOM, suitable for complex applications. 3.Webpack is used for module packaging and optimize resource loading.
The Legacy of HTML5: Understanding H5 in the PresentApr 10, 2025 am 09:28 AMHTML5hassignificantlytransformedwebdevelopmentbyintroducingsemanticelements,enhancingmultimediasupport,andimprovingperformance.1)ItmadewebsitesmoreaccessibleandSEO-friendlywithsemanticelementslike,,and.2)HTML5introducednativeandtags,eliminatingthenee
H5 Code: Accessibility and Semantic HTMLApr 09, 2025 am 12:05 AMH5 improves web page accessibility and SEO effects through semantic elements and ARIA attributes. 1. Use, etc. to organize the content structure and improve SEO. 2. ARIA attributes such as aria-label enhance accessibility, and assistive technology users can use web pages smoothly.
Is h5 same as HTML5?Apr 08, 2025 am 12:16 AM"h5" and "HTML5" are the same in most cases, but they may have different meanings in certain specific scenarios. 1. "HTML5" is a W3C-defined standard that contains new tags and APIs. 2. "h5" is usually the abbreviation of HTML5, but in mobile development, it may refer to a framework based on HTML5. Understanding these differences helps to use these terms accurately in your project.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools

Dreamweaver CS6
Visual web development tools







