我使用 JQuery:1.6.4
我的目标是创建函数,无需在 () 中提供变量,就可以启动和停止脚本。
当我尝试达到..目标..我发现了一些问题...
特定变量仅适用于 JQuery 插件,可通过函数访问(更改变量)
函数 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);
}
});
};
最佳答案
也许尝试将全局变量保存到 jQuery.data()
( ref )
但是,由于听起来您可能需要从插件外部访问方法,因此我建议检查一些不同的插件模式。
看看这个很棒的“图书馆”jQuery plugin patterns .
特别关注将插件实例保存到 jQuery 数据中的模式;这是所有小部件工厂模板中默认完成的。但我个人最喜欢的是“jquery.namespace.plugin-boilerplate.js”
关于javascript - JQuery 插件,Javascript - 如何仅为 JQuery 插件全局变量或 json 对象创建并在函数级别更改此(变量、对象),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7699988/