JS之两侧跟随的广告

需求:屏幕滚动多少,两侧的图片联动向下移动等距离

步骤:
1.老三步
2.获取被卷去的头部的值
3.移动两个盒子(缓动移动)

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
    <title></title>
<style type="text/css">
.img1{
    position: absolute;
    top:120px;
    left: 5px;
}
.img2{
    position: absolute;
    top: 120px;
    right: 5px;
}
div{
    width: 1210px;
    margin: 100px auto;
    text-align: center;
    font: 500 26px/35px "simsun";
}
</style>
</head>
<body>
<img class="img1"src="../images/aside.jpg"/>
<img class="img2"src="../images/aside.jpg"/>
<div>
<p>-------梦-------</p>
<p>-------梦-------</p>
<p>-------梦-------</p>
<p>-------梦-------</p>

<p>-------梦-------</p>
<p>-------梦-------</p>
<p>-------梦-------</p>
<p>-------梦-------</p>
<p>-------梦-------</p>
<p>-------梦-------</p>
<p>-------梦-------</p>
<p>-------梦-------</p>
<p>-------梦-------</p>
<p>-------梦-------</p>

<p>-------梦-------</p>
<p>-------梦-------</p>
<p>-------梦-------</p>

<p>-------梦-------</p>
<p>-------梦-------</p>
<p>-------梦-------</p>
<p>-------梦-------</p>
<p>-------梦-------</p>
<p>-------梦-------</p>

<p>-------如-------</p>
</div>
<script type="text/javascript" src="my.js"></script>
<script type="text/javascript">
//需求:屏幕滚动多少,两侧的图片联动向下移动等距离
//步骤:
//1.老三步
//获取被卷去的头部的值
//3.移动两个盒子(缓动移动)
//
//
var imgArr = document.getElementsByTagName("img");
window.onscroll = function(){
    var val = window.pageYOffset;
    animate(imgArr[0],val+80);
    animate(imgArr[1],val+80);
}
//缓动动画封装
function animate(ele,target) {
    clearInterval(ele.timer);
    ele.timer = setInterval(function () {
        var step = (target-ele.offsetTop)/10;
        step = step>0?Math.ceil(step):Math.floor(step);
        ele.style.top = ele.offsetTop + step + "px";
        console.log(1);
        if(Math.abs(target-ele.offsetTop)<Math.abs(step)){
            ele.style.top = target + "px";
            clearInterval(ele.timer);
        }
    },18);
}
</script>
</body>
</html>

 

转载于:https://www.cnblogs.com/creazybeauty/p/8144525.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值