首頁 web前端 js教程 用JQuery 实现的自定义对话框_jquery

用JQuery 实现的自定义对话框_jquery

May 16, 2016 pm 07:16 PM

JavaScript为我们提供几种对话框功能,但功能都十分有限;虽然在IE下可以通过模式窗体的方式创建更丰富的对话框功能,但毕竟只有IE得到支持。为了得到更丰富的自定义对话框功能,于是用JQuery编写了一个对话框插件;只需简单地引用相关JavaScript就能得到丰富的自定义对话框功能。
插件功能特点:
       允许通过CSS进行外观控制。
       可以任意把面页的元素作为对话框显示。
       当对话框激活时,对话框外的任何元素不能接受鼠标操作。
       对话框可以在工作区范围内任意移动,根据工作区大小自适应居中功能。
       并不需要编写复杂的JavaScript,通过简单HTML属性描述即可。
       经过测试插件可以在Firefox和IE下正常工作。
简单使用描述:
       引用JQuery和对话框插件文件:
       <script></script>
<script></script>   
       定义相关按钮为行:
       
       定义相关对话框显示的内容:
    


        
            
                
            
            
                
            
        

                    JQuery模式对话框插件好用吗?

                    
                    

    


下载例程和源码
复制代码 代码如下:

/* JQuery 模式对话框插件 
 * writer: FanJianHan (henryfan@msn.com) 
 * License: GPL (GPL-LICENSE.txt) licenses. 
 */ 
 //是否已初始化过对话框 
var MessageOninit = false; 
//记录body滚动条的x,y偏移量;显示内容的元素对象,显示内容元素对象的父对象 
var MessageBox_scrolltop,MessageBox_scrollleft,Messagebox_AC,MessageBox_PC; 
//对话框对象,对话框宽度,对话框高度 
var MessageBox_win,MessageBox_width,MessageBox_height; 
//对话框是否处于移动状态 
var MessageBox_Moving = false; 
//显示模式提示框 
function ShowMessageBox(option) 

    var container,iframe,enabled,enabledframe; 
    var height=400; 
    var width =400; 
    MessageBox_scrolltop =0; 
    MessageBox_scrollleft =0; 
    if(!MessageOninit) 
    { 
       CreateContainer(option); 
       MessageOninit = true; 
       $('#messagebox_close').click(function(){ 
             CloseMessageBox(); 
       }); 
       $(window).resize(function(){ 
            SetStyle(option); 
            SetEnabledStyle(); 
       }); 
       $(window).scroll(function(e){ 
            MessageBox_scrolltop =document.documentElement.scrollTop; 
            MessageBox_scrollleft = document.documentElement.scrollLeft; 
            SetEnabledStyle(); 
       }); 
       MessageBox_win = $("#messagebox_win"); 
       $('#messagebox_title').mousedown(handleMouseDown); 
       $('#messagebox_title').mouseup(handleMouseUp); 
       $('#messagebox_title').mousemove(handleMouseMove); 
       document.onmouseup = handleMouseUp; 

    } 

    if(option.height) 
        height = parseInt(option.height); 
    if(option.width) 
        width = parseInt(option.width); 
    MessageBox_height = height; 
    MessageBox_width = width; 
    Messagebox_AC = $('#'+option.control); 
    MessageBox_PC = Messagebox_AC.parent(); 
    Messagebox_AC.css('display',''); 
    enabled='

    enabledframe=''; 
    $('#messagebox_enabledframe').remove()_ 
    $('#messagebox_enabled').remove(); 
    $('#messagebox_title').html(option.title); 
    $('#messagebox_from').append(Messagebox_AC); 
    SetStyle(option); 
    $(document.body).append(enabledframe); 
    $(document.body).append(enabled); 
    SetEnabledStyle(); 
    $('#messagebox_win').fadeIn("slow"); 

    //创建对话框容器 
    function CreateContainer(option) 
    { 
        var html; 
        html='
'; 
        if(option.parent) 
        { 
            $('#' + option.parent).append(html); 
        } 
        else 
        { 
            $(document.body).append(html); 
        } 
    } 
    //设置显示时背景式样 
    function SetEnabledStyle() 
    { 
        var de,w,h,css,region; 
        region = GetDocumentRegion(); 
        css ={width:region.width+"px",height:region.height+"px", 
        left: MessageBox_scrollleft+'px',top: MessageBox_scrolltop +'px'} 
        GetOpacity(css); 
        $("#messagebox_enabled").css(css); 
        $("#messagebox_enabledframe").css(css); 
    } 
    //设置透明式样 
    function GetOpacity(css) 
    { 
        if(window.navigator.userAgent.indexOf('MSIE')>=1) 
        { 
            css.filter= 'progid:DXImageTransform.Microsoft.Alpha(opacity=30)'; 
        } 
        else 
        { 
            css.opacity= '0.3'; 
        }    
    } 
    //设置对话框试样 
    function SetStyle(option) 
    { 
        var region,css; 
        region = GetDocumentRegion(); 
        css ={width:MessageBox_width+'px',height:MessageBox_height+'px', 
        left: ((region.width - MessageBox_width)/2)+'px',top: ((region.height - MessageBox_height)/2)+'px'} 
        if(region.height         { 
            css.top=10+'px'; 
        } 
        else 
        { 
            css.top=((region.height - MessageBox_height)/2)+'px' 
        } 
        $('#messagebox_win').css(css); 
        css.top='0px'; 
        css.left='0px'; 
        $('#messagebox_table').css(css); 
        css.width='100%'; 
        css.height='16px'; 
        $('#messagebox_title_td').css(css); 
        css.height= height-46 +'px'; 
        $('#messagebox_body_td').css(css); 

    } 
    var down_x,down_y,cx,cy; 
    function handleMouseDown(e) 
    { 
            var evt = e || event; 
         down_x=evt.clientX; 
         down_y = evt.clientY; 
         cx =(parseInt(MessageBox_win.css('left'))|0); 
         cy = (parseInt(MessageBox_win.css('top'))|0) 
         MessageBox_Moving= true;3 
         document.documentElement.onselectstart = function(){return false}; 
         document.documentElement.ondrag = function(){return false}; 
         document.onmousemove = handleMouseMove; 
         $(document.body).append('
'); 
         $('#messagebox_move').css('width',MessageBox_win.css('width')); 
         $('#messagebox_move').css('height',MessageBox_win_u99 ?ss('height')); 
         $('#messagebox_move').css('left',MessageBox_win.css('left')); 
         $('#messagebox_move').css('top',MessageBox_win.css('top')); 

    } 
    function GetDocumentRegion() 
    { 
        var w,h,de; 
        de = document.documentElement; 
        w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth; 
        h = self.innerHeight || (de&&de.clientHeight)|| document.body.clientHeight; 
        return {height:h,width:w}; 
    } 
    function handleMouseMove(e) 
    { 
        var left,top,region; 
        if (MessageBox_Moving) 
        { 
            var evt = e || event; 
            left =evt.clientX+cx-down_x; 
            top = evt.clientY+cy-down_y; 
            region = GetDocumentRegion(); 
            if(left+ MessageBox_width > region.width) 
            { 
                left = region.width - 10- MessageBox_width; 
            } 
            if(top + MessageBox_height >region.height)3 
            { 
                top = region.height-10 - MessageBox_height; 
            } 
            if(left                 left =10; 
            if(top                 top =10; 
            var css ={left:left+'px',top:top+'px'} 
            $('#messagebox_move').css(css); 
        } 
    } 
    function handleMouseUp() 
    { 
        if(MessageBox_Moving) 
        { 
            MessageBox_win.css('width',$('#messagebox_move').css("width")); 
            MessageBox_win.css('height',$('#messagebox_move').css("height")); 
            MessageBox_win.css('left',$('#messagebox_move').css("left")); 
            MessageBox_win.css('top',$('#messagebox_move').css("top")); 
        } 
        MessageBox_Moving _u61 ? false; 
        document.onmousemove = null; 
       $('#messagebox_move').remove(); 
    } 


//关闭模式对话框 
function CloseMessageBox() 

    if(MessageOninit) 
    { 
       $('#messagebox_win').hide(); 
       $('#messagebox_enabled').remove(); 
       $('#messagebox_enabledframe').remove(); 
      Messagebox_AC.css('display','none'); 
       MessageBox_PC.append(Messagebox_AC); 
    } 
     document.documentElement.onselectstart = null; 
         document.documentElement.ondrag = null; 

$(document).ready(function(){ 
    $(document).find('[@showoption]').each(function(){ 
        var namevalue; 
        //虽然显示的元素id,显示宽度,显示高度,标题,对话框寄居的元素对象id 
        var option={control:'',width:'400',height:'400',title:'',parent:null}; 
        var properties = $(this).attr('showoption').split(';');  
        for(i=0;i        { 
            namevalue = properties[i].split(':'); 
            if(namevalue.length >1) 
            { 
                execute ="option." + namevalue[0] +'=\''+ namevalue[1]+'\';'; 
                eval(execute); 
            } 
        } 
        $(this).click(function(){ 
            ShowMessageBox(option); 
            document.body.focus(); 
        }); 
    });     
}); 
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

前端熱敏紙小票打印遇到亂碼問題怎麼辦? 前端熱敏紙小票打印遇到亂碼問題怎麼辦? Apr 04, 2025 pm 02:42 PM

前端熱敏紙小票打印的常見問題與解決方案在前端開發中,小票打印是一個常見的需求。然而,很多開發者在實...

神秘的JavaScript:它的作用以及為什麼重要 神秘的JavaScript:它的作用以及為什麼重要 Apr 09, 2025 am 12:07 AM

JavaScript是現代Web開發的基石,它的主要功能包括事件驅動編程、動態內容生成和異步編程。 1)事件驅動編程允許網頁根據用戶操作動態變化。 2)動態內容生成使得頁面內容可以根據條件調整。 3)異步編程確保用戶界面不被阻塞。 JavaScript廣泛應用於網頁交互、單頁面應用和服務器端開發,極大地提升了用戶體驗和跨平台開發的靈活性。

誰得到更多的Python或JavaScript? 誰得到更多的Python或JavaScript? Apr 04, 2025 am 12:09 AM

Python和JavaScript開發者的薪資沒有絕對的高低,具體取決於技能和行業需求。 1.Python在數據科學和機器學習領域可能薪資更高。 2.JavaScript在前端和全棧開發中需求大,薪資也可觀。 3.影響因素包括經驗、地理位置、公司規模和特定技能。

如何使用JavaScript將具有相同ID的數組元素合併到一個對像中? 如何使用JavaScript將具有相同ID的數組元素合併到一個對像中? Apr 04, 2025 pm 05:09 PM

如何在JavaScript中將具有相同ID的數組元素合併到一個對像中?在處理數據時,我們常常會遇到需要將具有相同ID�...

JavaScript難以學習嗎? JavaScript難以學習嗎? Apr 03, 2025 am 12:20 AM

學習JavaScript不難,但有挑戰。 1)理解基礎概念如變量、數據類型、函數等。 2)掌握異步編程,通過事件循環實現。 3)使用DOM操作和Promise處理異步請求。 4)避免常見錯誤,使用調試技巧。 5)優化性能,遵循最佳實踐。

如何實現視差滾動和元素動畫效果,像資生堂官網那樣?
或者:
怎樣才能像資生堂官網一樣,實現頁面滾動伴隨的動畫效果? 如何實現視差滾動和元素動畫效果,像資生堂官網那樣? 或者: 怎樣才能像資生堂官網一樣,實現頁面滾動伴隨的動畫效果? Apr 04, 2025 pm 05:36 PM

實現視差滾動和元素動畫效果的探討本文將探討如何實現類似資生堂官網(https://www.shiseido.co.jp/sb/wonderland/)中�...

JavaScript的演變:當前的趨勢和未來前景 JavaScript的演變:當前的趨勢和未來前景 Apr 10, 2025 am 09:33 AM

JavaScript的最新趨勢包括TypeScript的崛起、現代框架和庫的流行以及WebAssembly的應用。未來前景涵蓋更強大的類型系統、服務器端JavaScript的發展、人工智能和機器學習的擴展以及物聯網和邊緣計算的潛力。

console.log輸出結果差異:兩次調用為何不同? console.log輸出結果差異:兩次調用為何不同? Apr 04, 2025 pm 05:12 PM

深入探討console.log輸出差異的根源本文將分析一段代碼中console.log函數輸出結果的差異,並解釋其背後的原因。 �...

See all articles