Ceiling is a relatively common interactive effect. When the page slides out of the screen boundary, the title will automatically be adsorbed on the edge of the screen to remind the user. This article mainly introduces the relevant information of the title ceiling function of Html5. Interested friends can refer to
Ceiling function
Ceiling is a relatively common interactive effect. When the page slides When the title exceeds the edge of the screen, the title will be automatically attached to the edge of the screen to remind the user.
Basic Principle
The basic principle implemented in H5 is to determine the relationship between the sliding distance of the current page scrollTop and the distance between the title and the top of the page offsetTop , and then set the position of the title = fixed. Here you need to understand the meaning of scrollTop and offsetTop attributes.
scrollTop
represents the distance the scroll bar scrolls down when there is a scroll bar, which is the height of the blocked part of the top of the element. scrollTop==0 is always true when there is no scroll bar. The unit is px, readable and settable.
offsetTop
The distance between the top of the current element and the top of the nearest parent element has nothing to do with whether there is a scroll bar or not. Unit px, read-only element.
So, when scrollTop>offsetTop, the title's position = fixed, top = 0, so that it is fixed at the top of the screen; when scrollTop
if (fixedDom[0].offsetTop - elementScrollTop < 0){ fixedDom.addClass("road-tab-fixed") }else { fixedDom.removeClass("road-tab-fixed") }
The effect is as follows:
##Optimization
The picture shows that the basic functions are almost implemented, but something feels weird. When the page slides up, the effect is relatively natural. However, when the page slides down, the title will return to its original position only when the page slides completely to the top, causing it to be excessively unnatural. Therefore, the position of the title must be set in two parts. Two situations: sliding up and down.Judge up and down sliding direction
Judge up and down sliding click hereWhen the page slides upWhen scrollTop>offsetTop , the position of the title = fixed, top = 0, so that it is fixed at the top of the screen; When the page slides downWhen scrollTopif(beforeElementScrollTop - elementScrollTop <=0){//up console.log('up'); if (beforeOffsetTop - elementScrollTop < 0){ fixedDom.addClass("road-tab-fixed") } }else{ console.log('down'); // console.log('beforeOffsetTop-----------',beforeOffsetTop); // console.log('elementScrollTop--------------',elementScrollTop); if (beforeOffsetTop - elementScrollTop >= 0){ fixedDom.removeClass("road-tab-fixed") } }The effect is as follows:
Optimization of scroll throttling
When the scroll event is monitored on the page, the scroll callback will always be executed when sliding, affecting the page performance and throttling A function is only allowed to be executed once within X milliseconds. The next function call can only be made after the time interval you specify has passed since the last function execution. The code is as followsconst fixedDom = $("#road-tab"), isIos = utils.getMobileType(), tabclass = "road-tab-fixed"; let beforeElementScrollTop = 0; let beforeOffsetTop = fixedDom[0].offsetTop; //scroll节流 const throttle = (func,wait,mustRun) => { var timeout, startTime = new Date(); return function() { var context = this, args = arguments, curTime = new Date() clearTimeout(timeout) // 如果达到了规定的触发时间间隔,触发 handler if(curTime - startTime >= mustRun){ beforeElementScrollTop = document.body.scrollTop; console.log("beforelementScrollTop----------",document.body.scrollTop); func.apply(context,args); startTime = curTime // 没达到触发间隔,重新设定定时器 }else{ timeout = setTimeout(func, wait) } } } const winScroll = (e) => { const elementScrollTop=document.body.scrollTop; console.log('elementScrollTop--------------',elementScrollTop); if(beforeElementScrollTop - elementScrollTop <=0){//up console.log('up'); if (beforeOffsetTop - elementScrollTop < 0){ fixedDom.addClass("road-tab-fixed") } }else{ if (beforeOffsetTop - elementScrollTop >= 0){console.log("UUUUUU"); fixedDom.removeClass("road-tab-fixed") } } }; $(window).off("scroll").on("scroll", throttle(winScroll,10,100));
The above is the detailed content of Html5 title ceiling function. For more information, please follow other related articles on the PHP Chinese website!

html5的div元素默认一行不可以放两个。div是一个块级元素,一个元素会独占一行,两个div默认无法在同一行显示;但可以通过给div元素添加“display:inline;”样式,将其转为行内元素,就可以实现多个div在同一行显示了。

html5中列表和表格的区别:1、表格主要是用于显示数据的,而列表主要是用于给数据进行布局;2、表格是使用table标签配合tr、td、th等标签进行定义的,列表是利用li标签配合ol、ul等标签进行定义的。

固定方法:1、使用header标签定义文档头部内容,并添加“position:fixed;top:0;”样式让其固定不动;2、使用footer标签定义尾部内容,并添加“position: fixed;bottom: 0;”样式让其固定不动。

html5中不支持的标签有:1、acronym,用于定义首字母缩写,可用abbr替代;2、basefont,可利用css样式替代;3、applet,可用object替代;4、dir,定义目录列表,可用ul替代;5、big,定义大号文本等等。

HTML5中画布标签是“<canvas>”。canvas标签用于图形的绘制,它只是一个矩形的图形容器,绘制图形必须通过脚本(通常是JavaScript)来完成;开发者可利用多种js方法来在canvas中绘制路径、盒、圆、字符以及添加图像等。

html5废弃了dir列表标签。dir标签被用来定义目录列表,一般和li标签配合使用,在dir标签对中通过li标签来设置列表项,语法“<dir><li>列表项值</li>...</dir>”。HTML5已经不支持dir,可使用ul标签取代。

3种取消方法:1、给td元素添加“border:none”无边框样式即可,语法“td{border:none}”。2、给td元素添加“border:0”样式,语法“td{border:0;}”,将td边框的宽度设置为0即可。3、给td元素添加“border:transparent”样式,语法“td{border:transparent;}”,将td边框的颜色设置为透明即可。

因为html5不基于SGML(标准通用置标语言),不需要对DTD进行引用,但是需要doctype来规范浏览器的行为,也即按照正常的方式来运行,因此html5只需要写doctype即可。“!DOCTYPE”是一种标准通用标记语言的文档类型声明,用于告诉浏览器编写页面所用的标记的版本。


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

Zend Studio 13.0.1
Powerful PHP integrated development environment

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

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools