Home Web Front-end JS Tutorial Solution to the problem that document.body.scrollTop value is always 0. Common standard problems_javascript skills

Solution to the problem that document.body.scrollTop value is always 0. Common standard problems_javascript skills

May 16, 2016 pm 06:40 PM
standard

When making pages, you may use fixed-position layers. Read document.body.scrollTop to set the position of the layer, like this:

window.onscroll = function (){
var oFix = document .getElementById("divfix");
oFix.style.top = document.body.scrollTop "px";
}

But why did it not achieve the expected effect, output document.body.scrollTop Looking at the value, it is always 0. It turns out to be a DTD problem. If the page starts directly with , there will be no problem. But to comply with web standards, DTD is of course indispensable. When you have a DTD just use document.documentElement.scrollTop instead of document.body.scrollTop.

window.onscroll = function (){
var oFix = document.getElementById("divfix");
oFix.style.top = document.documentElement.scrollTop "px";
}

Editor's Note:

When the page has a DTD (or DOCTYPE is specified), use document.documentElement.
When the page does not have a DTD (or DOCTYPE is not specified), use document.body.
This is true in both IE and Firefox.
For compatibility, you can use the following code: var scrollTop = window.pageYOffset
|| document.documentElement.scrollTop
|| document.body.scrollTop
|| 0;

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Principles and standards for PHP function library design Principles and standards for PHP function library design Jun 16, 2023 am 11:37 AM

As the importance of PHP in Web development continues to increase, PHP function library design has become one of the key issues in development. A good function library can not only improve development efficiency, but also ensure code quality and maintainability. Therefore, designing function libraries needs to follow some basic principles and standards. 1. A function library with good reusability should be reusable and can be used in different projects. Therefore, functions should be abstract and general and cannot be tied to a specific project or scenario. 2. Ease of use The function library should be easy to use and pass parameters

What criteria can be used to classify PHP functions? What criteria can be used to classify PHP functions? Apr 19, 2024 am 09:30 AM

PHP functions can be classified according to various criteria: purpose (string processing, array processing, etc.), scope (built-in functions, user-defined functions) and return value (with or without return value). Practical case: The explode() function can split a string into an array with a specified delimiter.

Demystifying the key elements of sticky positioning: How to evaluate the criteria for sticky positioning? Demystifying the key elements of sticky positioning: How to evaluate the criteria for sticky positioning? Jan 28, 2024 am 10:39 AM

How to judge the criteria for sticky positioning? Revealing the key elements of sticky positioning Introduction: Sticky positioning, as a market positioning strategy, plays a vital role in corporate brand promotion and market competition. However, how to judge the criteria for sticky positioning is a headache. This article will reveal the key elements of sticky positioning and help companies better judge the criteria for sticky positioning. 1. Understand the significance of sticky positioning Sticky positioning means that an enterprise generates strong identification and loyalty among consumers through its unique brand characteristics and core competitive advantages, thus forming a

Analyzing the benchmarks and core requirements of sticky positioning: an in-depth discussion Analyzing the benchmarks and core requirements of sticky positioning: an in-depth discussion Jan 28, 2024 am 08:07 AM

The standard of sticky positioning refers to the ability of a company or brand to occupy a fixed position in consumers' minds for a long time in market competition and to stably maintain market share and brand loyalty. Sticky positioning is an important concept in marketing. It emphasizes that in a fiercely competitive market environment, companies need to establish their own unique positioning and establish close relationships with consumers to maintain competitive advantages. The core requirements of sticky positioning include the following aspects: Uniqueness: The positioning of a company or brand in the market must be unique and different from competitors.

What you need to know about general artificial intelligence What you need to know about general artificial intelligence May 09, 2023 pm 02:25 PM

Recently, there has been increasing discussion about generative artificial intelligence tools, especially after the release of several large-scale language models and image generators such as DALL-E or Midjourney. These inventions have once again put general artificial intelligence (GPAI) under the spotlight and once again raised hypothetical questions such as whether GPAI should be regulated. Before one explores the possibilities further, first understand the concept of GPAI, what it means, when was it introduced, etc. What is general artificial intelligence? Two years ago, in April 2021, the European Commission launched general artificial intelligence. The original AI bill proposal exempted creators of general artificial intelligence from complying with a number of legal instruments and other liability standards. reason

A Deep Dive into PHP's Standard Markup A Deep Dive into PHP's Standard Markup Mar 29, 2024 pm 03:39 PM

In web development, PHP is a widely used programming language. Its standard tags are special tag symbols in PHP code that are used to identify the beginning and end of PHP code. It is very important to have an in-depth understanding of PHP's standard tags because they directly affect the parsing and execution process of PHP code. This article will delve into PHP's standard tags and provide specific code examples to help readers better understand and use PHP programming. 1. PHP standard tags In PHP, the most commonly used standard tags are "

Does the Go language meet the standards of upper-level languages? Does the Go language meet the standards of upper-level languages? Mar 13, 2024 am 11:39 AM

Title: Does the Go language meet the standards of upper-level languages? In recent years, Go language, as an emerging programming language, has received widespread attention and application. As a statically typed, compiled language, the Go language has unique advantages in concurrent programming, memory management, and code readability. However, in the eyes of some programmers, it does not fully meet the standards that upper-level languages ​​should have. This article will discuss whether the Go language meets the standards of the upper-level language from several aspects, and discuss it with specific code examples. 1. Code simplicity. Upper-level languages ​​are usually

Standards for sticky positioning and analysis of elements and requirements for sticky positioning Standards for sticky positioning and analysis of elements and requirements for sticky positioning Feb 02, 2024 pm 12:36 PM

Sticky positioning is a common web layout technique that provides a better user experience by keeping elements in a fixed position as they are scrolled. This article will analyze the standards, elements, and requirements for sticky positioning, and provide specific code examples. 1. Standard compatibility of sticky positioning: Sticky positioning should work normally on mainstream browsers, such as Chrome, Firefox, Safari, etc. Scroll effect: Elements should transition smoothly when scrolling to avoid flickering or jittering. Responsive design: Sticky positioning should adapt to different devices and screen sizes to ensure

See all articles