简体   繁体   English

如何在视口中获取选定的div?

[英]How to get selected div inside a viewport?

 $('.lorem').click(function() { $('.active').removeClass('active'); $(this).addClass('active'); }); $(document).keydown(function(e){ e.preventDefault(); var i = $('.active').index('.lorem') + 1; var next = $('.lorem').eq(i); $('.active').removeClass('active'); next.addClass('active'); }); 
 .lorem{ line-height:21px; cursor:pointer; } .active{ background:gold; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class='lorem'>lorem</div> <div class='lorem'>lorem</div> <div class='lorem'>lorem</div> <div class='lorem'>lorem</div> <div class='lorem'>lorem</div> <div class='lorem'>lorem</div> <div class='lorem'>lorem</div> <div class='lorem'>lorem</div> <div class='lorem'>lorem</div> <div class='lorem'>lorem</div> <div class='lorem'>lorem</div> 

I have the above code to select next class lorem by pressing down arrow key. 我上面的代码来选择下一堂课loremdown箭头键。

And the similar code to select previous lorem by pressing up arrow key. 和类似的代码可以通过按up箭头键选择以前的lorem

Imagine there are a lot more lorem divs on the page (about 200). 想象一下,页面上还有更多的lorem div(大约200个)。

Is there a way to scroll page automatically when the selected div is outside of viewport, so it would be inside the viewport? 当选定的div在视口之外时,是否有一种自动滚动页面的方法,因此它会在视口内?

You can add this code to your key down listener: 您可以将此代码添加到按键监听器中:

$('html, body').animate({
     scrollTop: next.offset().top
}, 1000);

And check if an element in viewport or not: 并检查视口中是否有元素:

function isElementInViewport(el) {
     var rect = el.getBoundingClientRect();
     return (
           rect.top >= 0 &&
           rect.left >= 0 &&
           rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */
           rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */
     );
}

It will scroll the page down to the next element. 它将页面向下滚动到下一个元素。

 function isElementInViewport(el) { var rect = el.getBoundingClientRect(); return ( rect.top >= 0 && rect.left >= 0 && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */ rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */ ); } $('.lorem').click(function() { $('.active').removeClass('active'); $(this).addClass('active'); }); $(document).keydown(function(e){ e.preventDefault(); var i = $('.active').index('.lorem') + 1; var next = $('.lorem').eq(i); $('.active').removeClass('active'); next.addClass('active'); if (next[0] && !isElementInViewport(next[0])) { $('html, body').animate({ scrollTop: next.offset().top }, 1000); } }); 
 .lorem{ line-height:21px; cursor:pointer; } .active{ background:gold; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class='lorem'>lorem</div> <div class='lorem'>lorem</div> <div class='lorem'>lorem</div> <div class='lorem'>lorem</div> <div class='lorem'>lorem</div> <div class='lorem'>lorem</div> <div class='lorem'>lorem</div> <div class='lorem'>lorem</div> <div class='lorem'>lorem</div> <div class='lorem'>lorem</div> <div class='lorem'>lorem</div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM