Javascript 从另一个.js文件调用一个.js文件中的函数时出现命名空间问题
在尝试为许多图像制作可滑动的幻灯片时,我尝试将两个插件缝合在一起,然后。我想让滑动插件中的幻灯片放映计时器事件调用Lazyloader中的更新函数;也就是说,Swipe不知道Lazyloader中的内容,因此无法调用该函数Javascript 从另一个.js文件调用一个.js文件中的函数时出现命名空间问题,javascript,callback,javascript-namespaces,Javascript,Callback,Javascript Namespaces,在尝试为许多图像制作可滑动的幻灯片时,我尝试将两个插件缝合在一起,然后。我想让滑动插件中的幻灯片放映计时器事件调用Lazyloader中的更新函数;也就是说,Swipe不知道Lazyloader中的内容,因此无法调用该函数 给定以下代码,我是否正确理解我的问题 如果是这样的话,我如何让Swipe访问Lazyloader的功能 多谢各位 后续行动: 在与Cheeso进行了问答之后,我现在明白了,关于我最终需要发生什么,我问了一个错误的问题。我添加此评论是为了避免将来的读者因为我的问题而感到困惑。这
this.callback = this.options.callback || function() {
在html中调用的脚本:
<script src="js/jquery.js" type="text/javascript"></script>
<script src="js/jquery.lazyload.js" type="text/javascript"></script>
<script src='js/swipe.js' type="text/javascript"></script>
<script type="text/javascript">$(document).ready(function() {
function myAlert(){
//call a function from inside jquery.lazyload.js
alertMe();
}
//instantiate a swipe object and pass in a bunch of
//options, especially "callback" which fires on every slide change
window.mySwipe = new Swipe(document.getElementById('slider1'), {
startSlide: 0,
speed: 1000,
auto: 5000,
callback: function(){
myAlert();
}
});
// run lazyload on every VISIBLE image with the class "lazy" on load, and on every click
$("img.lazy").lazyload({event: "click", effect : "fadeIn", threshold: 0,});
});
</script>
可以这么简单:在Swipe实例上设置
callback
选项,使其指向执行LazyLoader操作的函数。这只是一个猜测,因为我不知道滑动,但我查看了代码转储并看到了回调函数;它似乎是刷卡扩展的关键点
如果我是正确的,那么这根本不是名称空间问题。这正是你所说的——将不同的库或模块组合在一起。通常一个js模块会有一两个扩展点来实现这一点。我猜Swipe的扩展点就是回调
也许是这样的:
function myFn() {
// do whatever lazyload thing you want to do, here.
}
$(document).ready(function() {
var slider1 = new Swipe(document.getElementById('slider1'),
{startSlide: 0,
speed: 1000,
auto: 10000,
callback:myFn});
$("img.lazy").lazyload({event: "click"});
});
您可能需要重构以使slider1
或某些其他变量成为全局变量,以便在doc.ready函数之外访问它们
跟进
你有这个:
<script type="text/javascript">
$(document).ready(function() {
function myAlert(){
//call a function from inside jquery.lazyload.js
alertMe();
}
//instantiate a swipe object and pass in a bunch of
//options, especially "callback" which fires on every slide change
window.mySwipe = new Swipe(document.getElementById('slider1'), {
startSlide: 0,
speed: 1000,
auto: 5000,
callback: function(){
myAlert();
}
});
$("img.lazy").lazyload({event: "click", effect : "fadeIn", threshold: 0,});
});
</script>
$(文档).ready(函数(){
函数myAlert(){
//从jquery.lazyload.js内部调用函数
alertMe();
}
//实例化一个滑动对象并传入一组
//选项,尤其是每次幻灯片更改时触发的“回调”
window.myswip=new swip(document.getElementById('slider1'),{
开始滑动:0,
速度:1000,
汽车:5000,
回调:函数(){
myAlert();
}
});
$(“img.lazy”).lazyload({事件:“单击”,效果:“fadeIn”,阈值:0,});
});
这段代码有几个问题。它应该更接近这一点:
<script type="text/javascript">
function myThing(){
// ** Do something useful here. This may involve calling
// ** other functions. Those functions need not be defined
// ** within jquery.lazyload.js.
}
$(document).ready(function() {
//instantiate a swipe object and pass in a bunch of
//options, especially "callback" which fires on every slide change.
// ** There is no need, as far as I can tell, to assign the
// ** output to a global variable. (window.mySwipe). In fact, given
// ** the code you have, there is no need to retain the output at all.
new Swipe(document.getElementById('slider1'), {
startSlide: 0,
speed: 1000,
auto: 5000,
// ** Just use the function name. You don't need to define
// ** a new anonymous function to wrap around it.
callback: myThing
});
$("img.lazy").lazyload({event: "click", effect : "fadeIn", threshold: 0,});
});
</script>
函数myThing(){
//**在这里做一些有用的事情。这可能包括打电话
//**其他功能。无需定义这些功能
//**在jquery.lazyload.js中。
}
$(文档).ready(函数(){
//实例化一个滑动对象并传入一组
//选项,尤其是每次幻灯片更改时触发的“回调”。
//**据我所知,没有必要指定
//**输出到全局变量。(window.mysweep)。事实上,给定
//**您拥有的代码,根本不需要保留输出。
新滑动(document.getElementById('slider1'),{
开始滑动:0,
速度:1000,
汽车:5000,
//**只需使用函数名,无需定义
//**一个新的匿名函数来包装它。
回调:神话
});
$(“img.lazy”).lazyload({事件:“单击”,效果:“fadeIn”,阈值:0,});
});
更多 我想您想要的是懒洋洋地加载“接下来的几张”图像,以便 它们在用户浏览列表时可用。据我所知 阅读(简而言之),对于所有图像,只需调用一次
lazyload()
在页面上。如果图像位于“容器”内,请指定
container
lazyload上的选项。(见
)该插件然后监视
该容器的视口,并根据需要加载图像。有
不需要再次调用“lazyload”,也不需要回调
如果这是
不工作,则Swipe和jquery.lazyload.js可能无法一起使用,
因为lazyload中的假设。在这种情况下,你仍然可以
获得延迟加载,但您需要自己完成
为此,完全消除lazyload.js。然后,在滑动回调中,执行您希望lazyload完成的操作:在“下一步”上设置src
属性
形象。设置该属性将导致网页加载
形象。要获得延迟加载,在原始状态下,所有
在刷卡之前,图像必须为空,或设置为某个固定的图像URL
开始。现在,您可能会问:在回调中,您如何知道要加载哪个图像?
以及在src属性中设置哪个URL?那由你来决定
决定我想有办法得到合适的答案
在该回调中滑动的信息
如果你不明白这一点,那么你需要一些指导或建议
进一步阅读以建立对浏览器如何工作的基本理解
处理图像。从我的阅读,你不需要刷卡呼叫
lazyload的update()
一些一般性建议:
- 对于任何模块,都不应尝试调用定义的函数 该模块的内部。它们是内部的,是有原因的
- 对于任何模块,都不应修改其源代码,除非 非常肯定你知道你在做什么。如果你在四处闲逛 尝试一些东西,把代码放在你自己的模块中。您自己的javascript代码属于该网页,或者属于该网页引用的单独模块
- 您可能不应该在中定义函数
$(
<script type="text/javascript"> function myThing(){ // ** Do something useful here. This may involve calling // ** other functions. Those functions need not be defined // ** within jquery.lazyload.js. } $(document).ready(function() { //instantiate a swipe object and pass in a bunch of //options, especially "callback" which fires on every slide change. // ** There is no need, as far as I can tell, to assign the // ** output to a global variable. (window.mySwipe). In fact, given // ** the code you have, there is no need to retain the output at all. new Swipe(document.getElementById('slider1'), { startSlide: 0, speed: 1000, auto: 5000, // ** Just use the function name. You don't need to define // ** a new anonymous function to wrap around it. callback: myThing }); $("img.lazy").lazyload({event: "click", effect : "fadeIn", threshold: 0,}); }); </script>