Javascript 模块内的简单函数调用,得到NaN,是吗?

Javascript 模块内的简单函数调用,得到NaN,是吗?,javascript,nan,Javascript,Nan,以下是我正在学习的模块: var FeatureRotator = (function($,global) { var self = {}, currentFeature = 0, images = [], imagePrefix = "/public/images/features/", timer = null, totalImages = 0, initialFeatur

以下是我正在学习的模块:

var FeatureRotator = (function($,global) {
    var self = {},
        currentFeature = 0,
        images = [],
        imagePrefix = "/public/images/features/",
        timer = null,        
        totalImages = 0,
        initialFeature,
        interval,
        blendSpeed,
        element = null,
        img1 = null,
        img2 = null;

    function setVisibleImage(iid) {
        $("#img1").attr('src',images[iid].src).css('opacity',1);
        $("#img2").css('opacity',0);
        $(".active").removeClass("active");
        $("#f"+iid).addClass("active");
    }

    function setCurrentImage(id) {
        currentFeature = id;
        setVisibleImage(id);
    }

    function doHoverIn(position) {
        if (currentFeature === position) {
            self.pause();
        } else {
            setCurrentImage(global.parseInt(position, 10));
            self.pause();
        }
    }

    function doHoverOut(position) {
        self.unpause();
    }

    self.init = function(options,callback) {
        var i = 0,
            tempImg = null;

        interval = options.interval || 5000;
        blendSpeed = options.blendSpeed || 500;
        element = options.element;
        initialFeature = options.initialFeature || 0;
        img1 = $("<img/>").attr('id','img1');
        img2 = $("<img/>").attr('id','img2').css('opacity','0').css('margin-top',-options.height);

        $(element).append(img1).append(img2);

        totalImages = $(".feature").size();

        for (i = 0;i < totalImages; i++) {
            tempImg = new global.Image();
            tempImg.src = imagePrefix +"feature_" + i + ".png";
            images.push(tempImg);


            $("#f"+i).css('background-image',
                            'url("'+imagePrefix+"feature_"+i+"_thumb.png"+'")')
                     .hover(doHoverIn($(this).attr('position'))
                     , doHoverOut($(this).attr('position'))
                     ).attr('position',i);
        }

        setVisibleImage(initialFeature);

        if (options.autoStart) {
            self.start();
        }
        if (callback !== null) {
            callback();
        }
    };

    function updateImage() {        
        var active = $("#img1").css('opacity') === 1 ? "#img1" : "#img2";
        var nextFeature = (currentFeature === totalImages-1 ? 0 : currentFeature+1);

        if (active === "#img1") {
            $("#img2").attr('src',images[nextFeature].src);            
            $("#img2").fadeTo(blendSpeed, 1);            
            $("#img1").fadeTo(blendSpeed, 0);
        } else {
            $("#img1").attr('src',images[nextFeature].src);            
            $("#img1").fadeTo(blendSpeed, 1);            
            $("#img2").fadeTo(blendSpeed, 0);
        }

        $("#f"+currentFeature).removeClass("active");
        $("#f"+nextFeature).addClass("active");

        currentFeature = nextFeature;
    }



    self.start = function() {
        currentFeature = initialFeature;
        setVisibleImage(currentFeature);
        timer = global.setInterval(function(){
            updateImage();
        }, interval);
    };        

    self.pause = function() {
        global.clearTimeout(timer);
    };

    self.unpause = function() {
        timer = global.setInterval(function(){
            updateImage();
        }, interval);
    };


    return self;
}(this.jQuery, this));
var-FeatureRotator=(函数($,全局){
var self={},
currentFeature=0,
图像=[],
imagePrefix=“/public/images/features/”,
计时器=空,
totalImages=0,
初始特征,
间隔
混合速度,
元素=空,
img1=null,
img2=null;
函数设置VisibleImage(iid){
$(“#img1”).attr('src',images[iid].src).css('opacity',1);
$(“#img2”).css('opacity',0);
$(.active”).removeClass(“active”);
$(“#f”+iid).addClass(“活动”);
}
函数setCurrentImage(id){
currentFeature=id;
设置可视图像(id);
}
功能doHoverIn(位置){
如果(currentFeature==位置){
self.pause();
}否则{
setCurrentImage(global.parseInt(位置,10));
self.pause();
}
}
功能输出(位置){
self.unpose();
}
self.init=函数(选项、回调){
var i=0,
tempImg=null;
间隔=选项。间隔| | 5000;
blendSpeed=options.blendSpeed | | 500;
element=options.element;
initialFeature=options.initialFeature | | 0;
img1=$(”
下面是如何在页面上使用它:

<script type="text/javascript">
        // ...

        $(function() {
            FeatureRotator.init({
                interval:5000,
                element:'#intro',
                autoStart:true,
                height:177,
                blendSpeed:1000,
                initialFeature:0
            });
        });
    </script>

// ...
$(函数(){
FeatureRotator.init({
间隔时间:5000,
元素:“#简介”,
自动启动:对,
身高:177,
混合速度:1000,
initialFeature:0
});
});
问题是,当从init方法调用setVisibleImage时,iid的值是NaN。我已经通过调试器验证了调用setVisibleImage函数时'initialFeature'是0,但遗憾的是,该值没有出现在那里

有人能帮我确定问题出在哪里吗?我已经通过JSLint运行了代码,结果没有问题

更新

好的,这是我的更新代码,它现在可以工作了,只是褪色不起作用,图像只是翻转到下一个,不再平滑褪色:

var FeatureRotator = (function($,global) {
    var self = {},
        currentFeature = 0,
        images = [],
        imagePrefix = "/public/images/features/",
        timer = null,        
        totalImages = 0,
        initialFeature = 0,
        interval,
        blendSpeed;


    function setVisibleImage(iid) {
        $("#img1").attr('src',images[iid].src).css('opacity',1);
        $("#img2").css('opacity',0);
        $(".active").removeClass("active");
        $("#f"+iid).addClass("active");
    }

    function setCurrentImage(id) {
        currentFeature = id;
        setVisibleImage(id);
    }

    function doHoverIn(obj) {
        var position = global.parseInt(obj.target.attributes["position"].value,10);

        if (currentFeature === position) {
            self.pause();
        } else {
            setCurrentImage(global.parseInt(position, 10));
            self.pause();
        }
    }

    function doHoverOut() {
        self.unpause();
    }

    self.init = function(options,callback) {
        var i = 0,
            tempImg = null,
            element = null,
            img1 = null,
            img2 = null;

        interval = options.interval || 5000;
        blendSpeed = options.blendSpeed || 500;
        element = options.element;
        initialFeature = options.initialFeature || 0;
        img1 = $("<img/>").attr('id','img1');
        img2 = $("<img/>").attr('id','img2').css('opacity','0').css('margin-top',-options.height);

        $(element).append(img1).append(img2);

        totalImages = $(".feature").size();

        for (i = 0;i < totalImages; i++) {
            tempImg = new global.Image();
            tempImg.src = imagePrefix +"feature_" + i + ".png";
            images.push(tempImg);


            $("#f"+i).css('background-image','url("'+imagePrefix+"feature_"+i+"_thumb.png"+'")')
                     .hover(doHoverIn, doHoverOut)
                     .attr('position',i);
        }

        setVisibleImage(initialFeature);

        if (options.autoStart) {
            self.start();
        }
        if (typeof callback === "function") {
            callback();
        }
    };

    function updateImage() {        
        var active = $("#img1").css('opacity') === 1 ? "#img1" : "#img2";
        var nextFeature = (currentFeature === totalImages-1 ? 0 : currentFeature+1);

        if (active === "#img1") {
            $("#img2").attr('src',images[nextFeature].src);            
            $("#img2").fadeTo(blendSpeed, 1);            
            $("#img1").fadeTo(blendSpeed, 0);
        } else {
            $("#img1").attr('src',images[nextFeature].src);            
            $("#img1").fadeTo(blendSpeed, 1);            
            $("#img2").fadeTo(blendSpeed, 0);
        }

        $("#f"+currentFeature).removeClass("active");
        $("#f"+nextFeature).addClass("active");

        currentFeature = nextFeature;
    }



    self.start = function() {
        currentFeature = initialFeature;
        setVisibleImage(currentFeature);
        timer = global.setInterval(function(){
            updateImage();
        }, interval);
    };

    self.stop = function() {
        global.clearTimeout(timer);
    };

    self.pause = function() {
        global.clearTimeout(timer);
    };

    self.unpause = function() {
        timer = global.setInterval(function(){
            updateImage();
        }, interval);
    };


    return self;
}(this.jQuery, this));
var-FeatureRotator=(函数($,全局){
var self={},
currentFeature=0,
图像=[],
imagePrefix=“/public/images/features/”,
计时器=空,
totalImages=0,
initialFeature=0,
间隔
混合速度;
函数设置VisibleImage(iid){
$(“#img1”).attr('src',images[iid].src).css('opacity',1);
$(“#img2”).css('opacity',0);
$(.active”).removeClass(“active”);
$(“#f”+iid).addClass(“活动”);
}
函数setCurrentImage(id){
currentFeature=id;
设置可视图像(id);
}
函数doHoverIn(obj){
var position=global.parseInt(obj.target.attributes[“position”].value,10);
如果(currentFeature==位置){
self.pause();
}否则{
setCurrentImage(global.parseInt(位置,10));
self.pause();
}
}
函数doHoverOut(){
self.unpose();
}
self.init=函数(选项、回调){
var i=0,
tempImg=null,
元素=空,
img1=null,
img2=null;
间隔=选项。间隔| | 5000;
blendSpeed=options.blendSpeed | | 500;
element=options.element;
initialFeature=options.initialFeature | | 0;

img1=$(“因为您正在获取
NaN
,我猜它实际上是从这行发生的:

.hover(doHoverIn($(this).attr('position'))
…这就叫:

setCurrentImage(global.parseInt(position, 10)); // note the parseInt()
setVisibleImage(id);
…这就叫:

setCurrentImage(global.parseInt(position, 10)); // note the parseInt()
setVisibleImage(id);
因此,传递给
parseInt
位置
来自
$(this).attr('position')
,这可能是一个无法解析为数字的值,因此得到
NaN

检查
for
语句块第一行中该属性的值

for (i = 0;i < totalImages; i++) {
    console.log( $(this).attr('position') ); // verify the value of position
    // ...
for(i=0;i
在调用
setVisibleImage()之前,您是否检查了
initialFeature
的值
?好的,大部分情况下,它工作正常,更新了我的代码,现在又出现了另一个问题,如果你能看看发生了什么?我可以点击那些fadeTo方法,混合速度值是正确的。我发现了……updateImage中的“active”变量总是设置为#img2,因为我使用的是==,css值是com作为字符串而不是整数输入,我将其更改为==并再次工作,这是在jslint告诉我更改它之前的方式,这是有意义的。我应该将不透明度值解析为整数,以便我知道我正在比较苹果。