简体   繁体   English

Twitter Bootstrap Modal滚动显示页面

[英]Twitter Bootstrap Modal scrolling the page up on show

Clicking on a link which triggers a Twitter Bootstrap Modal to pop up is causing the browser to scroll up the page. 单击触发弹出Twitter Bootstrap Modal的链接会导致浏览器向上滚动页面。 This behavior is only occurring in Chrome. 此行为仅在Chrome中发生。

Here is a demonstration page: 这是一个演示页面:

http://www.turizmburosu.com/test2.aspx http://www.turizmburosu.com/test2.aspx

The page has 600 rows of links, causing the page body to exceed window height. 该页面具有600行链接,导致页面正文超过窗口高度。 If any of these links is clicked, it displays the modal dialog. 如果单击这些链接中的任何一个,它将显示模式对话框。 When in Chrome, clicking any of the links below the initial view will cause the background page to scroll up (not always entirely to the top). 在Chrome中,单击初始视图下方的任何链接将导致背景页面向上滚动(并不总是完全滚动到顶部)。

I am use the following function to trigger the display of the Modal: 我正在使用以下功能来触发模态的显示:

function test(id, imp) {
    $("#modalLabel").html("Header");
    $("#modalBody").html("Body");
    $("#myModal").modal("show");
}

and the links are of the form: 链接的形式为:

<a href="" onclick="test();return false;">Test Link ###</a>

I am seeing this behavior in Chrome 22.0.1229.94 m . 我在Chrome 22.0.1229.94 m中看到了这种现象

Any suggestions/ideas as to why this is happening and how to prevent it? 关于为什么发生这种情况以及如何预防的任何建议/想法?


Additional Example 附加示例

This can be recreated on the Twitter Bootstrap documentation page for the Modal plugin . 这可以在Modal插件Twitter Bootstrap文档页面上重新创建。

Once on the page, simply override the existing data-api to use the JavaScript api instead: 进入页面后,只需覆盖现有的data-api即可使用JavaScript api:

$('#modals a[data-toggle="modal"]').on('click',
  function(e) {
    $('#myModal').modal('toggle');
    return false
  });

Now, if the Launch Demo Modal button is clicked, the page will scroll up when the modal is shown. 现在,如果单击“ 启动演示模态”按钮,则显示模态时页面将向上滚动。

Again, this is using Chrome. 同样,这是使用Chrome。

Fixed (10/29/2012) 固定(10/29/2012)

This issue has been fixed since Bootstrap v2.2.0. 自Bootstrap v2.2.0起已解决此问题。 See commit e24b46... for details. 有关详细信息,请参见commit e24b46...。


(original answer) (原始答案)

This is due to a bug in the implementation of the Twitter Bootstrap Modal (as of 2.1.1). 这是由于Twitter Bootstrap Modal(从2.1.1版本开始)的实现中存在错误。 The root of the problem is that the focus is set to the modal prior to it completing its transition. 问题的根源在于在完成过渡之前将焦点设置为模态。 This will only occur under the condition that 这只会在以下情况下发生

  1. the browser supports CSS transitions (and you are using bootstrap-transition.js ); 浏览器支持CSS过渡(并且您正在使用bootstrap-transition.js );
  2. the modal has the class fade ; 情态有阶级fade ; and
  3. when focus is set to an element outside the current view, the browser will scroll to move it into view (eg, Chrome, Safari). 当焦点设置为当前视图之外的元素时,浏览器将滚动以将其移入视图(例如Chrome,Safari)。

It should be noted that this affects both the Data API (markup-based) and the Programmatic API (JS-based). 应当注意,这会影响数据API(基于标记)和编程API(基于JS)。 However, the reason it is more noticeable when taking the programmatic approach is because, unlike the Data API, it does not automatically restore focus to the triggering element, and, hence, does not scroll the view back down when the modal is hidden. 但是,采用编程方法时更引人注意的原因是,与Data API不同,它不会自动将焦点恢复到触发元素,因此在隐藏模式时不会将视图向下滚动。

More info is available on the Twitter Bootstrap repo under Issue #5336: Modal Scrolls Background Content . 有关更多信息,请参阅《 问题#5336:模态滚动背景内容》下的Twitter Bootstrap存储库。

A fix has been merged into the 2.1.2-wip branch , which should be up for release any day now. 修复程序已合并到2.1.2-wip分支中 ,该分支现在应该可以随时发布。

Here is a demo using the patch bootstrap-modal.js : 这是使用补丁bootstrap-modal.js的演示:

JSFiddle JSFiddle

Same problem. 同样的问题。 It is caused by "#" in and the anchor is doing to scroll. 这是由“#”引起的,并且锚点正在滚动。 I fixed that by deleting the anchor tag and place this: 我通过删除锚标记来解决此问题并将其放置:

<span onclick="$('#umisteni_modal').modal('show');" style="cursor:pointer;">Změnit místo dodání</span>

I had a similar issue on my site, which was caused by another jquery plugin (sidr). 我的网站上遇到了类似的问题,这是由另一个jquery插件(sidr)引起的。 When the sidebar was present and I opened a modal window, I would be sent back to top. 当出现侧边栏并打开一个模式窗口时,我将被发送回顶部。

In jquery.sidr.js I changed line 102-106 from: 在jquery.sidr.js中,我将行102-106更改为:

// Prepare page if container is body
if($body.is('body')){
scrollTop = $html.scrollTop();
$html.css('overflow-x', 'hidden').scrollTop(scrollTop);
}

To: 至:

// Prepare page if container is body
if($body.is('body') && !$('#modal').hasClass('in')){
scrollTop = $html.scrollTop();
$html.css('overflow-x', 'hidden').scrollTop(scrollTop);
}

If you are using this plugin along with twitter bootstrap's modal, maybe this can help you. 如果您将此插件与twitter bootstrap的模态一起使用,也许可以为您提供帮助。

I had the same problem because of these lines of CSS which were thought to permantly display scrollbars in the browser. 由于存在这些CSS行,我被认为会永久在浏览器中显示滚动条,因此我遇到了同样的问题。 Removing the two lines solved the problem: 删除两行即可解决问题:

html,
body {
    min-height: 100%;
    height: 101%;
}

to bring the focus back on close 使焦点重新集中

var triggerOffset=0;
$('.modal').on('show.bs.modal', function() {
    triggerOffset=$(window).scrollTop();
});
$('.modal').on('hidden.bs.modal', function() {
     var animationTime=1;
    $('html, body').animate({
        scrollTop: (triggerOffset)
    }, animationTime);
});

I had been having this issue for so long but my solution was to remove href from the anchor tag 我这个问题已经存在了很长时间,但是我的解决方案是从锚标记中删除href

ie

<a onclick="test();return false;">Test Link ###</a>

I get no jump now 我现在没有跳

Hope this helps someone in the future 希望这对以后的人有帮助

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

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