首頁  >  文章  >  web前端  >  js 居中漂浮廣告

js 居中漂浮廣告

高洛峰
高洛峰原創
2017-02-04 14:24:461591瀏覽

程式原始碼 

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) + &#39;px&#39;; 
} 
}; 
/* 目标居中并处于最底部 */ 
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 + &#39;px&#39;; 
this.control.style.left = this.left + &#39;px&#39;; 
}; 
/* fadeIn */ 
floatAd.fadeIn = function(callback) { 
var _this = this, _top = 0; 
this.control.style.display = &#39;block&#39;; // *要提前显示.不然无法取得clientWidth 
this.setCenter(); 
var _timer = window.setInterval(function() { 
_this.control.style.top = _this.getScrollTop() + _this.getBrowser().height - (++_top) + &#39;px&#39;; 
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 = &#39;none&#39;; 
} else { 
_this.control.style.top = _this.getScrollTop() + _this.getBrowser().height - (--_top) + &#39;px&#39;; 
} 
}, 2); 
this.control.style.top = (parseInt(this.control.style.top, 10) + 100) + &#39;px&#39;; 
}; 
var newAd = &#39;start&#39;; 
document.getElementById(&#39;show&#39;).onclick = function() { 
if(newAd == &#39;start&#39;) { 
newAd = floatAd.init(&#39;ad&#39;, { fadeTime: 10 }); 
} 
}

程式原理 
整個廣告運行具有四步驟動作. 
1. 初始化時隱藏於頁最底部. 
2. 自底向上升起.直到整個廣告漂浮出來 
3. 啟動偵測
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 = &#39;start&#39;; 
document.getElementById(&#39;show&#39;).onclick = function() { 
if(newAd == &#39;start&#39;) { 
newAd = floatAd.init(&#39;ad&#39;, { fadeTime: 10 }); 
} 
}

這裡為了示範方便.所以當點擊按鈕時候才初始化廣告.也可以在window.onload的時候就載入廣告. 

🎜更多js 居中漂浮廣告相關文章請關注PHP中文網! 🎜
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn