【发布时间】:2014-04-26 16:21:46
【问题描述】:
BLUF:使用 jQuery Waypoints 使用滚动到元素的 ID 更新 location.hash 会导致 FF 中的滚动冒泡/卡顿,但 Chrome 或 IE 不会。
问题: 我正在使用jQuery Waypoints 做一些事情,因为用户滚动浏览具有这种基本布局的表格:
____________________________________________________
| <tr class="category" id="cat1">Category One</tr> |
| Cell | Cell | Cell |
| Cell | Cell | Cell |
| Cell | Cell | Cell |
____________________________________________________
| <tr class="category" id="cat2">Category Two</tr> |
| Cell | Cell | Cell |
| Cell | Cell | Cell |
| Cell | Cell | Cell |
我想做的一件事是将视口顶部的.category 行的ID 附加到window.location。这个 jQuery 在 Chrome 34 和 IE 中工作,但在 FF 28 中它会导致“冒泡”行为,即浏览器会尝试跳回以将元素放在视口顶部,即使用户不断向下滚动强>。我在这里注释掉了preventDefault,因为虽然它修复了FF 中的跳转,但它也阻止了脚本在到达第一个.category 元素后更新哈希。有没有人在更新 FF 中的location.hash 时遇到过类似的问题,或者知道如何解决它?
$( '.category').waypoint(function() {
//strip current hash from window.location
window.location.hash.replace('#','');
//store waypoint element's id
var setHref = $(this).attr('id');
//set id as location.hash
window.location.hash = setHref;
//e.preventDefault();
return false;
}, { context: 'section' });
【问题讨论】:
标签: javascript firefox window.location jquery-waypoints