0
我在我的網站上集成了鍵盤導航。當訪客按下頁面上的「向上」鍵或「底部」鍵時,將自動滾動到不同的標題上。使代碼與最新版本的jQuery兼容
問題是此代碼只與jQuery 1.7.2(和更低版本)兼容,並且我需要使用其他代碼來實現更新的版本。
我需要修改此代碼以使其與最新版本的jQuery兼容?
按鍵盤上的上下箭頭:http://pepitodanger.free.fr/Maquette/
預先感謝您。
function scroll(direction) {
var scroll, i,
positions = [],
here = $(window).scrollTop(),
collection = $('.data-scroll');
collection.each(function() {
positions.push(parseInt($(this).offset()['top'],10));
});
for(i = 0; i < positions.length; i++) {
if (direction == 'next' && positions[i] > here) {
scroll = collection.get(i);
break;
}
if (direction == 'prev' && i > 0 && positions[i] >= here) {
scroll = collection.get(i-1);
break;
}
}
if (scroll) {
$.scrollTo(scroll, {
duration: 700
});
}
return false;
}
$(function() {
$("#next,#prev").click(function() {
return scroll($(this).attr('id'));
});
});
$(window).keydown (function(event) {
if (event.altKey) {
switch (event.which) {
case 78: // Alt-N = next
case 110: // Alt-n = next
scroll ('next');
break;
case 80: // Alt-P = prev
case 112: // Alt-p = prev
scroll ('prev');
break;
}
}
else {
switch (event.keyCode) {
case 37: // key is left
case 38: // key is up
scroll ('prev');
break;
case 39: // key is right
case 40: // key is down
scroll ('next');
break;
}
}
});
您是否在較新版本的jQuery中測試過它?你看到了什麼錯誤?你看過[jQuery 1.9升級指南](http://jquery.com/upgrade-guide/1.9/)和[migrate plugin](https://github.com/jquery/jquery-migrate/#readme) ? – IMSoP