我想滾動到div,每次用戶按下j鍵。這是它的代碼。使用jquery從一個整數中減去
$(function() {
function scroll(direction) {
var scroll, i,
positions = [],
here = $(window).scrollTop(),
collection = $('.message_box');
collection.each(function() {
positions.push(parseInt($(this).offset()['top'],0));
});
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); break; }
}
if (scroll) {
$('html, body').animate({"scrollTop": $(scroll).offset().top-50});
$(scroll).css('color', 'blue');
$(scroll).mouseleave(function() {
$(this).css('color', 'black');
});
}
return false;
}
$("#next,#prev").click(function() {
return scroll($(this).attr('id'));
});
$('body').keyup(function(event) {
if (event.which == 74) {
return scroll('next');
}
});
$('body').keyup(function(event) {
if (event.which == 75) {
return scroll('prev');
}
});
});
我需要從div的後感減去50滾動到這是這是。
$('html, body').animate({"scrollTop": $(scroll).offset().top-50});
它會滾動第一次,但不是其他時間。我總是得到整數這是第一個div要滾動的偏移量。 DEMO - http://jsfiddle.net/XP5sP/6/ 有人可以幫助我嗎?
你能比「它不工作」更具體嗎?到底發生了什麼事情,這不是你想要的或期望的事情,以及你希望/期望的是什麼? –
另外,任何[jsFiddle](http://jsfiddle.net)的機會都可以證明問題? –
@AnthonyGrist http://jsfiddle.net/XP5sP/3/ – Subin