jQuery:点击滚动到下一节
jQuery: click to scroll to next section
我设置了一个函数,当您单击.section-down-arrow-wrap
时,它将遍历祖先并找到最近的.fullscreen
元素,其想法是当您单击它时,它会将.section
元素向下滚动到.fullscreen
的下一个实例,您可以看到这是否是一个小提琴:https://jsfiddle.net/neal_fletcher/d9zbw1k2/它没有按预期工作,有些人把你滚动到下一个,有些人没有,有些人向上滚动。我也需要一个方法,它可以在树的更上层找到fullscreen
,因为它并不总是section-down-arrow-wrap
的祖父母元素。这是标记:
HTML:
<div class="section">
<div class="fullscreen"> <span>
<div class="section-down-arrow-wrap scroller">CLICK</div>
</span>
</div>
<div class="fullscreen">
<div class="half-screen">
<div class="section-down-arrow-wrap scroller">CLICK</div>
</div>
</div>
<div class="fullscreen"> <span>
<div class="section-down-arrow-wrap scroller">CLICK</div>
</span>
</div>
<div class="fullscreen">
<div class="half-screen">
<div class="section-down-arrow-wrap scroller">CLICK</div>
</div>
</div>
</div>
jQuery:
$(document).ready(function () {
$('.section-down-arrow-wrap.scroller').on('click', function () {
var fuller = $(this).closest('.fullscreen').next('.fullscreen'),
section = $(this).closest('.section');
section.animate({
scrollTop: fuller.offset().top + 0
}, 700);
});
});
如有任何建议,我们将不胜感激!
您必须在计算中包括滚动的当前垂直位置.scrollTop()
$('.scroller').click(function(){
var fuller = $(this).closest('.fullscreen').next(),
section = $(this).closest('.section');
section.animate({
scrollTop: section.scrollTop() + fuller.offset().top
}, 700);
});
JSFiddle
您需要为此执行$(section).scrollTop() + fuller.offset().top
。
$(document).ready(function () {
$('.section-down-arrow-wrap.scroller').on('click', function () {
var fuller = $(this).closest('.fullscreen').next('.fullscreen'),
section = $(this).closest('.section');
section.animate({
scrollTop: $(section).scrollTop() + fuller.offset().top + 0
}, 700);
});
});
.section {
position: fixed;
width: 100%;
height: 100%;
top: 0;
left: 0;
overflow: scroll;
}
.fullscreen {
position: relative;
width: 100%;
height: 400px;
background: orange;
}
.fullscreen:nth-child(even) {
background: blue;
}
.section-down-arrow-wrap {
cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="section">
<div class="fullscreen"> <span>
<div class="section-down-arrow-wrap scroller">CLICK</div>
</span>
</div>
<div class="fullscreen">
<div class="half-screen">
<div class="section-down-arrow-wrap scroller">CLICK</div>
</div>
</div>
<div class="fullscreen">
<span>
<div class="section-down-arrow-wrap scroller">CLICK</div>
</span>
</div>
<div class="fullscreen">
<div class="half-screen">
<div class="section-down-arrow-wrap scroller">CLICK</div>
</div>
</div>
</div>
$(document).ready(function(){
$("#button").click(function() {
$('html, body').animate({
scrollTop: $("#scroll").offset().top
}, 1000);
});
});
button是一个可以点击的id
scroll是is滚动到
的位置相关文章:
- Javascript一页滚动脚本滚动到每个点击的链接一个接一个
- HTML5自动滚动到下一节
- 我只上一节课,而不是两节课
- 一页滚动自定义导航
- jQuery mMenu如何通过单击某些元素返回到上一节
- 整页.JS一页滚动在手机上不起作用
- 一页滚动iPad触摸滚动
- 我们可以使用 iscroller 一次滚动固定的 3 个列表项项目吗?
- 整页.js:如何自动添加下一节或上一节的名称
- 逐节滚动到
- Javascript手风琴一节打开
- jQuery Tools 可滚动:屏幕上有 3 个项目,但一次滚动一个
- 类似苹果的网站逐节滚动行为
- 如何指定每一步滚动移动的像素数
- 整页js幻灯片下一节超过当前
- 将html中的fancybox关闭按钮更改为一页滚动网站
- 单击一页滚动菜单时更改URL哈希值
- 滚动到下一节
- jQuery:点击滚动到下一节
- 滚动到下一节在一个水平的网站