jquery插件封装:弹出iframe模式框

  在web开发过程中我们经常需要弹出Iframe页面来加载另外一个页面来显示操作或显示数据,在关闭iframe后又是需要执行某一个回调函数。以下插件能够以较少的代码实现该功能并具有较好的扩展性。下面先看截图:
jquery插件封装:弹出iframe模式框_第1张图片

使用该插件只需要一行代码即可:
引入jquery库和popWin.js文件。
(1)只用iframe加载另一个页面

popWin.showWin("http://www.csdn.net",1000,600,function(){alert("关闭并执行回调函数")});

(2)用iframe加载另一个页面,关闭后执行回调函数。

popWin.showWin("http://www.csdn.net",1000,600,function(){alert("关闭并执行回调函数")}); 

Demo列表


<html>
  <head>
    <title>弹出iframe模式框(无标题栏)title>
    <meta charset="UTF-8">
    <script type="text/javascript" src="js/jquery-1.9.1/jquery-1.9.1.min.js">script>
    <script type="text/javascript" src="js/popwin.js">script>
    <script type="text/javascript">
        function openWin(){
            popWin.showWin("http://www.csdn.net",1000,600,function(){
                alert("关闭并执行回调函数");
            });
        }
    script>
  head>
  <body style="height:900px;">
      <div style="top:100px;">
      <input type="button" value="点击弹出iframe" onclick="openWin();" />
      div>
  body>
html>

popWin.js


/*
 *Haiwi@20150329
 *弹出iframe模式框(无标题栏)
 *调用方法:
 *(1)关闭时候无回调函数
 *      popWin.showWin("http://www.csdn.net",1000,600);
 *(2)关闭后时候执行回调函数
 *      popWin.showWin("http://www.csdn.net",1000,600,function(){alert("关闭并执行回调函数")}); 
 */
var popWin = {
    scrolling: 'auto',//是否显示滚动条 no,yes,auto
    closeCallBack:null,
    init: function() {
        this.mouseClose();
        this.closeMask();
    },
    showWin: function(src, width, height, callBackBeforeClose) {
        if(typeof callBackBeforeClose!=undefined && callBackBeforeClose!=null){
            closeCallBack=callBackBeforeClose;
        }
        var titleDivHeight=0;
        var iframeHeight = height - titleDivHeight;
        var marginLeft = width / 2;
        var marginTop = height / 2;
        var inntHtml = '';
        inntHtml += '
'
inntHtml += '
'px; height: ' + height + 'px; border: #999999 1px solid; background: #fff; color: #333; position: fixed; top: 50%; left: 50%; margin-left: -' + marginLeft + 'px; margin-top: -' + marginTop + 'px; z-index: 2999; filter: progid:DXImageTransform.Microsoft.Shadow(color=#909090,direction=120,strength=4); -moz-box-shadow: 2px 2px 10px #909090; -webkit-box-shadow: 2px 2px 10px #909090; box-shadow: 2px 2px 10px #909090;">' inntHtml += '
'px; line-height: 50px; font-family: Microsoft Yahei; font-size: 20px; color: #333333; padding-left: 20px; background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAyCAYAAABlG0p9AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAABvSURBVEhL1cq5DcAwDENR7T+sL9lOOoUbkCoCwwKewOJbiGe+31BkwgeDM18YgrPhxuBs4CkS4cQQZMKFwd0R+gzFJaFjcD+EfXgoMuHA4O4Iew/FJWHD4BJhwxDoYcNTIKwY3NGwYggQFgxODEt8xO1/6P+HHxEAAAAASUVORK5CYII=); border-bottom: 1px solid #999999; position: relative;">' inntHtml += '' inntHtml += '
'
inntHtml += '
'
inntHtml += ''; inntHtml += '
'
$("body").append(inntHtml); this.init(); }, mouseClose: function() { $("#popWinClose").on('mouseenter', function() { $(this).css("background-image", "url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAYAAAByDd+UAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAJwSURBVEhLvZbLSiNBFIb7DVyKiIgb17oQRRAXgor6CIIIeQKXMksfxYUbFbMZRh0Yb6ODMgEddCVmoWkRLzFekukxfay/+lRbqSqTVob+4CyqzuVPV59TaS8JYRhmhM0Ly5MB9tiX4fDPIQq0CpsT9sC1G4JYzmnlMskQCRPCrrnOh0EuanC5+ojAL5wXc5/LUW5qitba2ynreTWGPfgQY4JaXNaNKfZ0dkY7g4OWyHuGWOTovCuKI+AYib+8TF+bmpyF6xlykKuD2iwTITbQIPE7Q4Kr2EdMF0VtaLCcFJxjnzySzzyZaaihHy80WE4Kxq3vemcns7PStzsyYvn+zMxQUCzSRne35UMtBTSUWIb3ZKeZSRCrBoH0lwsF2u7vj32/JyepWi5L3/3hIW319dXkwvTuhRYE53kt29tMMAlub2lvdJRy09MUVqu8G3GxsGDlo6YCWhCMryvXnO0OD1PF9zkiQj5VGPIqonhwQOsdHVY+aiqgVfMIZrCy7YEBCm5uOMqmdHTkFFOmk0gQ9nNoiF4eHznyjed8nr41NztzlOkkFsQ7cwmWz89ps6fHmaNMJ5Gg7MZKhaNs/pVK8thduTCdhk2DOVNjoXg6PaW/V1e8ikBj7Y2NWflW06BVee0cC/x6nYfjY/nOfnR1yRHRucxmrXzXWNQdfNwgGGpwt79Pa21tsQ+XAC4D4K+s0GpLS00uzBp8vm3qXm1bvb1UWFyk752dlu/X+Dj5S0vOTnVebUAsUr+80/17AmIjvT9ghXCk94mhMEUBOg3t7ZpT7MGnd6OioZgCRyAsnc9EhUhI70PYRBT4T5/6nvcKYG1hElXAZggAAAAASUVORK5CYII=)"); }); $("#popWinClose").on('mouseleave', function() { $(this).css("background-image", "url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAYAAAByDd+UAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAJeSURBVEhLvZbPq2lRFMf9B4bSTTIxZiBSMlCI9ycoKX+Bod7w/il3YIL4NyhFmYmBKD2Sp0ix3vqes/e529n74t33Op9astevr3PO2tvxvcLtdquzfbAtyAV8IlYX6d+DG7yxvbP9Fr2fglxR8ybavAYX/GD7Jfr8NahFD9HuMZz4U9Q5jEYjqlarFA6HiVPuDD7EkOMGvTjna9xi8/mcstmsJvKVIRc1Kl+K4haIHItut0t+v9/Y+JGhBrUq6M2xT9iBAXGeGQrY/U+miqI3NNhvw4t3EbNuyXeuzG3ood5eaLDfhhfO6JueWbPZtGKFQkGLNRoN2u/3FI/HtRh6SaDBPkusLnzWpMlkaRC7XC5WfLVaUTqddmKVSoVOp5MVG4/HlEql7mph6vRCC4IfYm2Nt7vAzW63o2KxSLVaja7Xq/DatFotrR49JdCCoHNcmfZZPp+n9XotMmxwVVwnVjbD4ZAikYhWj54SaN1dgjtZWiaToe12K7J0JpOJUUyaykuCsFwuR8fjUWR+slgsKBAIGGukqbwsiGdmElwul5RIJIw10lReEsQ0ns9nkaVzOBys226qhak8HRrsM7ktJLPZjDabjVjZYLBKpZJWrw0NfzzcFvj1KtPp1HpmsVjM2iIq/X5fqzdti4cbHycINjUYDAYUCoWcGA4BHAag1+tRMBi8q4VpGx/wl4dHWzKZpHa7TdFoVIuVy2XqdDrGSTUebYAXnh/e3v49AXZ49wcs4YB3rxgStyjApGG8TfsUPsTUaZQ8FZPgFrB585oo4QLvXoTdcIP/9Krv8/0BDUSOirKWU6wAAAAASUVORK5CYII=)"); }); }, closeMask: function() { $("#popWinClose").on('click', function() { $("#mask,#maskTop").fadeOut(function() { $(this).remove(); }); if(typeof closeCallBack!=undefined && closeCallBack!=null){ closeCallBack(); } }); } };

点击查看源码:源码

你可能感兴趣的:(前端)