撒拉嘿哟木头 2009-08-25 14:37 采纳率: 100%
浏览 367
已采纳

如何使用 jQuery 生成一个简单的弹出窗口

I am designing a web page. When we click the content of div named mail, how can I show a popup window containing a label email and text box?

转载于:https://stackoverflow.com/questions/1328723/how-to-generate-a-simple-popup-using-jquery

  • 写回答

11条回答 默认 最新

  • Memor.の 2009-08-25 17:03
    关注

    First the CSS - tweak this however you like:

    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;
    }
    

    And the 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);
    };
    

    And finally the 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>
    

    Here is a jsfiddle demo and implementation.

    Depending on the situation you may want to load the popup content via an ajax call. It's best to avoid this if possible as it may give the user a more significant delay before seeing the content. Here couple changes that you'll want to make if you take this approach.

    HTML becomes:

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

    And the general idea of the JavaScript becomes:

    $("#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;
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(10条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号