每次在顶部滚动顶部重置
scrollTop reset at the top everytime
我得到了以下代码:
<script type="text/javascript">
jQuery(document).ready(function(){
var Opened = false;
jQuery('.expend-schedule').click(function(e){
// On empêche l'action
e.preventDefault();
// Variable
var DataType = jQuery(this).attr('data-type');
// On va fermer toutes les tables
jQuery('.schedule').each(function(){
// On va fermer uniquement si ouverte
if( jQuery(this).css('display') != 'none' && jQuery(this).attr('data-type') != DataType ){
// On ferme
jQuery(this).fadeOut( 500, function(){
// Est-ce que l'animation et l'ouverture s'est produite déjà ?
if(Opened === false){
var TableElement = jQuery('.schedule[data-type="'+DataType+'"]');
// On ouvre et on s'y déplace de façon automatique
TableElement.fadeIn(TableElement.height() * 0.47, function(){
jQuery('html, body').animate({
scrollTop: TableElement.offset().top - 50
}, 'slow');
});
// On dit que c'est ouvert, afin d'empêcher l'ouverture à de multiples reprises
// si'il y a eu un bug
Opened = true;
}
});
}
});
// On reset
Opened = false;
});
</script>
这是我的页面的一个例子:
<h4 class="sep_bg"><a class="expend-schedule" data-type="week" href="#">Monday to Friday</a></h4>
<table class="schedule table table-condensed" data-type="week">
<thead>
<tr>
<th style="width: 60px;">Hour</th>
<th style="width: 710px;">Transit</th>
</tr>
</thead>
<tbody>
<tr>
<td>04h</td>
<td>04h15, 04h27, 04h39, 04h59</td>
</tr>
</tbody>
</table>
<h4 class="sep_bg"><a class="expend-schedule" data-type="saturday" href="#">Saturday</a></h4>
<table class="schedule table table-condensed" data-type="saturday">
<thead>
<tr>
<th style="width: 60px;">Hour</th>
<th style="width: 710px;">Transit</th>
</tr>
</thead>
<tbody>
<tr>
<td>04h</td>
<td>04h15, 04h27, 04h39, 04h59</td>
</tr>
</tbody>
</table>
所以我的问题是:
每次我单击类expend-schedule
所在的位置时,它都应该从当前滚动位置移动到元素位置。这是有效的,除了当我单击时,它直接进入页面顶部,然后移动到元素位置。
有什么想法吗?谢谢。
我认为
它不会达到顶部,我尝试使用您提供的代码对其进行测试(缺少一些括号)。无论如何,我认为淡出会导致事物移动到顶部然后下降的感觉,因为您的 scrollTop 在动画完成功能中。如果你去掉淡入淡出,那么它就可以正常工作,尽管对我来说它只在下一次点击没有做任何事情时起作用。
因此,在没有褪色的情况下测试它,看看它是否仍在做你认为它正在做的事情。
浏览器似乎首先尊重<a href="#">
链接,将其带到页面顶部,然后执行动画。
我会将其更改为<a href="javascript:void(0);">Text</a>
以解决此问题。
相关文章:
- 单击顶部导航时如何进行向下滚动效果(向下滑动).
- 背景图像顶部的滚动图像不移动
- 滚动然后捕捉到顶部而不是取消捕捉
- 如何在从顶部滚动时暂停转盘/滑块,并在滚动到顶部时重新开始
- Android默认浏览器:谷歌地图制作“;位置:相对;元素在页面顶部滚动
- 如何将“切换滑块内容”的顶部滚动到视口的中心
- 修复了在页面顶部滚动时居中导航的问题
- 每次在顶部滚动顶部重置
- 为什么有时滚动顶部/滚动左不可写
- 当目标处于顶部滚动状态时,如何从菜单中激活项目
- 请在顶部滚动一次
- 当用户从顶部滚动超过一定距离时,执行Javascript
- 向表格添加水平顶部滚动条的Aurelia方式
- 如何使文档(页面)向左和顶部以及相对于正文的滚动(向左和顶部滚动)
- 从导航底部而非页面顶部滚动至Div
- 顶部滚动-可替代preventDefault()函数
- “无限scroll"代码只适用于顶部滚动
- 当导航栏固定顶部滚动时,引导捕获
- 单击时从窗口顶部滚动到特定位置
- 花式框弹出窗口始终在顶部滚动