Program source code
var floatAd = {}; floatAd.getScrollTop = function(node) { var doc = node ? node.ownerDocument : document; return doc.documentElement.scrollTop || doc.body.scrollTop; }; floatAd.getScrollLeft = function(node) { var doc = node ? node.ownerDocument : document; return doc.documentElement.scrollLeft || doc.body.scrollLeft; }; floatAd.getBrowser = function() { var d = document.documentElement; return { width: window.innerWidth || (d && d.clientWidth) || document.body.clientWidth, height: window.innerHeight || (d && d.clientHeight) || document.body.clientHeight } }; floatAd.extend = function(destination, source) { for(var property in source) { destination[property] = source[property]; } return destination; }; /* 默认属性扩展 */ floatAd.setOptions = function(options) { this.options = { delay: 20, // 调整速率 fadeTime: 1 // 自动消失时间 }; return this.extend(this.options, options || {}); }; /* 类初始化 */ floatAd.init = function(id, options) { var _this = this; this.extend(this, this.setOptions(options)); this.control = document.getElementById(id); var _callback = function() { // fadeIn完成后的回调函数 _this.timer = window.setInterval(function() { _this.scroll() }, _this.delay); // 滚动定位 window.setTimeout(function() { _this.fadeOut() }, _this.fadeTime * 1000); // 在固定时间内消失 } this.fadeIn(_callback); window.onresize = function() { _this.setCenter(); } }; /* 定时滚动 */ floatAd.scroll = function() { this.start = parseInt(this.control.style.top, 10); this.end = parseInt(this.getScrollTop() + this.getBrowser().height - this.control.clientHeight, 10); if(this.start != this.end) { this.amount = Math.ceil(Math.abs(this.end - this.start) / 15); /* 递减公式(this.start无限增大,整个分子无限减小,整个值就无限趋近于0) */ this.control.style.top = parseInt(this.control.style.top, 10) + ((this.end < this.start) ? -this.amount : this.amount) + 'px'; } }; /* 目标居中并处于最底部 */ floatAd.setCenter = function() { this.top = this.getScrollTop() + floatAd.getBrowser().height; this.left = (this.getScrollLeft() + floatAd.getBrowser().width - this.control.clientWidth) / 2; this.control.style.top = this.top + 'px'; this.control.style.left = this.left + 'px'; }; /* fadeIn */ floatAd.fadeIn = function(callback) { var _this = this, _top = 0; this.control.style.display = 'block'; // *要提前显示.不然无法取得clientWidth this.setCenter(); var _timer = window.setInterval(function() { _this.control.style.top = _this.getScrollTop() + _this.getBrowser().height - (++_top) + 'px'; if(_top >= _this.control.clientHeight) { window.clearInterval(_timer); callback && callback(); } }, 2); }; /* fadeOut */ floatAd.fadeOut = function() { var _this = this, _num = 0, _top = _this.control.clientHeight; window.clearTimeout(this.timer); var _timer = window.setInterval(function() { if(_top <= 0) { window.clearInterval(_timer); _this.control.style.display = 'none'; } else { _this.control.style.top = _this.getScrollTop() + _this.getBrowser().height - (--_top) + 'px'; } }, 2); this.control.style.top = (parseInt(this.control.style.top, 10) + 100) + 'px'; }; var newAd = 'start'; document.getElementById('show').onclick = function() { if(newAd == 'start') { newAd = floatAd.init('ad', { fadeTime: 10 }); } }
Program principle
The entire advertising operation has four steps.
1. Hidden at the bottom of the page during initialization.
2. Raise from bottom to top. Until The entire advertisement floats out
3. Start detection. Keep the advertisement always at the bottom of the middle of the page when scrolling.
4. When the custom interval is reached, the advertisement automatically fades out.
The most important thing about the entire implementation is to control the advertisement The distance from the top of the document (non-window). (scrollTop + browser.clientHeight). Here is the code to get these values.
Get scrollTop, scrollLeft
Note that Chrome and Safari even in standard doc mode The root document is also document.body instead of document.documentElement
floatAd.getScrollTop = function(node) { var doc = node ? node.ownerDocument : document; return doc.documentElement.scrollTop || doc.body.scrollTop; }; floatAd.getScrollLeft = function(node) { var doc = node ? node.ownerDocument : document; return doc.documentElement.scrollLeft || doc.body.scrollLeft; };
Get the width and height of the visual window
floatAd.getBrowser = function() { var d = document.documentElement; return { width: window.innerWidth || (d && d.clientWidth) || document.body.clientWidth, height: window.innerHeight || (d && d.clientHeight) || document.body.clientHeight } };
Code idea flow
Initialization (init) -----> Set the center and hide the bottom (setCenter) -----> fadeIn) -----> The fade-in is completed. Call the callback function _callback ----->
Start the countdown to fade-out Time (setTimeout(fadeOut, time)), and bind the real-time detection function (scroll) -----> Reach the custom time to hide the advertisement (fadeOut)
Instructions for use
Instantiation function. Pass in the advertisement Container ID. Set the default attributes.
The default attributes are:
delay: 20, // 调整速率 fadeTime: 1 // 自动消失时间(s) var newAd = 'start'; document.getElementById('show').onclick = function() { if(newAd == 'start') { newAd = floatAd.init('ad', { fadeTime: 10 }); } }
This is for the convenience of demonstration. So the advertisement is only initialized when the button is clicked. The advertisement can also be loaded during window.onload.
For more articles related to js centered floating ads, please pay attention to the PHP Chinese website!

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.


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

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

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

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

Dreamweaver Mac version
Visual web development tools
