在做微信開發的時候遇到這個問題:微信瀏覽器彈出框滑動時頁面跟着滑動。
我覺得這個問題用的是下面這幾行代碼:
var $body = $('body'),
dialogIsInView = !1,//當前是不是對話框
lastContentContainerScrollTop = -1,//用於彈出框禁止內容滾動
$contentContainer = $('#content-container');//內容容器
//阻止Window滾動
function stopWindowScroll() {
dialogIsInView = true;
//禁止頁面滾動
lastContentContainerScrollTop = $body.scrollTop();
$contentContainer.addClass('overflow-hidden').css({
'height': $window.height(),
'margin-top': -lastContentContainerScrollTop
});
}
//恢復Window滾動
function revertWindowScroll() {
dialogIsInView = !1;
//恢復頁面滾動
$contentContainer.css({
'height': 'auto',
'margin-top': 0
}).removeClass('overflow-hidden');
$body.scrollTop(lastContentContainerScrollTop);
}
這裏有個要求,內容跟元素是 id=”content-container”.
題外話:
微信瀏覽器在Android和ios中的表現形式是不一樣的,歸根結底是Android端用的QQ瀏覽器內核X5,IOS端用的蘋果開放的瀏覽器內核webkit。