【问题标题】:jQuery UI effect causes iframe reloadjQuery UI 效果导致 iframe 重新加载
【发布时间】:2013-02-13 22:53:31
【问题描述】:

我正在使用 jQuery 显示/隐藏恰好包含 iframe 的 div。它仅适用于标准的“显示”和“隐藏”方法。

所以现在我想从 jQuery UI (http://jqueryui.com/effect/) 中添加一些效果,但突然我的 iframe 每次显示/隐藏时都会重新加载。

这里有一个小提琴来演示:http://jsfiddle.net/BnZzk/1/ 这是代码,因为 SO 迫使我添加它:

<style>
div {
    height: 200px
}
span {
    display: block;
    padding-bottom: 10px;
    margin-bottom: 10px;
    border-bottom: 1px solid black;
}
</style>

<div>
    <iframe src="http://www.wikipedia.org/"></iframe>
</div>
<input type="button" value="Go"/>
<hr/>
<div id="msgs"></div>

<script>
$(function () {

var container = $('div'),
    ifrm = $('iframe'),
    msgs = $('#msgs')
    delay = 10; //change me to adjust delay in seconds between actions

$('input').on('click', normal);
log('First let the iframe load and then clear your network console -- click the "Go" button to get started.');

function log (msg) {
    msgs.append('<span>' + msg + '</span>');
}

function normal () {

    ifrm.
        hide(400, function () {

            log('That was a standard hide with no effect -- is your network console still empty? OK we\'ll pause for ' + delay + ' seconds and then do a standard show.');

            ifrm.
                delay(delay * 1000).
                show(400, function () {
                    log('That was a show with no effect -- is you network console *still* empty? Great! Let\'s try some effects.');
                    log('------------------------<br/>' +
                    '-- Begin Effects --<br/>' +
                    '------------------------<br/>');
                    withEffect();
                });

        }); //hide

} //normal

function withEffect () {

    log('We\'ll pause for another ' + delay + ' seconds -- get ready to watch your network console.');

    ifrm.
        delay(delay * 1000).
        hide('fold', {mode:'hide'}, 400, function () {

            log('That was a hide with effect -- is your network console flooded? Mine too :-( We\'ll wait ' + delay + ' seconds while you clear your console again.');

            ifrm.
                delay(delay * 1000).
                show('fold', {mode:'show'}, 400, function () {
                    log('That was a show with effect -- is your network console flooded again? Bummer ...');
                });

        }); //hide

} //withEffect

});
</<script>

知道如何保持精美效果但不刷新 iframe 的内容吗?

【问题讨论】:

  • 您有没有为此想出一个可以分享的解决方案?我相信这与我目前遇到的问题相同。
  • 是的,看起来我从来没有接受过正确的答案。我只是切换到直接使用动画以避免重新附加 iframe。

标签: javascript jquery jquery-ui iframe jquery-effects


【解决方案1】:

之所以发生,是因为这种效果重新组织了 DOM,在 IFRAME 周围放置了一个 DIV 包装器,因此当 IFRAME “重新附加”时,重新加载就会发生!您可以使用 Google Chrome 元素检查器查看此行为。

要解决这个问题,我建议您在 IFRAME 的父 DIV 中应用效果,但不要使用 effect 插件。查看http://api.jquery.com/animate/,操作宽度和高度样式属性。

【讨论】:

  • 我明白了。好吧,看起来效果已经在内部使用了“动画”。我只需要摆脱包装。感谢您为我指明正确的方向。
  • @Jamillo Santos,您能否请一些示例代码来防止将 div 变形器放在 iframe 周围?大概在jquery ui toggle方法的完整功能中,需要找到那个wrapper div,然后用jquery remnove。
【解决方案2】:

作为@Jamillo Santos 的回答,“重新附加”iFrame 的问题。

如果您正在使用 jQueryUI 的 dialog 小部件或其扩展 并希望防止这种情况, 只需重新定义小部件实现的 _moveToTop() 函数,如下所示。

_moveToTop: function() {
    return null;
},

【讨论】:

  • 我正在使用 jquery ui 核心 + 效果下载来使用 jquery ui 切换方法制作动画,并将切换方法传递给 jquery ui 效果。你知道我需要重新定义什么功能吗?我没有使用对话框小部件或扩展。
猜你喜欢
  • 2011-09-22
  • 2011-12-13
  • 1970-01-01
  • 1970-01-01
  • 2011-05-14
  • 2014-05-20
  • 1970-01-01
  • 2014-01-11
  • 1970-01-01
相关资源
最近更新 更多