JavaScript怎么实现页面滑动一起跟随的广告效果
Admin 2022-06-21 群英技术资讯 811 次浏览
在这篇文章中,我们来学习一下“JavaScript怎么实现页面滑动一起跟随的广告效果”的相关知识,下文有详细的讲解,易于大家学习和理解,有需要的朋友可以借鉴参考,下面就请大家跟着小编的思路一起来学习一下吧。浮动广告是目前网站很常见的一种广告形式,浮动广告能实时跟随用户的浏览,有效的传达产品要表达的意思,达到很好的传播效果。那么浮动广告是怎么实现的呢,其实实现浮动广告并不难,具体如下:
* {
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</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>
<p>我是正文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>
<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
});
//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.timerId = 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
}
}
效果图

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
idea编译器出现Vue缩进报错,怎么解决呢,很多朋友遇到这个问题都很棘手,不知该如何解决,今天小编给大家通过场景分析介绍解决方案,需要的朋友参考下吧
目录正文拖拽旋转缩放小结正文到目前为止,我们已经能够对物体进行点选和框选的操作了,但是这还不够,因为并没有什么实际性的改变,并且画布看起来也有点呆板,所以这个章节的主要目的就是让画布中的物体活起来,其实就是增加一些常见的交互而已啦????,比如拖拽、旋转和缩放。这是这个系列最重要的章节之一,希望能够对你有所帮助。拖拽先来说说
虽然 vue-router 4 大多数 API 保持不变,但是在 vue3 中以插件形式存在,所以在使用时有一定的变化。接下来就学习学习它是如何使用的
这篇文章主要为大家介绍了如何编写Vue插件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
这篇文章主要为大家详细介绍了elementUI实现级联选择器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008