position:fixed;实现底层页面不跟随浮层模块滚动

        由于工作需要,在首页做一个搜索浮层,要求浮层全屏显示(即:width:100%;height:100%;)。当页面内容超出屏幕高度时,页面添加滚动条(overflow:scroll;)。

        

        搜索页面(浮层)代码:


        可是问题是,当我们滑动到浮层底部或顶部时,底层页面(首页)也会跟着滑动,那么就会出现底层页面的滚动条。



        同样,当浮层滑动到顶部,如果继续滑动页面的话,底层(首页)也会跟随滚动……如果在手机端测试,你会发现有两个滚动条的出现!

          代码如下:
<span style="font-family:Microsoft YaHei;font-size:14px;"><!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=0.5,maximum-scale=1.0,user-scalable=0" />
	    <meta name="apple-mobile-web-app-capable" content="yes">
	    <meta name="apple-mobile-web-app-status-bar-style" content="black">
	    <meta name="format-detection" content="telephone=no">
		<title></title>
		<style type="text/css">
			*{padding: 0;margin: 0;}
			html{font-size: 40px;}
			body{color: #333;font-family: "microsoft yahei";}
			
			article{padding: 0 .8rem;margin-top: .5rem;overflow: hidden;}
			li{list-style: none;float: left;width: 25%;text-align: center;line-height: 1.6;}			
			h2{font-weight: normal;font-size: 42px;text-align: center;}

			.open-box{font-size: 30px;margin:20px 0;overflow: hidden;}
			.open-box span{width:260px;height:80px;background: #1BBC9B;color: #fff;text-align: center;line-height: 80px;display: block;margin: 0 auto 30px;}
			
			.open-box .box{width: 100%;height: 100%;background: #fff;position: fixed;left: 0;top: 0;line-height: 40px;display: none;overflow: scroll;}
			
			.open-box p{padding: 20px 30px;display: table;height: 1px;}
			.open-box .closed{margin-bottom:30px;}
		</style>
	</head>
	<body>
		<h1>首页--底层页面</h1>
		<article>
			<div class="open-box">
				<span>点击打开搜索页面</span>
				<div class="box">
					<h2>搜索页面--浮层</h2>
					<div style="height: 15rem;">内容模块div</div>
					<p>在面向对象的编程语言中,类是对对象的抽象,在类中可以定义对象的属性和方法的描述;对象是类的实例,类只有被实例化后才能被使用。定义类  在PHP中,使用关键字class加类名的方式定义类,然后用大括号包过类体,在类体中定义类的属性和方法。在面向对象的编程语言中,类是对对象的抽象,在类中可以定义对象的属性和方法的描述;对象是类的实例,类只有被实例化后才能被使用。定义类  在PHP中,使用关键字class加类名的方式定义类,然后用大括号包过类体,在类体中定义类的属性和方法。</p>
					<div style="height: 10rem;">内容模块div</div>
					<span class="closed">关闭搜索页面</span>
				</div>
			</div>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
			<br>
		</article>
		<script src="js/zepto.js" type="text/javascript"></script>
		<script type="text/javascript">
			$(function(){
				$(".open-box span").click(function(){
					$(this).next(".box").show(300);
				});
				$(".closed").click(function(){
					$(this).parent().hide(300);
				});
			})
		</script>
	</body>
</html>
</span>
        我试过设置<body>的height为100%,overflow:hidden;很遗憾的是不支持……
        最后,我无意中发现通过设置<body>的固定定位(position:fixed)居然可以实现!


        看你怎么滑动,都不会跟随滚动……

        有兴趣的童鞋可以试试,JS代码如下

        $(function(){

                $(".open-box span").click(function(){

                        $(this).next(".box").show(300);

                        $("body").css("position","fixed");

                });

                $(".closed").click(function(){

                        $(this).parent().hide(300);

                        $("body").css("position","relative");

                });

        });

 
       希望有所帮助……谢谢!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值