简体   繁体   English

JQuery 滚动到 Bootstrap Modal 的顶部

[英]JQuery scroll to top of Bootstrap Modal

I'm currently using the bootstrap modal plugin to display long legal messages on a website I'm designing, but the problem is that if you open one modal after the other, the second one will already be scrolled to whatever position the first one was.我目前正在使用 bootstrap modal 插件在我正在设计的网站上显示长长的法律消息,但问题是如果你一个接一个地打开一个模态,第二个将已经滚动到第一个的任何位置. So I'm looking for a way to scroll a div to the top with JS/JQuery.所以我正在寻找一种使用 JS/JQuery 将 div 滚动到顶部的方法。 This is the code I'm using currently:这是我目前使用的代码:

HTML: HTML:

<!--MODAL-->
<div id="modal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="modalTitle" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="modalTitle"></h3>
</div>
<div id="modal-content" class="modal-body">
</div>
<div class="modal-footer">
<button id="modalPrint" class="btn btn-info hidden-phone" onClick=''>Print</button>
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
</div>
</div>

Javascript: Javascript:

function openModal(heading, url) {
    $.ajax({
    url: url,
    cache: false
    }).done(function( html ) {
    $("#modal-content").html(html);
    $("#modalTitle").html(heading);
    $('#modalPrint').attr('onClick', 'loadPrintDocument(printCookies, {attr : "href", url : '+ url +', showMessage: false, message: "Please wait while we create your document"})');
    $('#modal').modal('show');
    $("#modal-content").scrollTop(0);
    });
}

As you can see, I've tried scrolling to the top after the modal has been shown.如您所见,我尝试在显示模态后滚动到顶部。 Any ideas?有什么想法吗?

Now with bootstrap 3 the events has change an can be achieved like this (plus a smooth animation to the top)现在使用 bootstrap 3,事件发生了变化,可以像这样实现(加上顶部的平滑动画)

$('#modal').on('shown.bs.modal', function () {
    $('#modal').animate({ scrollTop: 0 }, 'slow');
});

Okay I've answered my own question.好的,我已经回答了我自己的问题。 For anyone else with this issue, simply add this function to your JS!对于遇到此问题的其他人,只需将此功能添加到您的 JS 中即可!

$('#modal').on('shown', function () {
    $("#modal-content").scrollTop(0);
});

I will leave this question up, as there isn't one similar (that I could find) and it may help someone我会留下这个问题,因为没有类似的(我能找到的)并且它可能对某人有所帮助

On Bootstrap 4 (v4.0.0-alpha.6) the following worked well for me:在 Bootstrap 4 (v4.0.0-alpha.6) 上,以下对我来说效果很好:

$('#Modal').show().scrollTop(0);

Please note as of bootstrap-4.0.0-beta.1 and Firefox 56.0.1 this does not seem to work correctly;请注意,从bootstrap-4.0.0-beta.1Firefox 56.0.1 开始,这似乎无法正常工作;

I checked IE11, MS Edge and Chrome and this works fine.我检查了 IE11、MS Edge 和 Chrome,这很好用。

  1. Scroll up button has a class of: .page-scroll向上滚动按钮有一个类: .page-scroll

在此处输入图片说明

  1. Modal with a class of: .modal具有以下类别的模态: .modal

  2. JS to make scroll happen with the modal (JS could be simplified): JS 使滚动发生与模态(JS 可以简化):

     $('body').on('click', '.page-scroll', function(event) { var $anchor = $(this); $('html, body, .modal').stop().animate({ scrollTop: $($anchor.attr('href')).offset().top }, 1500, 'easeInOutExpo'); event.preventDefault(); });

In Bootstrap 4, the following works for me:在 Bootstrap 4 中,以下对我有用:

$('.modal-body').scrollTop(0);

You need to scrollTop on the modal-body because the parent elements (modal, modal-dialog, modal-content) are containers and do not scroll with the user.您需要在模态主体上滚动顶部,因为父元素(模态、模态对话框、模态内容)是容器并且不会随用户滚动。

However, if you prefer a less jerky movement, try the animation route:但是,如果您更喜欢不那么生涩的运动,请尝试动画路线:

$('.modal-body').animate({scrollTop: 0},400);

There was change in boostrap where you need to use: on shown.bs.modal您需要使用的 boostrap 发生了变化:on shown.bs.modal

Call a function when you show the modal window (I opted for this method)当你显示模态窗口时调用一个函数(我选择了这个方法)

<button onclick="showSMSSummary(); return false;" 
data-toggle="tooltip" title="Click To View Summary" 
class="btn btn-primary btn-sm">SMS Summary</button>

function showSMSSummary()
{
   $('#HelpScreenModalContent').modal('show');            
   $('#HelpScreenModal').animate({ scrollTop: 0 }, 'fast');
}

ScrollTop bootbox modal on fadeIn The answer is in this issue. FadeIn 上的 ScrollTop bootbox modal答案就在这个问题上。

When setting up bootbox dialog add .off("shown.bs.modal");设置引导框对话框时添加.off("shown.bs.modal"); this at the end.这在最后。

bootbox.dialog({ ... }).off("shown.bs.modal");

It will scroll be at top when open dialog.打开对话框时它会滚动到顶部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM