web-dev-qa-db-ja.com

JQueryを使って簡単なポップアップを生成する方法

私はWebページをデザインしています。 mailというdivの内容をクリックしたときに、ラベルのemailとテキストボックスを含むポップアップウィンドウを表示する方法

213
Rajasekar

まずはCSS - これを微調整してください。

a.selected {
  background-color:#1F75CC;
  color:white;
  z-index:100;
}

.messagepop {
  background-color:#FFFFFF;
  border:1px solid #999999;
  cursor:default;
  display:none;
  margin-top: 15px;
  position:absolute;
  text-align:left;
  width:394px;
  z-index:50;
  padding: 25px 25px 20px;
}

label {
  display: block;
  margin-bottom: 3px;
  padding-left: 15px;
  text-indent: -15px;
}

.messagepop p, .messagepop.div {
  border-bottom: 1px solid #EFEFEF;
  margin: 8px 0;
  padding-bottom: 8px;
}

そしてJavaScript

function deselect(e) {
  $('.pop').slideFadeToggle(function() {
    e.removeClass('selected');
  });    
}

$(function() {
  $('#contact').on('click', function() {
    if($(this).hasClass('selected')) {
      deselect($(this));               
    } else {
      $(this).addClass('selected');
      $('.pop').slideFadeToggle();
    }
    return false;
  });

  $('.close').on('click', function() {
    deselect($('#contact'));
    return false;
  });
});

$.fn.slideFadeToggle = function(easing, callback) {
  return this.animate({ opacity: 'toggle', height: 'toggle' }, 'fast', easing, callback);
};

そして最後にhtml:

<div class="messagepop pop">
  <form method="post" id="new_message" action="/messages">
    <p><label for="email">Your email or name</label><input type="text" size="30" name="email" id="email" /></p>
    <p><label for="body">Message</label><textarea rows="6" name="body" id="body" cols="35"></textarea></p>
    <p><input type="submit" value="Send Message" name="commit" id="message_submit"/> or <a class="close" href="/">Cancel</a></p>
  </form>
</div>

<a href="/contact" id="contact">Contact Us</a>

これがjsfiddleのデモと実装です。

状況によっては、AJAX呼び出しを介してポップアップコンテンツを読み込むことができます。コンテンツが表示される前にユーザーに大幅な遅延を与える可能性があるため、可能な限りこれを回避することをお勧めします。ここでは、このアプローチを採用した場合に必要となる変更をいくつか紹介します。

HTMLは次のようになります。

<div>
    <div class="messagepop pop"></div> 
    <a href="/contact" id="contact">Contact Us</a>
</div>

そしてJavaScriptの一般的な考え方は次のようになります。

$("#contact").on('click', function() {
    if($(this).hasClass("selected")) {
        deselect();               
    } else {
        $(this).addClass("selected");
        $.get(this.href, function(data) {
            $(".pop").html(data).slideFadeToggle(function() { 
                $("input[type=text]:first").focus();
            });
        }
    }
    return false;
});
238
Andy Gaskell

jQuery UI Dialog をチェックしてください。あなたはこのようにそれを使うでしょう:

JQuery:

$(document).ready(function() {
    $("#dialog").dialog();
});

マークアップ:

<div id="dialog" title="Dialog Title">I'm in a dialog</div>

完了しました。

これが最も単純なユースケースについてのものであることを心に留めておいてください、私は ドキュメンテーション を読むことを提案します。

96
karim79

ColorBox というjQueryプラグインを使用しています。

  1. とても使いやすい
  2. 軽量
  3. カスタマイズ可能
  4. 私はまだjQueryのために見た中で最も良いポップアップダイアログ
23
JasonDavis
8
Yanni

Magnific Popup を試してみてください。

8
Marvin3

私はこれが 素晴らしいチュートリアルだと思います 簡単なjqueryポップアップを書くことについて。プラスそれはとても見えます 美しい

4
andy boot

まさにこれの良い、簡単な例がここにあります: http://www.queness.com/post/77/simple-jquery-modal-window-tutorial

3
Evernoob

非常に軽量なモーダルポップアッププラグイン。 POPELT - http://welbour.com/labs/popelt/

軽量、入れ子になったポップアップ、オブジェクト指向、動的ボタン、レスポンシブなどをサポートしています。次回のアップデートには、ポップアップAjaxフォーム送信などが含まれます。

気軽に使用して、フィードバックをツイートしてください。

3
Elton Jain

Html5とjavascriptを使った簡単なポップアップウィンドウ.

html: -

    <dialog id="window">  
     <h3>Sample Dialog!</h3>  
     <p>Lorem ipsum dolor sit amet</p>  
     <button id="exit">Close Dialog</button>
    </dialog>  

  <button id="show">Show Dialog</button> 

JavaScript: - /

   (function() {  

            var dialog = document.getElementById('window');  
            document.getElementById('show').onclick = function() {  
                dialog.show();  
            };  
            document.getElementById('exit').onclick = function() {  
                dialog.close();  
            };
        })();
1
Arshid KV

これは非常に単純なポップアップです。

<!DOCTYPE html>
<html>
    <head>
        <style>
            #modal {
                position:absolute;
                background:gray;
                padding:8px;
            }

            #content {
                background:white;
                padding:20px;
            }

            #close {
                position:absolute;
                background:url(close.png);
                width:24px;
                height:27px;
                top:-7px;
                right:-7px;
            }
        </style>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script>
            var modal = (function(){
                // Generate the HTML and add it to the document
                $modal = $('<div id="modal"></div>');
                $content = $('<div id="content"></div>');
                $close = $('<a id="close" href="#"></a>');

                $modal.hide();
                $modal.append($content, $close);

                $(document).ready(function(){
                    $('body').append($modal);                       
                });

                $close.click(function(e){
                    e.preventDefault();
                    $modal.hide();
                    $content.empty();
                });
                // Open the modal
                return function (content) {
                    $content.html(content);
                    // Center the modal in the viewport
                    $modal.css({
                        top: ($(window).height() - $modal.outerHeight()) / 2, 
                        left: ($(window).width() - $modal.outerWidth()) / 2
                    });
                    $modal.show();
                };
            }());

            // Wait until the DOM has loaded before querying the document
            $(document).ready(function(){
                $('a#popup').click(function(e){
                    modal("<p>This is popup's content.</p>");
                    e.preventDefault();
                });
            });
        </script>
    </head>
    <body>
        <a id='popup' href='#'>Simple popup</a>
    </body>
</html>

このチュートリアルでは、より柔軟な解決策を見つけることができます。 http://www.jacklmoore.com/notes/jquery-modal-tutorial/ サンプルは close.png です。

0
John29

CSSポップアップロジックのみ!試してみてください。簡単!私はこれが将来的に人気のハックだと思います

            <a href="#openModal">OPEN</a>

            <div id="openModal" class="modalDialog">
                <div>
                    <a href="#close"  class="close">X</a>
                    <h2>MODAL</h2>

                </div>
            </div>


.modalDialog {
    position: fixed;
    font-family: Arial, Helvetica, sans-serif;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: rgba(0,0,0,0.8);
    z-index: 99999;
    -webkit-transition: opacity 400ms ease-in;
    -moz-transition: opacity 400ms ease-in;
    transition: opacity 400ms ease-in;
    display: none;
    pointer-events: none;
}

.modalDialog:target {
    display: block;
    pointer-events: auto;
}

.modalDialog > div {
    width: 400px;
    position: relative;
    margin: 10% auto;
    padding: 5px 20px 13px 20px;
    border-radius: 10px;
    background: #fff;
    background: -moz-linear-gradient(#fff, #999);
    background: -webkit-linear-gradient(#fff, #999);
    background: -o-linear-gradient(#fff, #999);
}
0
zloctb