1 回答
TA貢獻1836條經驗 獲得超13個贊
不確定這是否正是您想要實現的,但這是代碼。如果標題距窗口頂部的距離超過 100 像素(這不是很常見,因為標題頂部應該有一些東西),那么新類將添加到標題中。
$(function() {
var $header = $('#v0');
$(window).scroll(function () {
if ($header.offset().top - $(this).scrollTop() > 100) {
$header.addClass('blabla');
} else {
$header.removeClass('blabla');
}
});
});
更新:根據您的反饋,這是我想到的第一個解決方案。我認為這就是你需要的行為。希望對你有用:
$(function() {
var $header = $('header');
var $video = $('#v0');
var $videoContainer = $('.videoContainer');
$(window).scroll(function () {
// Here we check if video field touches the header, and add 'fixed' class
if ((($header.offset().top + $header.height()) - $video.offset().top) >= 0) {
$video.addClass('fixed')
}
// Since both video and header is fixed now I needed some other
// element to check if we are again getting away from the header
// (scrolling up again) That's why I added the $videoContainer element
// to be able to remove the 'fixed' class.
if ($videoContainer.offset().top > ($header.offset().top + $header.height())) {
$video.removeClass('fixed');
}
});
});
更新代碼:https : //jsbin.com/foyoyus/6/edit?html,css,js,output
添加回答
舉報
