function roll_active(){
$(".tm").each(function() { //需要隨滾動(dòng)位置添加動(dòng)畫(huà)的對(duì)象均加上類(lèi)名tm
var e = $(this);
if(!e.hasClass('active')){
var cth = $(window).scrollTop();
// console.log('可視窗口頂部距離頁(yè)面頂部的高度'+cth);
var cbh = $(window).height() + $(window).scrollTop();
// console.log('可視窗口底部距離頁(yè)面頂部的高度'+cbh);
var domth = e.offset().top;
// console.log('元素頂部距離頁(yè)面頂部的位置' + domth);
var dombh = e.offset().top + e.height();
//console.log('元素底部距離頁(yè)面頂部的位置' + dombh);
if(cbh > domth && dombh > cth) {
e.addClass('active'); //加動(dòng)畫(huà)的類(lèi)名 .tm.active
}
}
});
}
//滾動(dòng)監(jiān)聽(tīng)插件
$(window).scroll(function(e) {
roll_active();
});
//頁(yè)面刷新初始化
$(document).ready(function(){
roll_active();
});
注意:
body需要高度為auto ,overflow:visible;