发布最近写的jquery.popup插件

由于Asp.Net Mvc 不支持ViewState,也不支持Asp.net Ajax服务器端控件,导致以前写的部门选择控件不能正常使用,搜索jquery的插件也没找到可以使用的插件,所以自己动手写一个jquery.popup插件

完整源代码及示例下载:jquery.popup.rar

下拉框选择控件,该插件需要引用jquery.dimensions插件,结合jquery.treeview可以支持树状结构的下拉框

先看看效果如下:

image

jquery.popup.js代码如下:
/* $Id: jquery.popup.js 67 2008-01-22 08:54:37Z Bolik@xjgc.com $ */
 
(function($) {
  function Popup(el, content) {
    this.input = $(el);
    this.popupContent = content;
    
    this.input.attr("readonly", "readonly");
    
    this.bindMethodsToObj("show", "hide", "hideIfClickOutside", "selectItem");
    
    this.build();
    //this.selectItem();
    this.hide();
  };
 
  Popup.prototype = {
    build: function() {
      this.ClearController = $('<a href="#">clear</a>').click(this.bindToObj(function(event) {
        this.selectItem("", "");
        this.hide();
        return false;
      })); 
        
      this.popupController = $('<div class="popupController"></div>').append(this.ClearController); 
      this.popupContent = $('<div class="popupContent"></div>').append(this.popupContent); 
     
      this.popupPanel = this.rootLayers = $('<div class="popupPanel"></div>')
        .css({ display: "none", position: "absolute", zIndex: 100 })
        .append(this.popupController, this.popupContent)
        .appendTo(document.body);
      
      if ($.browser.msie && $.browser.version < 7) {
        this.ieframe = $('<iframe class="popupPanel_ieframe" frameborder="0" src="#"></iframe>')
          .css({ position: "absolute", display: "none", zIndex: 99 })
          .insertBefore(this.popupPanel);
        this.rootLayers = this.rootLayers.add(this.ieframe);
      };
      
      $("a", this.popupContent).click(this.bindToObj(function(event) {
        this.selectItem($(event.target).attr("id"), $(event.target).attr("innerHTML"));
        this.hide();
        return false;
      }));
 
      //this.input.change(this.bindToObj(function() { this.selectItem(); }));
    }, 
    
    selectItem: function(item, text) {  
      this.selectedItem = item;
      this.input.attr("SelectedItem", item);
      this.input.val(text);      
    },
    
    show: function() {
      this.setPosition();
      this.rootLayers.css("display", "block");
      this.input.unbind("focus", this.show);
      $(document.body).click(this.hideIfClickOutside);
    },
    
    hide: function() {
      this.rootLayers.css("display", "none");
      $(document.body).unbind("click", this.hideIfClickOutside);
      this.input.focus(this.show);
    },
    
    hideIfClickOutside: function(event) {
      if (event.target != this.input[0] && !this.insideSelector(event)) {
        this.hide();
      };
    }, 
     
    setPosition: function() {
      var offset = this.input.offset();
      this.rootLayers.css({
        top: offset.top + this.input.outerHeight(),
        left: offset.left
      });
      
      if (this.ieframe) {
        this.ieframe.css({
          width: this.popupPanel.outerWidth(),
          height: this.popupPanel.outerHeight()
        });
      };
    },  
     
    insideSelector: function(event) {
      var offset = this.popupPanel.offset();
      offset.right = offset.left + this.popupPanel.outerWidth();
      offset.bottom = offset.top + this.popupPanel.outerHeight();
      
      return event.pageY < offset.bottom &&
             event.pageY > offset.top &&
             event.pageX < offset.right &&
             event.pageX > offset.left;
    },
    
    bindToObj: function(fn) {
      var self = this;
      return function() { return fn.apply(self, arguments) };
    },
    
    bindMethodsToObj: function() {
      for (var i = 0; i < arguments.length; i++) {
        this[arguments[i]] = this.bindToObj(this[arguments[i]]);
      };
    } 
    
  };
 
  $.fn.popup = function(content) {
    return this.each(function() { 
      new Popup(this, content); 
    });
  };
})(jQuery);

使用方法如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">  
  <meta name="author" content="Bolik" />
  <meta content="Copyright 2006-2008 Bolik" name="copyright" />
  <meta name="description" content="WorkItem Tracking System" />
  <meta name="keywords" content="wit, WorkItem, WorkItemTracking, Ajax, Web2.0, MVC" />
  <%
   1: --<meta http-equiv="Pragma" content="no-cache" />--
%>
  <meta name="robots" content="none" />
  <link charset="utf-8" rel="stylesheet" type="text/css" href="Common.css" />
  <link  charset="utf-8" rel="stylesheet" type="text/css" href="jquery.popup.css"/>
  <link charset="utf-8" rel="stylesheet" type="text/css" href="jquery.treeview.css" />
  <script type="text/javascript" charset="utf-8" src="jquery.js"></script>
   1:  
   2:   <script type="text/javascript" charset="utf-8" src="jquery.treeview.js">
   1: </script>
   2:   <script type="text/javascript" charset="utf-8" src="jquery.cookie.js">
   1: </script>
   2:   <script type="text/javascript" charset="utf-8" src="jquery.dimensions.js">
   1: </script>
   2:   <script type="text/javascript" charset="utf-8" src="jquery.popup.js">
   1: </script> 
   2: </head>
   3: <body>
   4:   <div>
   5:     <input id="popupDemo1" type="text" class="popup" />
   6:     <input id="popupDemo2" type="text" class="popup" />
   7:     <input id="TreeViewPopup" type="text" />
   8:     <script type="text/javascript">
   9:       jQuery(function($){
  10:             $("input.popup").popup('<a href="#" id="demo1">demo1Text</a><br /><a href="#" id="demo2">demo2Text</a>');
  11:             $("input#TreeViewPopup").popup(' <ul class="treeview-popup"><li><a href="#" id="node_100">Europe</a><ul><li><a href="#" id="node_101">Norway</a><ul><li><a href="#" id="node_102">Rogaland</a><ul><li class="tree_sheet.gif"><a href="#" id="node_103">Stavanger</a></li><li class="tree_sheet.gif"><a href="#" id="node_104">Haugesund</a></li></ul></li><li class="tree_sheet.gif"><a href="#" id="node_105">Hordaland</a></li>            <li class="tree_sheet.gif"><a href="#" id="node_106">Oslo</a></li>          </ul>        </li>        <li><a href="#" id="node_107">United Kingdom</a>          <ul>            <li><a href="#" id="node_108">London</a></li>            <li><a href="#" id="node_109">Manchester</a></li>            <li><a href="#" id="node_110">Oxford</a></li>          </ul>        </li>        <li><a href="#" id="node_111">Sweden</a></li>        <li><a href="#" id="node_112">Denmark</a></li>        <li><a href="#" id="node_113">Germany</a></li>      </ul>    </li>    <li><a href="#" id="node_114">Asia</a></li>    <li><a href="#" id="node_115">Africa</a><ul><li><a href="#" id="node_116">Tanzania</a></li><li><a href="#" id="node_117">Kenya</a></li></ul></li><li><a href="#" id="node_118">America</a><ul><li class="tree_sheet.gif"><a href="#" id="node_119">Canada</a></li><li><a href="#" id="node_120">United States</a></li><li class="tree_sheet.gif"><a href="#" id="node_121">Mexico</a></li><li><a href="#" id="node_122">Argentina</a></li></ul></li></ul>');
  12:             $(".treeview-popup").treeview({             
  13:             persist: "cookie",
  14:             cookieId: "treeview-popup"
  15:         });
  16:         });
  17:     
</ script >
  </div>
</body>
</html>

欢迎大家交流指正!!!

 

完整源代码及示例下载:jquery.popup.rar

转载于:https://www.cnblogs.com/Bolik/archive/2008/01/23/1050074.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
jQuery是一种重要的JavaScript库,它提供了许多强大的功能,其中之一是popup画面。popup画面是指在网页中弹出一个新的浮动窗口,通常用于显示更多的信息、进行用户操作或者提示用户。下面我将简单介绍一下如何使用jQuery来创建popup画面。 首先,需要在网页中包含jQuery库的引用,通过在<head>标签中添加以下代码实现: <script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script> 然后,在HTML中添加触发popup画面的元素,可以是一个按钮、一个链接或者其他任何需要触发popup的元素。例如,我们可以创建一个按钮来触发popup画面: <button id="popupButton">点击显示popup画面</button> 接下来,在JavaScript中编jQuery代码来实现popup画面。首先,我们需要为按钮添加一个点击事件,当按钮被点击时,显示popup画面。代码如下: $(document).ready(function(){ $("#popupButton").click(function(){ // 显示popup画面的代码 }); }); 在以上代码中,$(document).ready()是用来在文档加载完成后执行代码的函数。$("#popupButton")是用来选取ID为"popupButton"的元素,.click()是用来添加点击事件的函数。 最后,我们需要在点击事件的处理函数中编代码来显示popup画面。这可以通过使用jQuery的弹出窗口件、CSS样式和动画效果来实现。以下是一个简单的示例代码: $(document).ready(function(){ $("#popupButton").click(function(){ $("#popup").show(); // 显示popup画面 }); }); 在以上代码中,$("#popup")选取ID为"popup"的元素,并使用.show()函数来显示该元素。 当用户点击触发popup的元素时,popup画面将显示在网页中,用户可以在该画面中查看更多信息、进行操作或者关闭popup。 总结一下,通过使用jQuery库和简单的JavaScript代码,我们可以很容易地实现popup画面。通过添加点击事件和使用合适的弹出窗口件、CSS样式和动画效果,我们可以为用户提供一个更加友好和交互性强的用户界面体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值