How to refresh Fancybox content when closed?

 ̄綄美尐妖づ 提交于 2019-12-11 15:56:46

问题


Is there a way to reset the fancybox instance or refresh the contents of it, when closed? Note that it grabs content from a div on the same page.

I want it to display the content like it was first loaded into the dom, when re-opening the fancybox.

I guess this is more of a javascript/jQuery thing, than a Fancybox thing, right?

$.fancybox.open({
    href: '#popup',
    padding: 0,
    autoWidth: true,
    arrows: false,
    closeBtn: false,
    scrollOutside: true,
    helpers: {
        overlay: {
            css: {
                'background': 'rgba(0, 0, 0, 0.5)'
            },
            locked: false
        }
    },
    afterClose: function() {
        // Reset or refresh here
    }
});

回答1:


It depends on the sort of changes you are making to the target div, but you will not be able to "reset" them without reloading the page, or performing a funky Ajax call to the page and getting the div contents from there.

The simplest way of getting around this would be to store the markup of that div in a variable (using jQuery's html(), and "reset" that div using the variable in your afterClose() callback.

JS

var content = $('#popup').html(); // store content on documentReady()

$.fancybox.open({
    href: '#popup',
    padding: 0,
    autoWidth: true,
    arrows: false,
    closeBtn: false,
    scrollOutside: true,
    helpers: {
        overlay: {
            css: {
                'background': 'rgba(0, 0, 0, 0.5)'
            },
            locked: false
        }
    },
    afterClose: function(){
        $('#popup').html(content);
    }
});    

It might be best to use fancybox's beforeLoad callback, in conjunction with this, to do this for all of your content, but as you seem to be loading a single div, this should work.



来源:https://stackoverflow.com/questions/16444962/how-to-refresh-fancybox-content-when-closed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!