Homemade lightweight jQuery.boxy dialog plug-in code_jquery
In this regard, the jquery.boxy plug-in has been made very powerful. Commonly used prompts, confirmations, dragging, changing sizes, and asynchronous loading are all very practical, resulting in larger files (negligible), and many functions are not needed. For this reason , with the attitude and idea of learning and practicing at the same time, I made a lightweight pop-up layer plug-in suitable for this project. This is the first time I write a jqeury plug-in, and I am also preparing to encapsulate common operations into jquery plug-ins in the future. Bar.
First of all, let’s give the plug-in a name so that it can fool people. Let’s call it jquey.cvbox.min.js. cv is the abbreviation of the website domain name ChinaValue. The compressed capacity is controlled below 6K. Since it's not finished yet, I'll write down my thoughts first.
1. Add container elements to the page to display content, as well as the background of the pop-up layer. It only needs to be there. As for where you were born (that is, where it is displayed on the page) and what you will grow into. The appearance will be cultivated (set up) later, and the appearance will naturally be dressed up by the art director.
2. According to our needs, we define some commonly used objects in advance, such as the background of the mask, the container of the pop-up layer, the title bar of the pop-up layer, the content area of the pop-up layer, and the height of the current browser window. Kuan, etc., with these, it will be much more convenient to use later.
3. Start stuffing content into the container. The content can be a prompt (corresponding to the prompt function), a question (corresponding to the confirmation box), a picture (for example, for enlarging a small picture), or It is a piece of HTML code (replacing the inconvenience of writing HTML directly in JS).
4. Define the event that the user clicks to close, that is, hiding or removing the background layer and pop-up layer, leaving it to be called when the operation is completed.
5. Set the background layer transparency and scroll height, set the position of the pop-up layer, center it with scrolling or be fixed.
6. Finally, in order to facilitate use in multiple situations, extract variable parameters. The parameters must have default values, and use $.extend to complete it.
The beta version is expected to be released after the holidays, and it will be unabridged.
Online demo: http://demo.jb51.net/js/jquery_cvbox/index.htm
Package download: http://xiazai.jb51.net/201010/ yuanma/jquery_cvbox.rar
jquery.cvbox.min.js code
/*
* JQuery.cvbox.js
* http://www.chinavalue.net
*
* J.Wang
* http://0417.cnblog.socm
*
* 2010.09.30
*/
(function($) {
$.fn.cvbox = function(options) {
var self = $(this);
var defaults = {
titleBarText: "",
titleBarClose: "关闭",
bgClickClose: false,
bgShow: true,
bgOpacity: 0.2,
confirmText: "",
alertText: "",
delayClose: 0,
submitAfter: function() {
$.noop();
}
};
var param = $.extend({}, defaults, options || {});
//弹框的显示
var cvBoxElement = '';
cvBoxElement += '
cvBoxElement += '
cvBoxElement += '';
cvBoxElement += '
if ($("#cvBoxBorder").size()) {
$("#cvBoxBorder").show();
if (param.bgShow) {
$("#cvBoxShade").show();
}
else {
$("#cvBoxShade").hide();
}
}
else {
$("body").append(cvBoxElement);
}
//一些元素对象,浏览器宽高,滚动高度,页面高度
var cbBg = $("#cvBoxShade");
var cbBorder = $("#cvBoxBorder");
var cbTitleBar = $("#cvBoxTitleBar");
var cbBody = $("#cvBoxBody");
var w, h, st, ph;
var cb = {
//装载的内容
content: function() {
var text;
if (param.confirmText != "") {
text = $('
}
else if (param.alertText != "") {
text = $('
}
else {
self.show();
text = self;
}
return text;
},
hw: function(obj) {
//获取任意元素的高宽
var hwSize = {};
$('').appendTo("body").append(obj.clone());
hwSize.w = $("#cbBox").width();
hwSize.h = $("# cbBox").height();
$("#cbBox").remove();
return hwSize;
},
//The width and height transparency of the black background, etc., The position of the pop-up box
position: function() {
w = $(window).width(), h = $(window).height(), st = $(window).scrollTop(), ph = $(document).height();
cbBg.width(w).height(ph).css("opacity", param.bgOpacity);
//The position of the main content
var x_size = cb.hw(cb.content());
var xh = x_size.h, xw = x_size.w;
var t = st (h - xh) / 2, l = (w - xw) / 2;
cbBorder.css({
width: xw,
top: t,
left: l,
zIndex: 9999
});
},
//Position
posfix: function() {
if (window.XMLHttpRequest) {
cbBorder.css("position", "fixed");
} else {
$(window).scroll(function() {
cb.position();
});
}
},
//center
center : function() {
$(window).resize(function() {
cb.position();
});
},
bgclick: function() {
cbBg.click(function() {
cb.hide();
});
},
bghide: function() {
cbBg.hide ();
},
//Hide the pop-up box
hide: function() {
if (param.confirmText == "" && param.alertText == "") {
cb.content().hide().appendTo($("body"));
}
//cbBorder.fadeOut(300);
cbBorder.remove( );
cbBg.remove();
return false;
},
barhide: function() {
cbTitleBar.hide();
},
show: function() {
if (cbBody.html() == "") {
cbBody.append(cb.content());
}
cb.position();
cb.center();
if (param.titleBarText == "") {
cb.barhide();
}
if ( !param.bgShow) {
cb.bghide();
}
if (param.bgClickClose) {
cb.bgclick();
}
if (param.delayClose > 0) {
setTimeout(cb.hide, param.delayClose);
}
}
};
cb.show();
//Binding of some events
$("#cvBoxBtnSubmit").bind("click", function() {
if (param.confirmText != ""){
param.submitAfter() ;
}
cb.hide();
});
$("#cvBoxBtnCancel").bind("click", function() {
cb.hide ();
});
$("#cvBoxTitleBarClose").bind("click", function() {
cb.hide();
});
}
})(jQuery);
Complete test code
未压缩版本是6K大小,压缩后只有2K大,应该算很轻了。
1 |
|
1 |
|
1 |
|

Hot AI Tools

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

10 fun jQuery game plugins to make your website more attractive and enhance user stickiness! While Flash is still the best software for developing casual web games, jQuery can also create surprising effects, and while not comparable to pure action Flash games, in some cases you can also have unexpected fun in your browser. jQuery tic toe game The "Hello world" of game programming now has a jQuery version. Source code jQuery Crazy Word Composition Game This is a fill-in-the-blank game, and it can produce some weird results due to not knowing the context of the word. Source code jQuery mine sweeping game

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

This tutorial demonstrates how to create a captivating parallax background effect using jQuery. We'll build a header banner with layered images that create a stunning visual depth. The updated plugin works with jQuery 1.6.4 and later. Download the

This article demonstrates how to automatically refresh a div's content every 5 seconds using jQuery and AJAX. The example fetches and displays the latest blog posts from an RSS feed, along with the last refresh timestamp. A loading image is optiona

Matter.js is a 2D rigid body physics engine written in JavaScript. This library can help you easily simulate 2D physics in your browser. It provides many features, such as the ability to create rigid bodies and assign physical properties such as mass, area, or density. You can also simulate different types of collisions and forces, such as gravity friction. Matter.js supports all mainstream browsers. Additionally, it is suitable for mobile devices as it detects touches and is responsive. All of these features make it worth your time to learn how to use the engine, as this makes it easy to create a physics-based 2D game or simulation. In this tutorial, I will cover the basics of this library, including its installation and usage, and provide a

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.
