2013-03-23 53 views
-1

我有下面的代碼在所有瀏覽器包括Firefox正常工作:jQuery的斷裂在Firefox,但沒有其他的瀏覽器

var top; 
var imageHeight; 
function upFunction() { 
    top = parseInt($(".feature img").css("top")); 
    imageHeight = $(".feature img").height(); 
    if (top > (imageHeight - 335) * -1) { 
     $(".feature img").css("top", top - 1 + "px"); 
     $("#topPos").val(top - 1); 
    } 
}; 
$(document).ready(function() { 
$("a#up").mousedown(function(e) { 
     e.preventDefault(); 
     upFunction(); 
     timeoutId = setInterval(upFunction, 100); 
    }).bind('mouseup mouseleave', function() { 
     clearInterval(timeoutId); 
    }); 
}); 

但是有明顯別人在我的網頁的東西,這是造成這不是工作在Firefox和我無法解決這個問題。

有沒有什麼辦法可以找出導致它破裂的原因,而不刪除頁面上的所有內容來找出它是什麼?

編輯:

OK,用潔的答案的幫助下,似乎一個小的調整,我的代碼使得它工作在Firefox。以下是我所做的:

function upFunction() { 
    var top = parseInt($(".feature img").css("top")); 
    var imageHeight = $(".feature img").height(); 
    if (top > (imageHeight - 335) * -1) { 
     $(".feature img").css("top", top - 1 + "px"); 
     $("#topPos").val(top - 1); 
    } 
}; 

並從全局範圍中移除2個變量。不知道爲什麼它做了一個不同的,但它做到了。

+1

你是否有Firebug,如果是,控制檯中顯示的錯誤是什麼?如果沒有,得到它,然後看到錯誤。 – Amar 2013-03-23 15:09:12

+3

究竟「破」是什麼意思?哪部分不工作? – JJJ 2013-03-23 15:09:13

+2

*「有沒有什麼方法可以找出導致它中斷的原因,而無需從頁面中刪除所有內容以找出它是什麼?」*是的,使用FF的開發人員工具或Firebug,查看控制檯,設置斷點,檢查變量......只是普通的JavaScript調試。 – 2013-03-23 15:12:09

回答

0

嘗試這些固定變量聲明:

function upFunction() { 
    var img = $(".feature img"), 
     top = parseInt(img.css("top")); 
    if (top > (335 - img.height())) { 
     img.css("top", top - 1 + "px"); 
     $("#topPos").val(top - 1); 
    } 
}; 
$(document).ready(function() { 
    var timeoutId; 
    $("a#up").mousedown(function(e) { 
     e.preventDefault(); 
     upFunction(); 
     timeoutId = setInterval(upFunction, 100); 
    }).bind('mouseup mouseleave', function() { 
     clearInterval(timeoutId); 
    }); 
}); 

您的實際問題:通過在開發工具(螢火蟲)檢查它調試腳本。

0

你應該試試這個:

function upFunction(top, imageHeight) { 
    if (top > (imageHeight - 335) * -1) { 
     $(".feature img").css("top", top - 1 + "px"); 
     $("#topPos").val(top - 1); 
    } 
} 
$(document).ready(function() { 
    var top = parseInt($(".feature img").css("top")); 
    var imageHeight = $(".feature img").height(); 
    $("a#up").mousedown(function(e) { 
     e.preventDefault(); 
     upFunction(top, imageHeight); 
     timeoutId = setInterval(function(){ 
         upFunction(top, imageHeight); 
        }, 100); 
    }).bind('mouseup mouseleave', function() { 
     clearInterval(timeoutId); 
    }); 
}); 
0

不要使用top在全球範圍變量名稱,它已被使用。

嘗試在您的瀏覽器控制檯中輸入console.log(top)alert(window.top)以供參考。

相關問題