【发布时间】:2014-02-17 10:22:59
【问题描述】:
如何修改下面的脚本,以便在 DOM 中不存在“.fixit”元素时不会引发错误?
function fixedHeaders() {
var el = jQuery('.fixit'),
offset = el.offset(),
elHeight = el.height(),
scrollTop = jQuery(window).scrollTop()
if (((offset.top + 400) < scrollTop - el.height())) {
el.addClass('fixedElement');
}
if (scrollTop === 0) {
el.removeClass('fixedElement');
}
}
jQuery(function() {
jQuery(window)
.scroll(fixedHeaders)
.trigger("scroll");
});
【问题讨论】:
-
说真的,搜索时很难找到答案吗?
标签: jquery