사용자가 j 키를 누를 때마다 div로 스크롤하고 싶습니다. 여기에 대한 코드가 있습니다.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의 offest에서 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