[英]Prevent body scrolling on mobile device
How to stop scrolling of body when modal appears and allow fixed div scroll ?当模态出现并允许固定 div 滚动时如何停止正文滚动? I tried but i can't handle it.
我试过了,但我无法处理。 Any solution ?
有什么解决办法吗?
<div class="search">
<input type="text" placeholder="search here" class="searchMe">
</div>
<div id="modal"></div>
<div class="result">
<ul>
<li>Result from type</li>
<!-- Live example have lot of items -->
</ul>
</div>
<div class="content">
Many items will be place here
</div>
Here is link i am working on这是我正在处理的链接
I tried -webkit-overflow-scrolling: auto
and body
style with overflow: hidden
.我尝试
-webkit-overflow-scrolling: auto
和body
style with overflow: hidden
。
Changing the height of the content to 100% on open and setting overflow: hidden;打开时将内容的高度更改为 100% 并设置溢出:隐藏; will do the trick.
会成功的。 So write a css class that has
所以写一个有
.no-scroll {height:100%; overflow:hidden;}
then use jQuery to add that class when the input has focus and remove the class when user leaves focus.然后在输入具有焦点时使用 jQuery 添加该类,并在用户离开焦点时删除该类。
Heres a sample, note you will have to view the snippet in full page to see the no scroll.这是一个示例,请注意,您必须在整页中查看片段才能看到无滚动。 You will have to also work around Touch Move for ios Here is a codepen off of yours that does what you want.
您还必须解决 iOS 的 Touch Move这是一个您想要的代码笔。
jQuery(document).ready(function(){ var field = $(".searchMe"); field.keypress(function(){ $("#modal").show(); $('.content').addClass('no-scroll') //add class $(".result").show(); }); field.blur(function(){ $("#modal").hide(); $(".result").hide(); $('.content').removeClass('no-scroll') //remove class }); });
* { margin: 0; padding: 0; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; &::before, &::after { -webkit-box-sizing: inherit; -moz-box-sizing: inherit; box-sizing: inherit; } } body { background: #e6e5e4; } .content { padding-top: 60px; width: 100%; height: 1800px; background: darken(#e6e5e4, 15%); } // Search .search { position: fixed; top: 0; z-index: 2; height: 50px; width: 100%; background: #eee; input { width: 100%; height: 100%; border: 1px solid #374c45; font-size: 16px; padding: 0 0 0 1em; } } #modal { display: none; position: fixed; top: 0; width: 100%; height: 100%; z-index: 1; background: rgba(0,0,0,0.6); } .result { display: none; position: fixed; z-index: 2; top: 50px; bottom: 0; width: 100%; overflow-y: scroll; background: transparent; } /*class to stop scrolling */ .no-scroll {overflow:hidden;height:100%;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <div class="search"> <input type="text" placeholder="search here" class="searchMe"> </div> <div id="modal"></div> <div class="result"> <ul> <li>Result from type</li> <li>Result from type</li> <li>Result from type</li> <li>Result from type</li> <li>Result from type</li> <li>Result from type</li> <li>Result from type</li> <li>Result from type</li> <li>Result from type</li> <li>Result from type</li> <li>Result from type</li> <li>Result from type</li> <li>Result from type</li> <li>Result from type</li> <li>Result from type</li> <li>Result from type</li> <li>Result from type</li> <li>Result from type</li> <li>Result from type</li> <li>Result from type</li> </ul> </div> <div class="content"> Many items will be place here </div>
this.$scrollableEl.on('touchmove', function(event){ event.comesFromScrollable = true; //when you have containers that are srollable but //doesn't have enough content to scroll sometimes: //event.comesFromScrollable = el.offsetHeight < el.scrollHeight; }) $document.on('touchmove', function(event){ if(!event.comesFromScrollable){ event.preventDefault() } })
Is this you need?这是你需要的吗?
var winPos; $('.searchMe').keypress(function () { winPos = $(window).scrollTop(); $('html').addClass('modal-open').css('top', - winPos + 'px'); $('#modal').addClass('open'); }); $('#modal, .close').click(function () { $('#modal').removeClass('open'); $('html').removeClass('modal-open').removeAttr('style'); $(window).scrollTop(winPos); }); $('.modal-content').click(function (event) { event.stopPropagation(); });
html { height: 100%; max-height: 100%; } html.modal-open { position: fixed; width: 100%; overflow-x: hidden; overflow-y: auto; z-index: 1; padding-right: 17px; } #modal.open { display: block; } #modal { display: none; background-color: rgba(0, 0, 0, 0.4); position: fixed; left: 0; right: 0; top: 0; bottom: 0; overflow-y: auto; overflow-x: hidden; z-index: 2; -webkit-overflow-scrolling: touch; } .modal-content { background-color: #fff; height: 100px; width: 300px; margin: 50px auto; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="search"> <input type="text" placeholder="search here" class="searchMe"> </div> <div class="result"> <ul></ul> </div> <div class="content"> Many items will be place here </div> <div id="modal"> <div class="modal-content"> modal content </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.