Với jQuery AJAX Loader, bạn có thể chèn hiệu ứng hiển thị thông báo chờ bằng hình ảnh (gif) khá đẹp. Mặt khác plugin này còn vô hiệu hóa các thao tác của người dùng khi ajax đang hoạt động, giúp giảm thiểu lỗi xảy ra trong thời gian chờ.
Bạn hãy xem demo dưới đây để thấy cách mà jQuery AJAX Loader hoạt động. Sau đó có thể quyết định xem có nên dùng plugin này vào trang web của bạn không nhé!
Demo
View Demo :: jQuery Ajax LoaderThe HTML
Xem demo (View Source)Note: Phần tử chứa ajaxloader phải có thuộc tính css là “position:relative;”
The CSS
.ajax_overlay {} .ajax_loader {background: url("spinner_squares_circle.gif") no-repeat center center transparent;width:100%;height:100%;}Note: bạn có thể format lại style cho class ‘.ajax_overlay ‘ nếu không muốn dùng style mặc định của ajax loader
The jQuery
function ajaxLoader (el, options) { // Becomes this.options var defaults = { bgColor : '#fff', duration : 800, opacity : 0.7, classOveride : false } this.options = jQuery.extend(defaults, options); this.container = $(el); this.init = function() { var container = this.container; // Delete any other loaders this.remove(); // Create the overlay var overlay = $('Note: chèn được code trên trong file script (*.js)').css({ 'background-color': this.options.bgColor, 'opacity':this.options.opacity, 'width':container.width(), 'height':container.height(), 'position':'absolute', 'top':'0px', 'left':'0px', 'z-index':99999 }).addClass('ajax_overlay'); // add an overiding class name to set new loader style if (this.options.classOveride) { overlay.addClass(this.options.classOveride); } // insert overlay and loader into DOM container.append( overlay.append( $('').addClass('ajax_loader') ).fadeIn(this.options.duration) ); }; this.remove = function(){ var overlay = this.container.children(".ajax_overlay"); if (overlay.length) { overlay.fadeOut(this.options.classOveride, function() { overlay.remove(); }); } } this.init(); }
Usage examples
$(document).ready(function() { $(".box-1").live('click', function(){ new ajaxLoader(this, options); }); });Note: Xem chi tiết tại đây Ajax Loader demo page
Default options
var options = { bgColor : '#fff', duration : 800, opacity : 0.7, classOveride : false }Các giá trị của option
- bgColor: string – colour of the background overlay
- duration: number – length of fadeIn and fadeOut effects
- opacity: number – opacity of the background overlay
- classOveride: string – a class name to over-ride your default loader style
Download
‘Ajax Loader & Spinner’ - Demo và codejQuery Ajax Loader & Spinner
Tham khảo http://www.aplusdesign.com.au/blog/jquery-ajax-loader-spinner/