JavaScript實現跟隨廣告的示例程式碼
阿新 • • 發佈:2021-11-09
浮動廣告是目前很常見的一種廣告形式,浮動廣告能實時跟隨使用者的瀏覽,有效的傳達產品要表達的意思,達到很好的傳播效果。那麼浮動廣告是怎麼實現的呢,其實實現浮動廣告並不難,具體如下:
* { margin: 0; padding: 0; } img { position: absolute; left: 0; } p { text-align: center; line-height: 40px; }
<img src="images/left_ad.png" alt="實現跟隨廣告的示例程式碼"> <p>我是正文1</p> <p>我是正文2</p> <p>我是正文3</p> <p>我是正文4</p> <p>我是正文5</p> <p>我是正文6</p> <p>我是正文7</p> <p>我是正文8</p> <p>我是正文9</p> <p>我是正文10</p> <p>我是正文11</p> <p>我是正文12</p> <p>我是正文13</p> <p>我是正文14</p> <p>我是正文15&lwww.cppcns.comt;/p> <p>我是正文16</p> <p>我是正文17</p> <p>我是正文18</p> <p>我是正文19</p> <p>我是正文20</p> <p>我是正文21</p> <p>我是正文22</p> <p>我是正文23</p> <p>我是正文24</p> <p>我是正文25</p> <p>我是正文26</p> <p>我是正文27</p> <p>我是正文28</p> <p>我是正文29</p> <p>我是正文30</p> <p>我是正文31</p> <p>我是正文32</p> <p>我是正文33</p> <p>我是正文34</p> <p>我是正文35</p> <p>我是正文36</p> <p>我是正文37</p> <p>我是正文38</p> <AbsjalNzsp>我是正文39</p> <p>我是正文40</p> <p>我是正文41</p> <p>我是正文42</p> <p>我是正文43</p> <p>我是正文44</p> <p>我是正文45</p> <p>我是正文46</p> <p>我是正文47</p> <p>我是正文48</p> <p>我是正文49</p> &www.cppcns.comlt;p>我是正文50</p>
//1.拿到需要操作的元素
const oAdImg = document.querySelector("img");
//2.計算廣告圖片top的值=(視口高度-廣告高度)/2
const screenHeight = getScreen().height;
const imgHeight = oAdImg.offsetHeight;
const offsetY = (screenHeight - imgHeight) / 2;
// console.log(offsetY);
//3.將計算之後的top值,設定給廣告圖片
// oAdImg.style.top = offsetY + 'px';
easeAnimation(oAdImg,{
"top": offsetY
AbsjalNzs });
//4.監聽的滾動事件
window.onscroll = function() {
//獲取到網頁滾動的距離
//廣告圖片top的值+網頁滾動的距離
let pageOffsetY = getPageScroll().y;
easeAnimation(oAdImg,{
"top": offsetY + pageOffsetY
});
};
// 瀏覽器視口寬高
function getScreen() {
let width,height;
if (window.innerWidth) {
width = window.innerWidth;
height = window.innerHeight;
} else if (document.compatMode === "BackCompat") {
width = document.body.clientWidth;
height = document.body.clientHeight;
} else {
width = document.documentElement.clientWidth;
height = document.documentElement.clientHeight;
}
return {
width: width,height: height
}
}
// 緩動動畫
function easeAnimation(ele,obj,fn) {
clearInterval(ele.timerId);
ele.http://www.cppcns.comtimerId = setInterval(function() {
// flag變數用於標記是否所有的屬性都執行完了動畫
let flag = true;
for (let key in obj) {
let target = obj[key];
// 1.拿到元素當前的位置
let style = getComputedStyle(ele);
let begin = parseInt(style[key]) || 0;
// 2.定義變數記錄步長
// 公式: (結束位置 - 開始位置) * 緩動係數(0 ~1)
let step = (target - begin) * 0.3;
// 3.計算新的位置
begin += step;
if (Math.abs(Math.floor(step)) > 1) {
flag = false;
} else {
begin = target;
}
// 4.重新設定元素的位置
ele.style[key] = begin + "px";
}
//判斷動畫是否執行完
if (flag) {
//動畫執行完後關閉定時器
clearInterval(ele.timerId);
//判斷是否傳入fn函式,有才執行反之不執行
fn && fn();
}
},100);
}
// 網頁滾動距離
function getPageScroll() {
let x,y;
if (window.pageXOffset) {
x = window.pageXOffset;
y = window.pageYOffset;
} else if (document.compatMode === "BackCompat") {
x = document.body.scrollLeft;
y = document.body.scrollTop;
} else {
x = document.documentElement.scrollLeft;
y = document.documentElement.scrollTop;
}
return {
x: x,y: y
}
}
效果圖
到此這篇關於Script實現跟隨廣告的示例程式碼的文章就介紹到這了,更多相關JavaScript 跟隨廣告內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!