jQuery UI effect causes iframe reload

2019-02-27 20:16发布

I'm using jQuery to show / hide a div which happens to contain an iframe. It works great with just standard 'show' and 'hide' methods.

So now I want to get a little fancy and add some effects from jQuery UI (http://jqueryui.com/effect/) but suddenly my iframes are getting reloaded every time I show / hide them.

Here is a fiddle to demonstrate: http://jsfiddle.net/BnZzk/1/ And here is the code since SO is forcing me to add it:

<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>

Any idea how I can keep the fancy effects but not refresh the content of my iframes?

2条回答
贪生不怕死
2楼-- · 2019-02-27 21:04

It's happening because this effect reorganizes the DOM, putting a DIV wrapper around the IFRAME, so when the IFRAME is "reappended" the reload happens! You can see this behavior using the Google Chrome elements inspector.

To solve I suggest you apply the effect in a parent DIV from your IFRAME but not using the effect plugin. Check out the http://api.jquery.com/animate/, manipulating the width and height style properties.

查看更多
Emotional °昔
3楼-- · 2019-02-27 21:10

As @Jamillo Santos's answer, 'reappend' issues of iFrame.

if you are using dialog widget or its extension of jQueryUI and want to prevent this situation, just redefine _moveToTop() function of your widget implementation as below.

_moveToTop: function() {
    return null;
},
查看更多
登录 后发表回答