程序源码
复制代码 代码如下:
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 });
}
}
程序原理
整个广告运行具有四步动作.
1. 初始化时隐藏于页面最底部.
2. 自底向上升起.直到整个广告漂浮出来
3. 启动检测.滚动时保持广告始终处于页面中间最底部.
4. 到达自定义间隔时间.广告自动渐隐.
整个实现最重要的就是控制广告距离文档(非窗口)最顶部的距离.(scrollTop + browser.clientHeight).这里提供了获取这几个值的代码.
获取scrollTop, scrollLeft
注意Chrome和Safari即使在标准doc模式下的根文档也是document.body而不是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;
};
获取可视窗口的宽高
复制代码 代码如下:
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
}
};
代码思路流程
初始化(init) -----> 设置居中并隐藏底部(setCenter) -----> 渐显(fadeIn) -----> 渐显完毕.调用回调函数_callback ----->
开始倒计时渐隐时间(setTimeout(fadeOut, time)), 并绑定实时检测函数(scroll) -----> 到达自定义时间隐藏广告(fadeOut)
使用说明
实例化函数.传入广告容器ID.设置默认属性.
默认属性有:
复制代码 代码如下:
delay: 20, // 调整速率
fadeTime: 1 // 自动消失时间(s)
var newAd = 'start';
document.getElementById('show').onclick = function() {
if(newAd == 'start') {
newAd = floatAd.init('ad', { fadeTime: 10 });
}
}
这里为了演示方便.所以当点击按钮时候才初始化广告.也可以在window.onload的时候就载入广告.
演示下载地址 居中显示的漂浮广告代码
相关推荐:
AI缩写文本:助力智能生活的革新力量,ai智能写作生成神器下载
ChatGPT免费用户每天的使用限制:如何高效利用,突破困境!,花花制作ai
SEO领站-从零到一打造成功网站排名的秘密,轻淘客seo怎设置
GoogleSEO打满分自然流量会高吗?揭开SEO优化的真相,猪插画ai
seo要寻找什么资源,怎么找seo ,ai插画描边
SEO要不,来看看如何通过SEO优化提高网站流量和曝光度,seo 获客技巧
seo网站是什么东西,seo网站是什么东西啊 ,ai锯齿消失
SEO站群:打造强大网络营销引擎,助力企业快速提升排名与流量,seo网站排名案例
SEM做得好可以取代SEO吗?浅析两者的异同与未来趋势,王道ai
ChatGPT页面无法翻阅?带你深度了解背后的原因与解决方案,pdf怎么转换ai文字
什么是SEO优化方案,seo的优化方案 ,ai emorobot
ChatGPT宕机两小时,OpenAI紧急修复,用户期待AI恢复正常服务,oppo小布ai
优化*:打造更加高效、创新的娱乐体验,石排网站建设制作多少钱
AI人工智能文章生成平台,释放创作无限可能
SEO培训:助力企业实现互联网营销的无限可能,平塘网站优化推广价格
产品经理seo是什么,产品经理seo是什么意思 ,ai政府公文写作 软件
ChatGPT遇到问题?如何解决“您的应用遇到问题,无法正常启动”困境?,ai下载增强版
seo要懂些什么软件,seo常用软件 ,ai写作网站网址大全
主流seo是什么,seo是什么推广网站 ,AI心理师
AI一键生成文章免费:革新写作方式,提升创作效率
ChatGPT常见问题汇总:解答你关于AI的一切疑惑,松鼠ai智能教适应教育
seo站内优化包括什么营销,seo站内优化操作流程 ,ai 玻璃图标
ChatGPT破解:人工智能未来的无限可能,污污AI解说
什么是SEO金融,seo是做什么的 ,ai中转程序
seo网络推广要做什么,seo 网络推广 ,ai少女喝药
SEO手法如何通过精准优化提升网站排名,获取海量流量,网站建设协议流程是什么
什么是seo技术,什么是seo及seo的作用 ,AI倒放仓鼠
ChatGPT国内版:为中国用户量身定制的智能助手,开启AI新纪元,ai文章赚钱
用AI写文章会不会查重率高?破解写作困扰的真相
优化,让生活与工作更高效-从个人到企业的全面升级,广州百度seo费用
SEO和品牌营销:如何通过搜索引擎优化打造品牌影响力,建邺seo软件
SEO考核:如何通过精准的SEO优化提升网站排名与流量,茶艺营销推广方案怎么写
AI免费试用不需要登录:体验智能科技的魅力,轻松开启未来,华为ai身材
怎么用AI写文:让创作更轻松,效率翻倍
SEO超级,助力企业站点冲刺搜索引擎排名,白云全网营销推广哪家强
什么是seo优化营销,seo主要优化什么 ,ai绘画国风古韵
什么是seo发外链,seo外链类型有哪些 ,小小苏ai
OpenAI推出的GPT-4Turbo大幅降低了AI应用成本,推动了AI技术的普及化,ai*版
SEO课:让你从小白变成搜索引擎优化高手,推广自媒体营销计划
SEO优化做什么的?揭秘SEO优化的核心价值与实战应用,ai做金色
SEO有话:如何用精准优化助力企业在线增长,食品微信营销推广
SEO用户:如何为您的网站带来持续流量和转化,惠州网站推广哪个好
优化工具:提升工作效率的秘密武器,网站模板的优化策略是什么
文章去AI回归创作的本真之美
用AI生成文章,让创作更简单高效
打破创作边界,无限可能无限制生成文章的AI
seo系列什么意思,seo的分类 ,que n ai je
AI人工智能文章生成器写作新纪元
什么是seo快排,seo快排方案 ,ai剪图形
seo网站编辑是做什么,seo网站编辑可在家兼职 ,ai慢直播