簡體   English   中英

如何滾動以將DIV的底部設置在屏幕底部(監視器)

[英]how to scroll to set the bottom of a DIV at the bottom of the screen (monitor)

我的網站上有一個DIV ,它的高度不是固定的。 當用戶在圖像上移動鼠標時,將顯示此DIV ,並將顯示有關該圖像的一些信息。 頁面上有幾張網格格式的圖像,每個圖像都有自己的信息。 顯然,某些圖像位於屏幕底部,因此,通過將mouseover在其部分信息上, DIV將不在屏幕上。 我想自動控制這種情況,當它的DIV任何部分不在屏幕上時,滾動條就會下降,直到到達DIV的底部。

這是我的偽代碼,但是我無法在jquery中實現它。

  1. 計算screenHeight
  2. 計算divHeight
  3. 計算divDistanceFromTopOfScreen

如果screenHeight < divHeight + divDistanceFromTopOfScreen然后

    scroll down to bottom of `DIV`

如果您認為此算法正確,那么我該如何實現呢?

這應該做。

Javascript:

var top = $('.theDiv').offset().top;     // the top of your div relative to the document
var div_height = $('.theDiv').height();  // the height of your div
window.scrollTo(0, top + div_height);    // scroll to the bottom of your div

參考文獻:

小提琴: http : //jsfiddle.net/manishie/NGDBL/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM