JavaScript 协助
我遇到的问题是当针对不同元素多次调用该函数时。 我相信我需要本地化所有变量,以便该函数可以用于多个元素。 如果重要的话我还会调用 jquery 1.3。
如果我只调用 pageSlide 一次,一切都很好,但是一旦我多次调用它,就会变得痛苦。 没有错误,只是不稳定的行为。
代码已更新
var slideMenu=function(){
var speed, startWidth, time, menuId, liID, menuLen, menuWidth, globalWidth, openWidth;
return{
speed : 0, startWidth : 0, time : 0, menuId : 0, liID : 0, menuLen : 0, menuWidth : 0, globalWidth : 0, openWidth : 0,
build:function(ulID,passStartWidth,passTime,s,passSlideLen,passHeight){
speed=s;
startWidth=passStartWidth;
time=passTime;
menuId=document.getElementById(ulID);
liID=menuId.getElementsByTagName('li');
menuLen=liID.length;
menuWidth=menuId.offsetWidth;
globalWidth=menuWidth/menuLen;
openWidth=Math.floor((menuWidth-startWidth)/(menuLen-1));
var i=0;
for(i;i<menuLen;i++){
s=liID[i];
s.style.width=globalWidth+'px';
this.timer(s)
}
if(passSlideLen!=null){
menuId.timer=setInterval(function(){
slideMenu.slide(liID[passSlideLen-1])
},time)
}
},
timer:function(s){
s.onmouseover=function(){
clearInterval(menuId.htimer);
clearInterval(menuId.timer);
menuId.timer=setInterval(function(){
slideMenu.slide(s)
},
time)
}
s.onmouseout=function(){
clearInterval(menuId.timer);
clearInterval(menuId.htimer);
menuId.htimer=setInterval(function(){
slideMenu.slide(s,true)
},
time)
}
},
slide:function(s,passChange){
var changeWidth=parseInt(s.style.width);
if((changeWidth<startWidth && !passChange) || (changeWidth>globalWidth && passChange)){
var overallWidth=0;
var i=0;
for(i;i<menuLen;i++){
if(liID[i]!=s){
var slideObj,openWidth; var opening=0; slideObj=liID[i]; openWidth=parseInt(slideObj.style.width);
if(openWidth<globalWidth && passChange){
opening=Math.floor((globalWidth-openWidth)/speed);
opening=(opening>0)?opening:1;
slideObj.style.width=(openWidth+opening)+'px';
}else if(openWidth>openWidth && !passChange){
opening=Math.floor((openWidth-openWidth)/speed);
opening=(opening>0)?opening:1;
slideObj.style.width=(openWidth-opening)+'px'
}
if(passChange){
overallWidth=overallWidth+(openWidth+opening)}else{overallWidth=overallWidth+(openWidth-opening)
}
}
}
s.style.width=(menuWidth-overallWidth)+'px';
}else{
clearInterval(menuId.timer);
clearInterval(menuId.htimer)
}
}
};
}();
上面的代码没有错误,但无法工作。 当我使用 this 关键字时,它并没有变得更好。
我的问题是哪些变量应该是“this”。 我尝试了各种我认为可行的组合,但我错过了一些东西。
The issue I am having is when the function is called multiple times for different elements. I believe I need to localize all of the variables such that this function can be used against multiple elements. In case it matters I also call in jquery 1.3.
If I only call pageSlide once, everything is fine, but once I call it multiple times it gets painful. No errors, just erratic behaviour.
The code has been updated
var slideMenu=function(){
var speed, startWidth, time, menuId, liID, menuLen, menuWidth, globalWidth, openWidth;
return{
speed : 0, startWidth : 0, time : 0, menuId : 0, liID : 0, menuLen : 0, menuWidth : 0, globalWidth : 0, openWidth : 0,
build:function(ulID,passStartWidth,passTime,s,passSlideLen,passHeight){
speed=s;
startWidth=passStartWidth;
time=passTime;
menuId=document.getElementById(ulID);
liID=menuId.getElementsByTagName('li');
menuLen=liID.length;
menuWidth=menuId.offsetWidth;
globalWidth=menuWidth/menuLen;
openWidth=Math.floor((menuWidth-startWidth)/(menuLen-1));
var i=0;
for(i;i<menuLen;i++){
s=liID[i];
s.style.width=globalWidth+'px';
this.timer(s)
}
if(passSlideLen!=null){
menuId.timer=setInterval(function(){
slideMenu.slide(liID[passSlideLen-1])
},time)
}
},
timer:function(s){
s.onmouseover=function(){
clearInterval(menuId.htimer);
clearInterval(menuId.timer);
menuId.timer=setInterval(function(){
slideMenu.slide(s)
},
time)
}
s.onmouseout=function(){
clearInterval(menuId.timer);
clearInterval(menuId.htimer);
menuId.htimer=setInterval(function(){
slideMenu.slide(s,true)
},
time)
}
},
slide:function(s,passChange){
var changeWidth=parseInt(s.style.width);
if((changeWidth<startWidth && !passChange) || (changeWidth>globalWidth && passChange)){
var overallWidth=0;
var i=0;
for(i;i<menuLen;i++){
if(liID[i]!=s){
var slideObj,openWidth; var opening=0; slideObj=liID[i]; openWidth=parseInt(slideObj.style.width);
if(openWidth<globalWidth && passChange){
opening=Math.floor((globalWidth-openWidth)/speed);
opening=(opening>0)?opening:1;
slideObj.style.width=(openWidth+opening)+'px';
}else if(openWidth>openWidth && !passChange){
opening=Math.floor((openWidth-openWidth)/speed);
opening=(opening>0)?opening:1;
slideObj.style.width=(openWidth-opening)+'px'
}
if(passChange){
overallWidth=overallWidth+(openWidth+opening)}else{overallWidth=overallWidth+(openWidth-opening)
}
}
}
s.style.width=(menuWidth-overallWidth)+'px';
}else{
clearInterval(menuId.timer);
clearInterval(menuId.htimer)
}
}
};
}();
The code above does not error but fails to work. When I use the this keyword, it doesn't get any better.
My question is which variables should be "this.". I have tried various combinations that I thought would work, but I am missing something.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您误解了模块模式的总体概念,当您需要在所有实例之间共享全局静态变量时,您可以在 return 语句之前使用关键字“var”声明或初始化变量。
假设每当您更改这些变量时,您的所有实例都会受到更改的影响。
另一方面,如果您想保证变量在全局范围内安全,则必须将变量放入函数返回的对象中,这是提供每个实例唯一属性的可靠方法。
结论
有时,为了帮助区分内部/公共属性,有些人会在其内部属性上写一个前导下划线(请注意,属性仍然可以从外部访问)。
希望它有帮助,祝你好运!
I think you're misunderstanding the overall concept of the module pattern, when you need to have a global static variable shared between all your instances then you can declare or initialize your variable with the keyword "var" just before the return statement.
Assume that whenever you change those variables all your instances will be impacted by the change.
On the other hand if you want to keep variable safe from the global scope, you have to put your variable into the object returned by your function, this is a reliable way to provide unique per instance properties.
To conclude
Sometimes to help distinguish between internal / public properties some folks writes a leading underscore on their internal properties (note that properties will still remain accessible from the outside).
Hope it helps, good luck!
您之所以会出现这种行为,是因为您使用的模块模式允许在对象中保存私有静态变量。 问题是这些变量在所有实例之间共享。
您必须将这些变量移至您的公共实例中,即脚本的返回部分或构造函数中(但在这种情况下,您必须为每个变量提供一个 getter)。
You get this behavior because you are using the module pattern which allows to hold private static variables in your object. The problem is that those variables are shared between all instances.
You have to move these variables into your public instance aka in the return part of your script or in the constructor (but in this case you will have to provide a getter for every variable).