码迷,mamicode.com
首页 > Web开发 > 详细

jQuery基础教程-第8章-003Providing flexible method parameters

时间:2016-05-17 00:30:49      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:

一、The options object

1.增加阴影效果

 1 (function($) {
 2     $.fn.shadow = function() {
 3         return this.each(function() {
 4             var $originalElement = $(this);
 5             for (var i = 0; i < 5; i++) {
 6                 $originalElement
 7                     .clone()
 8                     .css({
 9                         position: ‘absolute‘,
10                         left: $originalElement.offset().left + i,
11                         top: $originalElement.offset().top + i,
12                         margin: 0,
13                         zIndex: -1,
14                         opacity: 0.1
15                     })
16                     .appendTo(‘body‘);
17             }
18         });
19     };
20 })(jQuery);
21 
22 $(document).ready(function() {
23     $(‘h1‘).shadow();
24 });

 

 1 (function($) {
 2     $.fn.shadow = function(options) {
 3         return this.each(function() {
 4             var $originalElement = $(this);
 5             for (var i = 0; i < options.copies; i++) {
 6                 $originalElement
 7                     .clone()
 8                     .css({
 9                         position: ‘absolute‘,
10                         left: $originalElement.offset().left + i,
11                         top: $originalElement.offset().top + i,
12                         margin: 0,
13                         zIndex: -1,
14                         opacity: options.opacity
15                     })
16                     .appendTo(‘body‘);
17             }
18         });
19     };
20 })(jQuery);

Calling this method now requires us to provide an object containing the option values:

1 $(document).ready(function() {
2     $(‘h1‘).shadow({
3         copies: 3,
4         opacity: 0.25
5     });
6 });

 

二、Default parameter values

 1 (function($) {
 2     $.fn.shadow = function(opts) {
 3         var defaults = {
 4             copies: 5,
 5             opacity: 0.1
 6         };
 7         var options = $.extend(defaults, opts);
 8         // ...
 9     };
10 })(jQuery);
11 
12 
13 
14 $(document).ready(function() {
15     $(‘h1‘).shadow({
16         copies: 3
17     });
18 });
19 
20 $(document).ready(function() {
21     $(‘h1‘).shadow();
22 });

 

三、Callback functions

 1 (function($) {
 2     $.fn.shadow = function(opts) {
 3         var defaults = {
 4             copies: 5,
 5             opacity: 0.1,
 6             copyOffset: function(index) {
 7                 return { x: index, y: index };
 8             }
 9         };
10         var options = $.extend(defaults, opts);
11         return this.each(function() {
12             var $originalElement = $(this);
13             for (var i = 0; i < options.copies; i++) {
14                 var offset = options.copyOffset(i);
15                 $originalElement
16                     .clone()
17                     .css({
18                         position: ‘absolute‘,
19                         left: $originalElement.offset().left + offset.x,
20                         top: $originalElement.offset().top + offset.y,
21                         margin: 0,
22                         zIndex: -1,
23                         opacity: options.opacity
24                     })
25                     .appendTo(‘body‘);
26             }
27         });
28     };
29 })(jQuery);
30 
31 $(document).ready(function() {
32     $(‘h1‘).shadow({
33         copyOffset: function(index) {
34             return { x: -index, y: -2 * index };
35         }
36     });
37 });

 

四、Customizable defaults

To make the defaults customizable, we need to move them out of our method definition and into a location that is accessible by outside code:

 1 (function($) {
 2     $.fn.shadow = function(opts) {
 3         var options = $.extend({}, $.fn.shadow.defaults, opts);
 4         // ...
 5     };
 6     $.fn.shadow.defaults = {
 7         copies: 5,
 8         opacity: 0.1,
 9         copyOffset: function(index) {
10             return { x: index, y: index };
11         }
12     };
13 })(jQuery);
14 
15 $(document).ready(function() {
16     $.fn.shadow.defaults.copies = 10;
17     $(‘h1‘).shadow({
18         copyOffset: function(index) {
19             return { x: -index, y: index };
20         }
21     });
22 });

 

jQuery基础教程-第8章-003Providing flexible method parameters

标签:

原文地址:http://www.cnblogs.com/shamgod/p/5499927.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!