gpt4 book ai didi

JavaScript 协助

转载 作者:行者123 更新时间:2023-11-30 23:52:49 26 4
gpt4 key购买 nike

我遇到的问题是当针对不同元素多次调用该函数时。我相信我需要本地化所有变量,以便该函数可以用于多个元素。如果重要的话我还会调用 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.”。我尝试了各种我认为可行的组合,但我错过了一些东西。

最佳答案

我认为您误解了模块模式的整体概念,当您需要在所有实例之间共享全局静态变量时,您可以在 return 语句之前使用关键字“var”声明或初始化变量.

假设每当您更改这些变量时,您的所有实例都会受到更改的影响

var slideMenu=function(){
// Declare or initialize your private static variables here
var speed, startWidth, time;

return {
// Public part of your object
slide:function(s,passChange){
//To access your variable
speed = 20;
...

另一方面,如果您想保证变量在全局范围内安全,则必须将变量放入函数返回的对象中,这是提供每个实例唯一属性.

var slideMenu=function(){
// Declare or initialize your private static variables here
var speed, startWidth, time;

return {
// Public part of your object
// Declare internal or public properties here
menuId : 0,
liID : 0,
menuLen : 0,
...
slide:function(s,passChange){
//To access your private static variable inside functions
speed = 20;
// To access your public/internal properties
this.menuId = s; // for example ;)
}

结论

有时,为了帮助区分内部/公共(public)属性,有些人会在其内部属性上写一个前导下划线(请注意,属性仍然可以从外部访问)。

希望对你有帮助,祝你好运!

关于JavaScript 协助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/939628/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com