登录论坛 | 注册会员 设为首页 | 收藏本站
当前位置 : 首页>软件学院>程序开发>网页设计>正文
 
纯JS实现网页漂浮广告效果的代码

http://www.dbit.cn 2010/3/22 7:23:39  来源:东北IT网  编辑:叶子
 

本文给大家介绍使用js来实现页漂浮广告,在百度空间,知道里有时候搜索出现过这种效果。

先来看下程序源码:

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';
    }
};

/* 目标居中并处于最底部 */

本新闻共3页,当前在第1页  1  2  3  

收藏】【打印】【进入论坛
  相关文章:

 
 
 
最新文章

抢先苹果,消息称英特尔芯片采用台积电
三星揭晓业内首款单条 512GB DDR5 内存
vivo 高端新机爆料:120Hz 曲面屏 + 天
vivo Y21 在印度正式上市:Helio P35 芯
微星推出 GeForce RTX 3080 Sea Hawk X
消息称三星 Galaxy Tab S8 系列平板将放
机械革命推出 F6 轻薄本:16 英寸全面屏
英特尔 12 代 Alder Lake CPU 600 系列
雷军:向小米手机 1 首批用户每人赠送价
小米李明谈用户被踢出 MIUI 测试版:大

推荐文章
1
2
3
4
5
6
7
8
9
10
叛逆嫩模性感写真
宫如敏不雅照疯传 看张馨予韩一菲兽兽谁
不惧孔子抢位 阿凡达游戏影音配置推荐
2015第十七届“东北安博会”火爆招商
第十六届东北国际公共安全防范产品博览
2016年第五届中国国际商业信息化博览会
2016年第五届中国国际POS机及相关设备展
互联网电视熟了吗 2013最火电视深解析
桑达获邀出席2015中国(广州)国际POS机
宝获利报名参加“2015年度中国POS机行业
八卦图解 More>>
叛逆嫩模性感写真 宫如敏不雅照疯传 看张馨予韩一菲
周伟童魔鬼身材日本性感写真图  联想V360笔记本模特写真