JavaScript application analysis for DOM (5)_DOM
In fact, this should not be included in the scope of JS tutorials. It should be FF. IE6 and above browsers support the fixed attribute. Only IE6 does not support it. So I went for the damn IE6. I'll give you a tutorial here. Moreover, this effect can also exercise students' computing skills. Many of the effects in the future will require your computing power. Haha
Fixed to center. Or the thoughts are almost the same wherever they are fixed. As long as you want the algorithm to be clear. The effect is a breeze to write. Post the code first
[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
var isIE=!!window .ActiveXObject;
var isIE6=isIE&&!window.XMLHttpRequest;
These two sentences are used to determine the browser. This is the method in Aajx. It’s also easy to understand. I won’t say much more here. If you are interested, you can search online to learn about various browsers.
var t= (document.documentElement.clientHeight - document.getElementById("gd").offsetHeight)/2 document.documentElement.scrollTop;
var l = (document.documentElement.clientWidth - document. getElementById("gd").offsetWidth)/2 document.documentElement.scrollLeft;
These two sentences are the key points. I want to say it carefully:
document.documentElement.clientHeight is to get the height of the current browser window.
document.getElementById("gd").offsetHeight This is to get the actual height of our dom element.
document.documentElement.scrollTop This is to get the height of the scroll bar.
document.documentElement.clientHeight/2 is half the height of the entire browser minus document.getElementById("gd").offsetHeight/2 is half the height of our dom element. It is the top distance from the top of the current browser that our DOM element needs to be centered. But this is just a dead end. Because the content of the browser cannot be exactly less than or equal to the height of the browser's current window. Scroll bars will appear when the content is particularly high. Fortunately, we have document.documentElement.scrollTop, which is the height of our scroll bar. Adding it is the top distance from the top of the current browser when the real-time DOM element needs to be centered. Of course, the algorithm in the following sentence is the same as this one. I won’t go into more details and let everyone understand it. This will make a deep impression. a little.
I don’t know if you understand what I said. This should be explained with pictures and text. But I think what I said should be quite clear. If you are still a little confused. You can understand it by just drawing on paper.
If the distance is clear. Now we just have to do one last thing. That's when the scroll bar scrolls. The scroll height is added in real time. Then set the immediate t and l values to the top and left attributes of the dom. This can create a false impression for users. Thought this was fixed there. The code is as follows:
if(isIE6){
setInterval(function( ){
t = (document.documentElement.clientHeight - document.getElementById("gd").offsetHeight)/2 document.documentElement.scrollTop;
l = (document.documentElement.clientWidth - document.getElementById(" gd").offsetWidth)/2 document.documentElement.scrollLeft;
document.getElementById("gd").style.position = "absolute"
document.getElementById("gd").style.top = t "px";
document.getElementById("gd").style.left = l "px";
},1)
This paragraph is for this purpose. if(isIE6) is when isIE6 is true. That is, when it is an IE6 browser. setInterval(...,1) sets a clock to execute the statement every 1 microsecond in the IE6 browser. That is, the values of t and l are constantly updated. Then it continuously pays the top and left attributes of the DOM element, 1 microsecond is very fast. It is impossible for the user to pull the scroll bar only in 1 microsecond. So it is impossible for users to see anything. I thought it was fixed there. Of course you can also set a scroll event windon.onscroll. That's ok too. But I don’t think it’s that good. If you are interested, you can also research it (please don’t bother me with my verbosity, I think it will be more useful if I research more things by myself than if I teach you a lot of things).
Of course, browsers other than IE6 support the fixed attribute. Just execute these statements.
document.getElementById("gd").style.position = "fixed"//Add a fixed attribute to the dom element
document.getElementById("gd").style.top = t "px";/ /Set a centered t value to the dom element during initialization.
document.getElementById("gd").style.left = l "px";//Set a centered l value to the dom element during initialization
Okay. That’s it for this article. It's just centered here. There are also fixed bottom pulls, fixed left pulls, etc. The principles are the same. It's just that the calculation of top and left is different. If you are fine, you can do your own research. There are also two methods, setInterval and setTimeout. It will be used a lot in future tutorials.

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



1. Native js gets the DOM node: document.querySelector (selector) document.getElementById (id selector) document.getElementsByClassName (class selector).... 2. Get the instance object of the current component in vue2: because each vue Each component instance contains a $refs object, which stores references to the corresponding DOM elements or components. So by default, the component's $refs point to an empty object. You can first add ref="name" to the component, and then pass this.$refs.

In web development, DOM (DocumentObjectModel) is a very important concept. It allows developers to easily modify and operate the HTML or XML document of a web page, such as adding, deleting, modifying elements, etc. The built-in DOM operation library in PHP also provides developers with rich functions. This article will introduce the DOM operation guide in PHP, hoping to help everyone. The basic concept of DOM DOM is a cross-platform, language-independent API that can

Vue3ref binding DOM or component failure reason analysis scenario description In Vue3, it is often used to use ref to bind components or DOM elements. Many times, ref is clearly used to bind related components, but ref binding often fails. Examples of ref binding failure situations The vast majority of cases where ref binding fails is that when the ref is bound to the component, the component has not yet been rendered, so the binding fails. Or the component is not rendered at the beginning and the ref is not bound. When the component starts to render, the ref also starts to be bound, but the binding between ref and the component is not completed. At this time, problems will occur when using component-related methods. The component bound to ref uses v-if, or its parent component uses v-if to cause the page to

DOM is a document object model and an interface for HTML programming. Elements in the page are manipulated through DOM. The DOM is an in-memory object representation of an HTML document, and it provides a way to interact with web pages using JavaScript. The DOM is a hierarchy (or tree) of nodes with the document node as the root.

There are 5 DOM objects including "document", "element", "Node", "Event" and "Window"; 2. "window", "navigator", "location" and "history" and "screen" and other 5 BOM objects.

BOM and DOM are different in terms of role and function, relationship with JavaScript, interdependence, compatibility of different browsers, and security considerations. Detailed introduction: 1. Role and function. The main function of BOM is to operate the browser window. It provides direct access and control of the browser window. The main function of DOM is to convert the web document into an object tree, allowing developers to Use this object tree to obtain and modify the elements and content of the web page; 2. Relationship with JavaScript, etc.

dom内置对象有:1、document;2、window;3、navigator;4、location;5、history;6、screen;7、document.documentElement;8、document.body;9、document.head;10、document.title;11、document.cookie。

This article will introduce to you how to implement horizontal and vertical scrolling animation of DOM elements in js. I hope it will be helpful to friends in need!
