我在项目中使用jQuery Mouse Wheel来操纵特定div上的滚动。
var viewWidth = $(window).width();
var temoignagesWidth = $('#temoignages .container-fluid').width();
var maxScrollTemoignages = temoignagesWidth - viewWidth;
var scrollVal = 0;
var temoignagesDiv = getTemoignagesDiv();
$(window).mousewheel(function(event){
if (temoignagesDiv.top <= $(window).scrollTop() + navbarHeight && $(window).scrollTop() + $(window).height() < temoignagesDiv.bot){
scrollVal += Math.round(event.deltaY * event.deltaFactor);
var scrollUp = function(scrollVal){
return scrollVal <= 0;
};
var scrollDown = function(scrollVal){
return Math.abs(scrollVal) <= maxScrollTemoignages;
};
var condition = null;
if (event.deltaY > 0){
scrollVal = scrollUp(scrollVal) ? scrollVal : 0;
condition = scrollUp(scrollVal);
} else{
scrollVal = scrollDown(scrollVal) ? scrollVal : maxScrollTemoignages * -1;
condition = scrollDown(scrollVal);
}
if (condition){
if (event.deltaY > 0 && scrollVal < 0){
event.preventDefault();
} else if (event.deltaY < 0 && Math.abs(scrollVal) < maxScrollTemoignages ){
event.preventDefault();
}
$('#temoignages .row').css({
"-webkit-transform":"translate(" + scrollVal + "px)",
"-ms-transform":"translate(" + scrollVal + "px)",
"transform":"translate(" + scrollVal + "px)"
});
}
}
});
它运行良好但有时当我滚动条件时temoignagesDiv.top <= $(window).scrollTop() + navbarHeight && $(window).scrollTop() + $(window).height() < temoignagesDiv.bot
未验证,因此代码不会被执行。有什么想法改进吗? :■