使用jQuery,我想禁用主体的滚动:
我的想法是:
- 组
body{ overflow: hidden;} - 捕获当前
scrollTop();/scrollLeft() - 绑定到主体滚动事件,将scrollTop / scrollLeft设置为捕获的值。
有没有更好的办法?
更新:
请在http://jsbin.com/ikuma4/2/edit上查看我的示例及其原因
我知道有人会想“为什么他不只是position: fixed在面板上使用?”。
请不要提出这个建议,因为我还有其他原因。
使用jQuery,我想禁用主体的滚动:
我的想法是:
body{ overflow: hidden;} scrollTop();/scrollLeft() 有没有更好的办法?
更新:
请在http://jsbin.com/ikuma4/2/edit上查看我的示例及其原因
我知道有人会想“为什么他不只是position: fixed在面板上使用?”。
请不要提出这个建议,因为我还有其他原因。
Answers:
我发现执行此操作的唯一方法与您所描述的类似:
然后,当您准备允许再次滚动时,请撤消所有操作。
编辑:自从我费劲去挖掘它以来,没有理由我不能给你代码...
// lock scroll position, but retain settings for later
var scrollPosition = [
self.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft,
self.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
];
var html = jQuery('html'); // it would make more sense to apply this to body, but IE7 won't have that
html.data('scroll-position', scrollPosition);
html.data('previous-overflow', html.css('overflow'));
html.css('overflow', 'hidden');
window.scrollTo(scrollPosition[0], scrollPosition[1]);
// un-lock scroll position
var html = jQuery('html');
var scrollPosition = html.data('scroll-position');
html.css('overflow', html.data('previous-overflow'));
window.scrollTo(scrollPosition[0], scrollPosition[1])
auto,它将被覆盖。我需要保留现有设置,这样也增加了一些开销。
html和body搭配overflow: hidden不够?我们仍然最终需要事件处理程序。
这将完全禁用滚动:
$('html, body').css({
overflow: 'hidden',
height: '100%'
});
恢复:
$('html, body').css({
overflow: 'auto',
height: 'auto'
});
在Firefox和Chrome上进行了测试。
height: 100%有效地将滚动锁定在当前位置而不会跳转-至少在最新的Chrome上。
试试这个
$('#element').on('scroll touchmove mousewheel', function(e){
e.preventDefault();
e.stopPropagation();
return false;
})
$('#element').off('scroll touchmove mousewheel');
body滚动,而又允许在具有子元素的其他子元素中滚动overflow: scroll?
我只是通过tfe对解决方案进行了一些调整。特别是,我添加了一些其他控件,以确保在滚动条设置为时,页面内容不会发生移位(aka 页面移位)hidden。
两个Javascript函数lockScroll()和unlockScroll()可以被定义,分别锁定和解锁页面滚动。
function lockScroll(){
$html = $('html');
$body = $('body');
var initWidth = $body.outerWidth();
var initHeight = $body.outerHeight();
var scrollPosition = [
self.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft,
self.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
];
$html.data('scroll-position', scrollPosition);
$html.data('previous-overflow', $html.css('overflow'));
$html.css('overflow', 'hidden');
window.scrollTo(scrollPosition[0], scrollPosition[1]);
var marginR = $body.outerWidth()-initWidth;
var marginB = $body.outerHeight()-initHeight;
$body.css({'margin-right': marginR,'margin-bottom': marginB});
}
function unlockScroll(){
$html = $('html');
$body = $('body');
$html.css('overflow', $html.data('previous-overflow'));
var scrollPosition = $html.data('scroll-position');
window.scrollTo(scrollPosition[0], scrollPosition[1]);
$body.css({'margin-right': 0, 'margin-bottom': 0});
}
我以为<body>没有初始保证金。
请注意,尽管上述解决方案在大多数实际情况下都可以使用,但由于它需要对页面进行一些进一步的自定义(例如,带有的标头),因此尚不确定position:fixed。让我们通过一个例子来研究这种特殊情况。假设有
<body>
<div id="header">My fixedheader</div>
<!--- OTHER CONTENT -->
</body>
与
#header{position:fixed; padding:0; margin:0; width:100%}
然后,应该在函数lockScroll()和中添加以下内容unlockScroll():
function lockScroll(){
//Omissis
$('#header').css('margin-right', marginR);
}
function unlockScroll(){
//Omissis
$('#header').css('margin-right', 0);
}
最后,注意边距或填充的一些可能的初始值。
marginRight和marginLeft:)
您可以使用以下代码:
$("body").css("overflow", "hidden");
我已经编写了一个jQuery插件来处理此问题:$ .disablescroll。
它防止滚动鼠标滚轮,触摸移动和按键事件,例如Page Down。
这里有一个演示。
用法:
$(window).disablescroll();
// To re-enable scrolling:
$(window).disablescroll("undo");
您可以附加一个功能来滚动事件并阻止其默认行为。
var $window = $(window);
$window.on("mousewheel DOMMouseScroll", onMouseWheel);
function onMouseWheel(e) {
e.preventDefault();
}
一个衬里可禁用滚动,包括鼠标中键。
$(document).scroll(function () { $(document).scrollTop(0); });
编辑:无论如何都不需要jQuery,下面与香草JS中的上面相同(这意味着没有框架,只有JavaScript):
document.addEventListener('scroll', function () { this.documentElement.scrollTop = 0; this.body.scrollTop = 0; })
this.documentElement.scrollTop-标准
this.body.scrollTop-IE兼容性
$("body").css("overflow", "hidden");$("body").css("overflow", "initial");您不能只将身高设置为100%并隐藏溢出吗?参见http://jsbin.com/ikuma4/13/edit
这可能无法实现您的目的,但是您可以扩展jScrollPane以在滚动之前触发其他功能。我只测试了一下,但可以确认您可以进入并完全阻止滚动。我所做的就是:
<script type="text/javascript" src="script/jquery.jscrollpane.js"></script>positionDragY(destY, animate)功能的第一行启动events.html,您将看到一个正常的滚动框,由于您的编码干预,该滚动框不会滚动。
您可以通过这种方式控制整个浏览器的滚动条(请参阅fullpage_scroll.html)。
因此,大概下一步是添加一个对其他函数的调用,该函数将关闭并执行锚定魔术,然后确定是否继续滚动。您还可以通过API调用来设置scrollTop和scrollLeft。
如果您需要更多帮助,请发布您的建议!
希望这有所帮助。
我在这里提出了一个可能有用的答案: jQuery simplemodal禁用滚动
它显示了如何在不移动文本的情况下关闭滚动条。您可以忽略有关simplemodal的部分。
您可以使用可滚动的div覆盖窗口,以防止滚动页面上的内容。并且,通过隐藏和显示,您可以锁定/解锁滚动。
做这样的事情:
#scrollLock {
width: 100%;
height: 100%;
position: fixed;
overflow: scroll;
opacity: 0;
display:none
}
#scrollLock > div {
height: 99999px;
}
function scrollLock(){
$('#scrollLock').scrollTop('10000').show();
}
function scrollUnlock(){
$('#scrollLock').hide();
}
对于布局居中(通过margin:0 auto;)的人们,这里是该position:fixed解决方案与@tfe提出的解决方案的混搭。
如果遇到页面捕捉(由于滚动条显示/隐藏),请使用此解决方案。
// lock scroll position, but retain settings for later
var scrollPosition = [
window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft,
window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
];
var $html = $('html'); // bow to the demon known as MSIE(v7)
$html.addClass('modal-noscroll');
$html.data('scroll-position', scrollPosition);
$html.data('margin-top', $html.css('margin-top'));
$html.css('margin-top', -1 * scrollPosition[1]);
…结合…
// un-lock scroll position
var $html = $('html').removeClass('modal-noscroll');
var scrollPosition = $html.data('scroll-position');
var marginTop = $html.data('margin-top');
$html.css('margin-top', marginTop);
window.scrollTo(scrollPosition[0], scrollPosition[1])
……最后是.modal-noscroll…… 的CSS
.modal-noscroll
{
position: fixed;
overflow-y: scroll;
width: 100%;
}
我敢说这比其他解决方案更合适,但是我还没有对它进行彻底的测试……:P
编辑:请注意,我不知道这可能会在触摸设备上执行(读取:爆炸)的效果。
这就是我最终要做的事情:
CoffeeScript:
$("input").focus ->
$("html, body").css "overflow-y","hidden"
$(document).on "scroll.stopped touchmove.stopped mousewheel.stopped", (event) ->
event.preventDefault()
$("input").blur ->
$("html, body").css "overflow-y","auto"
$(document).off "scroll.stopped touchmove.stopped mousewheel.stopped"
Javascript:
$("input").focus(function() {
$("html, body").css("overflow-y", "hidden");
$(document).on("scroll.stopped touchmove.stopped mousewheel.stopped", function(event) {
return event.preventDefault();
});
});
$("input").blur(function() {
$("html, body").css("overflow-y", "auto");
$(document).off("scroll.stopped touchmove.stopped mousewheel.stopped");
});
不知道是否有人尝试过我的解决方案。这适用于整个body / html,但毫无疑问,它可以应用于触发滚动事件的任何元素。
只需根据需要设置和取消设置scrollLock。
var scrollLock = false;
var scrollMem = {left: 0, top: 0};
$(window).scroll(function(){
if (scrollLock) {
window.scrollTo(scrollMem.left, scrollMem.top);
} else {
scrollMem = {
left: self.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft,
top: self.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
};
}
});
这是示例JSFiddle
希望这个可以帮助到别人。