jQuery前端架構easyui使用Dialog時bug處理,jqueryeasyui

來源:互聯網
上載者:User

jQuery前端架構easyui使用Dialog時bug處理,jqueryeasyui

最近一直都在用easyui前端架構來開發設計UI,但在使用Dialog時,發現如果頁面內容比較多,就會出現問題,首先看一下我的原代碼:

複製代碼 代碼如下:
 <input type="button" value="確認預約" id="btnconfirm" onclick="javascript:openconfirmDlg();" />
    <div id="confirmd"> 
        <p>請選擇確認結果:</p>
        <p><input type="radio" value="True" id="rtrue" name="rresult" class="rresult" /><label for="rtrue">成功</label>
             
        <input type="radio" value="False" id="rfalse" name="rresult" class="rresult" /><label for="rfalse">失敗</label></p>
    </div>
     <script type="text/javascript">
         $("#confirmd").dialog({
             title: '預約確認',
             iconCls: 'icon-save', resizable: false, modal: true, closed: true,
             width: 200, height: 200,
             buttons: [{ text: '提 交', handler: function () {
                 alert("ok");
             }
             }, { text: '取 消', handler: function () {
                 $("#confirmd").dialog("close");
             }
             }]
         });
     function openconfirmDlg() {
         $("#confirmd").dialog("open");
     }
     </script>

當點擊【確認預約】按鈕時,開啟對話方塊,效果如下:

可以看到幾個問題,一是遮罩層沒有全部蓋住網頁內容,二是對話方塊不見了,當然不是真的不見了,而是顯示到了頁面的上方,需要將捲軸拖回到項端方可見到,造成這樣的原因很清楚,一是擷取網頁內容高度不正確,只是得到了window的高度(即可視高度),才會出現遮罩不完整,二是定位不正確,未能正確識別到scrollTop,造成對話方塊定位不準,針對這些問題,我做出了相應的改進,從而解決了該問題,下面是改進後的代碼:

複製代碼 代碼如下:
    <input type="button" value="確認預約" id="btnconfirm" onclick="javascript:openconfirmDlg();" />
    <div id="confirmd"> 
        <p>請選擇確認結果:</p>
        <p><input type="radio" value="True" id="rtrue" name="rresult" class="rresult" /><label for="rtrue">成功</label>
             
        <input type="radio" value="False" id="rfalse" name="rresult" class="rresult" /><label for="rfalse">失敗</label></p>
    </div>
     <script type="text/javascript">
         $("#confirmd").dialog({
             title: '預約確認',
             iconCls: 'icon-save', resizable: false, modal: true, closed: true,
             width: 200, height: 200,
             buttons: [{ text: '提 交', handler: function () {
                 alert("ok");
             }
             }, { text: '取 消', handler: function () {
                 $("#confirmd").dialog("close");
             }
             }]
         });
     window.onscroll = function () {
         $("#confirmd").dialog("move", { top: $(document).scrollTop() + ($(window).height() - 200) * 0.5 });
     }
     function openconfirmDlg() {
         $("#confirmd").dialog("open");
         $("#confirmd").dialog("move", { top: $(document).scrollTop() + ($(window).height() - 200) * 0.5 });
         $(".window-mask").css({ height: $(document).height()});
     }
     </script>

現在開啟對話方塊就正常了,效果如下:

即使滾動也能始終處在網頁中間,效果如下:

確保如上效果的關鍵代碼是:

複製代碼 代碼如下:
         $("#confirmd").dialog("move", { top: $(document).scrollTop() + ($(window).height() - 200) * 0.5 }); //移動到當前內容頁面的中間
         $(".window-mask").css({ height: $(document).height()}); //調整遮罩層的高度為網頁內容高度

大家測試下,是不是比之前的好用多了,本人測試了大多數瀏覽器都沒有問題,如果有遺漏的,還請留言告之,本代碼持續更新。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.