【问题标题】:JQuery plugin, Javascript - How create only for JQuery plugin global variable, or json object and change this (variable, object) in function levelJQuery 插件,Javascript - 如何仅为 JQuery 插件全局变量或 json 对象创建并在函数级别更改此(变量、对象)
【发布时间】:2011-10-08 21:49:15
【问题描述】:

我使用 JQuery:1.6.4

我的目标是创建函数,该函数无需在 () 中提供变量,就可以启动和停止脚本。

当我尝试达到.. 目标.. 时发现了一些问题...

  1. 仅适用于 JQuery 插件的特定变量,具有来自函数的访问权限(更改变量)

  2. 函数 move_text

div 190px 和距离 2.5px 的结果

190, 187.5, 190... 我们可以.. 检测全局变量的错误变化

应该是 190、187.5、185...

如何解决这个问题(JSON?窗口)?

我尝试过使用 JSON 的方式,对象......但是......我浪费了很多时间......但什么也没找到......

那么.. 使用 window['distance_iv'] 的方式好吗?

这是我的代码:

$.fn.emulate_marquee = function( options ) {

    var settings = {
      'ob_name': '', // object who we use...
      'direction': 'left', 
      'scroll_amount': 100, 
      'delay': 25 // Animation speed
    };

    //call in the default otions
            options = $.extend(settings, options);

            return this.each(function(options) {



                var object = $(settings.ob_name);
                window['distance_ob'] = width = object.width(); 

                //object.css(settings.direction , width + 'px'); // Set Width
                //alert(width);

                total_width  = (2 * width) / settings.scroll_amount;
                // total_width

                settings.distance = settings.scroll_amount / (1000/ settings.delay); // Calculate distance for one frame

                width_s = screen.width; // Get screen width




               next_obj = setTimeout (function(){ 
                            move_text()
                          }, 1);

               /*next_res = setTimeout (function(){ 
                            resolution_ch()
                          }, 1);

                */

                var move_text = function()
                {
                    //alert((settings.width - (i * settings.scroll_amount)) + 'px');

                    alert(window['distance_ob'] + 'px');
                    window['distance_ob'] -= settings.distance;
                    object.css(settings.direction , window['distance_ob'] + 'px');
                    alert(window['distance_ob'] + 'px');


                    if (window['distance_ob'] < total_width)
                    {
                        next_obj = setTimeout (function(){ 
                            move_text()
                        }, settings.delay); 
                    }
                    else
                    {
                       next_obj = setTimeout (function(){ 
                            move_text()
                        }, settings.delay);
                        window['distance_ob'] = width;
                    }

                }

                var resolution_ch = function()
                {
                    if (screen.width != width_s)
                    {
                        // change varabiales... for new resolution

                        width_s =  screen.width;
                        distance_ob = width = object.width();
                        alert(width_s + ' ' + object.width());
                        total_width  = (2 * width) / settings.scroll_amoun;


                        alert("Detected Change resolution.... nuke in 5 second... started...");

                    }

                    next_res = setTimeout (function(){ 
                                resolution_ch()
                               }, 1000);



                }


            });
  };

【问题讨论】:

  • 在 firefox 7 中...故障 190、192.5、190(左:(distance_ob)),函数 move_text ...嗯,这是个好问题
  • /* 小更新评论 */ 在firefox 7等浏览器... 故障190, 192.5, 190, ... (like a 1, 2, 1, 2) (左: (distance_ob )),函数 move_text ... 嗯,所以我尝试找到解决它的好方法
  • 移动功能应该适用于 190px div 190, 187.5, 185...

标签: javascript jquery json global-variables


【解决方案1】:

也许尝试将您的全局变量保存到jQuery.data() (ref)

但由于听起来您可能需要从插件外部访问方法,所以我建议您检查一些不同的插件模式。

查看jQuery plugin patterns 这个很棒的“库”。

特别看一下将插件实例保存到 jQuery 数据中的模式;这是在所有小部件工厂模板中默认完成的。但我个人最喜欢的是“jquery.namespace.plugin-boilerplate.js”

【讨论】:

  • 谢谢,它正在工作。 :) 但我在我的脚本中发现了有趣的(错误):total_width = (2 * width) / settings.scroll_amount;这个错误使 window['distance_ob'] > total_width 选项 2 在 if and... 递归中。解决方案是更改 'total_width = (2 * width) / settings.scroll_amount;'这要“宽度”和更多的睡眠。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多