调试从 jQuery 1.4.4 到 1.6.4 的代码 - 因升级而损坏
好的,提前为发布大量代码表示歉意!
我的问题是这样的:我不是 jQuery 小开发周期方面的专家,任何人都可以发现这段代码中的任何内容会使其与 jQuery 1.6.4 不兼容吗?
该代码最初是为 jQuery 1.4.4 编写的,因此我正在寻找 1.4.4 和 1.6.4 之间会影响以下代码的任何更改:
$(document).ready(function() { //when the document is ready...
//save selectors as variables to increase performance
var $window = $(window);
var $firstBG = $('#first');
var $firstGrid = $('#first .grid');
var $secondBG = $('#second');
var $thirdBG = $('#third');
var $fourthBG = $('#fourth');
var trainers = $("#second .bg");
var windowHeight = $window.height(); //get the height of the window
//apply the class "inview" to a section that is in the viewport
$('#first, #second, #third, #fourth').bind('inview', function (event, visible) {
if (visible == true) {
$(this).addClass("inview");
} else {
$(this).removeClass("inview");
}
});
//function that is called for every pixel the user scrolls. Determines the position of the background
/*arguments:
x = horizontal position of background
windowHeight = height of the viewport
pos = position of the scrollbar
adjuster = adjust the position of the background
inertia = how fast the background moves in relation to scrolling
*/
function newPos(x, windowHeight, pos, adjuster, inertia){
return x + "% " + (-((windowHeight + pos) - adjuster) * inertia) + "px";
}
//function to be called whenever the window is scrolled or resized
function Move(){
var pos = $window.scrollTop(); //position of the scrollbar
//if the first section is in view...
if($firstBG.hasClass("inview")){
//call the newPos function and change the background position
$firstBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 750, 0.3)});
}
//if the second section is in view...
if($secondBG.hasClass("inview")){
//call the newPos function and change the background position
$secondBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 1250, 0.3)});
//call the newPos function and change the secnond background position
trainers.css({'backgroundPosition': newPos(50, windowHeight, pos, 1900, 0.6)});
}
//if the third section is in view...
if($thirdBG.hasClass("inview")){
//call the newPos function and change the background position
$thirdBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 2850, 0.3)});
}
//if the fourth section is in view...
if($fourthBG.hasClass("inview")){
//call the newPos function and change the background position for CSS3 multiple backgrounds
$fourthBG.css({'backgroundPosition': newPos(0, windowHeight, pos, 200, 0.9) + ", " + newPos(50, windowHeight, pos, 0, 0.7) + ", " + newPos(50, windowHeight, pos, 0, 0.5) + ", " + newPos(50, windowHeight, pos, 700, 0.3)});
}
$('#pixels').html(pos); //display the number of pixels scrolled at the bottom of the page
}
$window.resize(function(){ //if the user resizes the window...
Move(); //move the background images in relation to the movement of the scrollbar
});
$window.bind('scroll', function(){ //when the user is scrolling...
Move(); //move the background images in relation to the movement of the scrollbar
});
});
OK, apologies in advance for posting a huge chunk of code!
My problem is this: I'm not an expert on the small development cycles of jQuery, can anyone spot anything in this code that would make it incompatible with jQuery 1.6.4?
The code was originally written for jQuery 1.4.4 so I'm looking for any changes between 1.4.4 and 1.6.4 that would affect the following code:
$(document).ready(function() { //when the document is ready...
//save selectors as variables to increase performance
var $window = $(window);
var $firstBG = $('#first');
var $firstGrid = $('#first .grid');
var $secondBG = $('#second');
var $thirdBG = $('#third');
var $fourthBG = $('#fourth');
var trainers = $("#second .bg");
var windowHeight = $window.height(); //get the height of the window
//apply the class "inview" to a section that is in the viewport
$('#first, #second, #third, #fourth').bind('inview', function (event, visible) {
if (visible == true) {
$(this).addClass("inview");
} else {
$(this).removeClass("inview");
}
});
//function that is called for every pixel the user scrolls. Determines the position of the background
/*arguments:
x = horizontal position of background
windowHeight = height of the viewport
pos = position of the scrollbar
adjuster = adjust the position of the background
inertia = how fast the background moves in relation to scrolling
*/
function newPos(x, windowHeight, pos, adjuster, inertia){
return x + "% " + (-((windowHeight + pos) - adjuster) * inertia) + "px";
}
//function to be called whenever the window is scrolled or resized
function Move(){
var pos = $window.scrollTop(); //position of the scrollbar
//if the first section is in view...
if($firstBG.hasClass("inview")){
//call the newPos function and change the background position
$firstBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 750, 0.3)});
}
//if the second section is in view...
if($secondBG.hasClass("inview")){
//call the newPos function and change the background position
$secondBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 1250, 0.3)});
//call the newPos function and change the secnond background position
trainers.css({'backgroundPosition': newPos(50, windowHeight, pos, 1900, 0.6)});
}
//if the third section is in view...
if($thirdBG.hasClass("inview")){
//call the newPos function and change the background position
$thirdBG.css({'backgroundPosition': newPos(50, windowHeight, pos, 2850, 0.3)});
}
//if the fourth section is in view...
if($fourthBG.hasClass("inview")){
//call the newPos function and change the background position for CSS3 multiple backgrounds
$fourthBG.css({'backgroundPosition': newPos(0, windowHeight, pos, 200, 0.9) + ", " + newPos(50, windowHeight, pos, 0, 0.7) + ", " + newPos(50, windowHeight, pos, 0, 0.5) + ", " + newPos(50, windowHeight, pos, 700, 0.3)});
}
$('#pixels').html(pos); //display the number of pixels scrolled at the bottom of the page
}
$window.resize(function(){ //if the user resizes the window...
Move(); //move the background images in relation to the movement of the scrollbar
});
$window.bind('scroll', function(){ //when the user is scrolling...
Move(); //move the background images in relation to the movement of the scrollbar
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您绝对应该检查控制台。
在 Safari 或 Chrome 中,右键单击页面内的任意位置并选择“检查元素”。这将带来开发人员工具,即使您不是专家,您也会发现它们非常有用。
然后转到控制台,您将看到是否发生任何错误。使用 jQuery,如果应该发生某些事情但没有发生,并且控制台中没有错误,则几乎总是意味着您通过 id 或调用引用了不存在的 html 标签。如果您执行类似 $('#divIdName').hide() 的操作,但您有 < div id="divIdName_slightlyDiffent">在你的 DOM 中,该行不起作用,但也不会抛出任何错误。
我在您的代码中没有看到任何已弃用的函数,因此您应该真正检查控制台和拼写错误。如果您所做的唯一更改是 jQuery 版本,则可能某些函数的某些内部行为发生了更改,但如果是这种情况,您应该保持警惕。
You should definatelly check the console.
In Safari or Chrome right click anywhere inside the page and select Inspect element. That will bring the developer tools which you'll find very helpful even if you're not an expert.
Then go to the console and you'll see if any error happened. With jQuery if something was supposed to happen but didn't and there's no error in the console, it almost always means you're referring to an html tag by id or call which doesn't exist. If you do something like $('#divIdName').hide() but you have < div id="divIdName_slightlyDiffent"> in your DOM, that line won't work but won't throw any error either.
I don't see any deprecated function in your code so you should really check the console and typos. If the only change you did was the jQuery version maybe some internal behavior of some function changed but if that's the case you should have some alert.
对于这个特定问题,插件
jQuery Parallax
已过时。更新到 jQuery parallax v1.1 解决了这个问题,但作者已经删除了它(我认为)。如果您正在使用该插件,这对明智的人来说是一句话。In the case of this particular issue, the plugin
jQuery Parallax
was outdated. Updating to jQuery parallax v1.1 fixed the issue but the author has since removed it (I think). So that's a word to the wise if you are using the plugin.首先,您有一些简单的语法错误。
缺少结束括号
)
。另外,我建议用
attr()
、prop()
或val()
替换一些.css()
>。First of all, you have some simple syntax errors.
has a missing end parenthesis
)
.Also, I suggest to replace some
.css()
withattr()
,prop()
orval()
.