页面滚动固定

本文介绍了一种使用jQuery实现的布局方案,在页面滚动时保持两个区块的相对位置不变。通过监听文档滚动事件,调整区块的位置属性,使区块能够在特定条件下固定在视口底部。

实例代码:

 1 $(document).scroll(function () {
 2 
 3         var $leftBox = $('.lFixedBlock'),
 4                 $rightBox = $('.rFixedBlock'),
 5                 leftBoxHeight = $leftBox.height(),
 6                 rightBoxHeight = $rightBox.height(),
 7                 windowHeight = $(window).height(),
 8                 scrollTop = $(document).scrollTop(),
 9                 diffHeight = leftBoxHeight - rightBoxHeight;
10         if (diffHeight > 0) {
11             if ((scrollTop + windowHeight) > (leftBoxHeight + $leftBox.offset().top)) {
12                 $rightBox.css({'position': 'fixed', 'bottom': (scrollTop + windowHeight) - (leftBoxHeight + $leftBox.offset().top)});
13             } else if ((scrollTop + windowHeight) > (rightBoxHeight + $leftBox.offset().top)) {
14                 $rightBox.css({'position': 'fixed', 'bottom': 0});
15             } else {
16                 $rightBox.css({'position': 'static', 'bottom': 'auto'});
17             }
18         } else {
19             if ((scrollTop + windowHeight) > (rightBoxHeight + $rightBox.offset().top)) {
20                 $leftBox.css({'position': 'fixed', 'bottom': (scrollTop + windowHeight) - (rightBoxHeight + $rightBox.offset().top)});
21             } else if ((scrollTop + windowHeight) > (leftBoxHeight + $rightBox.offset().top)) {
22                 $leftBox.css({'position': 'fixed', 'bottom': 0});
23             } else {
24                 $leftBox.css({'position': 'static', 'bottom': 'auto'});
25             }
26         }
27 
28     });

 

转载于:https://www.cnblogs.com/xingmeng/p/3503703.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值