사용자 커서가 화면의 오른쪽 20px 내에있을 때 열리는 애니메이션 메뉴를 만들었습니다. 사용자 커서가 2 초 내에이 영역 밖으로 나가면 자바 스크립트 타임 아웃으로 어려움을 겪고 있습니다. 내 코드는 지금까지 다음과 같습니다mousemove에서 Javascript clear timeout
HTML
자바 스크립트 // 타이머 변수 var에 타이머;
function openToolbar()
{
// Only execute for desktop
$('.no-touch').on('mousemove',function(event) {
// Toolbar and Window width
var tableToolbar = $('.ac-table-toolbar'),
winWidth = $(window).width();
// If cursor enters right hand side of the screen start the timer
// and execute after 2 seconds
if(event.pageX > (winWidth - 20)) {
// Timeout
timer = setTimeout(function()
{
// Add active class to toobar and css transition will animate it
// to open position
tableToolbar.addClass('active').removeClass('notActive');
}, 2000);
}
// If mouse pointer leaves right hand side of the screen and
// still has notActive class cancel the timeout to prevent
// the toolbar from opening
if(event.pageX < (winWidth - 20) && tableToolbar.hasClass('notActive'))
{
clearTimeout(timer);
}
// Toolbar has active class so we know its visible
if(tableToolbar.hasClass('active') && event.pageX < (winWidth - 220))
{
// Clear timeout (if needed?)
clearTimeout(timer);
// Remove active class and css transition will return it to docked position
tableToolbar.removeClass('active').addClass('notActive');
}
});
}
애니메이션은 활성화 된 notActive 클래스에 의해 트리거되는 CSS 전환으로 처리됩니다.
누구든지 올바른 방향으로 나를 가리킬 수 있습니다. 미리 감사드립니다.
논외 약간,하지만 당신은 다시 정의 tableToolbar 및 winWidth 때마다 마우스 움직임을 할 필요가 없습니다, 그래서'VAR tableToolbar = $ ('AC-테이블을 이동하는 것이 좋습니다. -toolbar '), winWidth = $ (window) .width();'mousemove' 이벤트 콜백 외부. – jpotapova