Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Twitter bootstrap 3 如何在bootstrap3中重置modal的滚动位置?_Twitter Bootstrap 3_Bootstrap Modal - Fatal编程技术网

Twitter bootstrap 3 如何在bootstrap3中重置modal的滚动位置?

Twitter bootstrap 3 如何在bootstrap3中重置modal的滚动位置?,twitter-bootstrap-3,bootstrap-modal,Twitter Bootstrap 3,Bootstrap Modal,我有一个,就像文档中的一样 当我第一次打开模态(“启动演示模型”按钮)时,垂直滚动条位于顶部位置。如果我向下滚动并在模式中单击“取消”,它将关闭。没关系。但如果我再次打开模态,它将已经向下滚动。这是我想避免的情况。如何在顶部位置打开模式?我认为您需要使用打开模式时触发的事件来重置滚动 比如: $('#myModal').on('shown.bs.modal', function () { window.scrollTo(0,0); }); 根据您的页面,您可能希望使用一些jQuery或动画

我有一个,就像文档中的一样


当我第一次打开模态(“启动演示模型”按钮)时,垂直滚动条位于顶部位置。如果我向下滚动并在模式中单击“取消”,它将关闭。没关系。但如果我再次打开模态,它将已经向下滚动。这是我想避免的情况。如何在顶部位置打开模式?

我认为您需要使用打开模式时触发的事件来重置滚动

比如:

$('#myModal').on('shown.bs.modal', function () {
  window.scrollTo(0,0);
});
根据您的页面,您可能希望使用一些jQuery或动画:

$('#myModal').on('shown.bs.modal', function () {
 $('html, body').animate({
   scrollTop: $("#myModal").offset().top
 }, 1000);
});

我已通过在my modal的顶部输入元素上设置焦点来解决此问题:

    $("#RuleDialog").on('shown.bs.modal', function() {
        $('#LinkedRuleName').focus();
    });
在生产中工作 引导3模式

$modal.off('shown.bs.modal').on('shown.bs.modal', function() {
    if ($modal.scrollTop() > 0) {
        $modal.scrollTop(0)
    }
}

下面是我如何让这个功能工作的。首先设置显示的模态事件

$(document).ready(function () {
    var modalMatrix = $('#modalMatrix');
    modalMatrix.on('shown.bs.modal', function () {
        modalMatrix.find('.modal-body').scrollTop(0); // This is important , for me the scrollbar is in the body of the modal .
    });
});
然后我像往常一样显示模态

$('#modalMatrix').modal('show');

我已尝试使用“show.bs.modal”事件。它可以正确启动,但windows.scrollTo对模式没有影响。相反,它会滚动主窗口(在后台)。尽管这是一篇非常古老的文章,我在这里评论它可能对某人有所帮助。您必须将
.modal
添加到选择器列表中,因此它将不是`$('html,body')`而是`$('html,body,.modal')`或删除
html
body
。模态内容将滚动,因为它不是通用解决方案。正确的事件名称应为“show.bs.modal”或“hide.bs.modal”。我更喜欢后者。反正这只是“B”。)
$('#modalMatrix').modal('show');