【发布时间】:2015-04-23 15:19:19
【问题描述】:
我的网站上有一个垂直导航点击功能,在 Firefox、IE10 和 IE9 中运行良好,但在 Chrome 或 Safari 中无法运行。检查页面时,我没有收到任何 chrome 错误。
我从 FF 和 Chrome 中获得的原始代码,但由于我已经分叉了它,因此该代码在 Chrome 中不再起作用。
原始代码: Scrolling to the next element
jsFiddle 原文:http://jsfiddle.net/Pm3cj/3/
我的 Jsfiddle:https://jsfiddle.net/hjb6631d/1/
HTML:
<div style="height: 5000px">
<section class="cover" style="height: 100px; border: 1px solid black; margin-bottom: 10px;">
<section class="controls">
<p class="prev">prev</p>
<p class="next">next</p>
1
</section>
</section>
<section class="cover" style="height: 100px; border: 1px solid black; margin-bottom: 10px;">
<section class="controls">
<p class="prev">prev</p>
<p class="next">next</p>
2
</section>
</section>
<section class="cover" style="height: 100px; border: 1px solid black; margin-bottom: 10px;">
<section class="controls">
<p class="prev">prev</p>
<p class="next">next</p>
3
</section>
</section>
</div>
脚本:
$('.cover:first').addClass('first-child');
$('.cover:last').addClass('last-child');
$(".next, .prev").on("click", function(e) {
// the container that will be scrolled to
var target = '.cover';
container = $(this).parent();
// am I the last .container in my group?
while (document != container[0] && container.find('~'+target, '~:has('+target+')').length == 0)
container = container.parent(); // if so, search siblings of parent instead
prevdiv = container.prevAll(target, ':has('+target+')').first();
nextdiv = container.nextAll(target, ':has('+target+')').first();
// if clicked next object
if ( $(this).hasClass('next') ) {
// no next .container found, go back to first container
if (nextdiv.length==0) nextdiv = $(document).find(target+':first');
//$(document).scrollTop(nextdiv.offset().top);
$('html').animate({scrollTop:nextdiv.offset().top},300);
}
// if clicked prev
if ( $(this).hasClass('prev') ) {
// no next .container found, go back to first container
// scroll to previous element
prevdiv = $(this).parents(target).prev(target);
// if is first element on page, then scroll to last element
if ( $(this).parents(target).hasClass('first-child') ) {
prevdiv = $(document).find(target+':last');
};
//$(document).scrollTop(nextdiv.offset().top);
$('html').animate({scrollTop:prevdiv.offset().top},300);
}
// $(this).parent().next() // this is the next div container.
return false;
});
【问题讨论】:
标签: javascript jquery html css google-chrome