window.scrollY = 現在のフォームのドロップダウンストリップオフセット= $(ウィンドウ).scrollTop()
obj.offsetTop = から電流素子本体の上部から
だから、あなたは、ブラウザの上部から現在の要素から離れて取得したい = obj.offsetTop-window.scrollY。
端部でのブラウザバーのドロップダウンかどうかを確認します。
1. 知っている最初のものは、プルダウンバーブラウザ表示可能領域の高さによるものである> ドキュメントの高さが出てくるまではしませんでした
2. ブラウザの表示領域の高さ:$(ウィンドウ).height() 、ドキュメントの高さ: $(ドキュメント).height() 、プルダウンバーの上部には、オフセット: $(ウィンドウ)を.scrollTop()
3. これら三つの関係:文書の高さ= ドロップダウンバー上記量は、オフセットされた+ ブラウザ表示可能エリアの高さ+ ドロップダウン量は底部バーに達していません
4. プルダウンの、すなわち量は、まだ下のバーに達していない、プルダウンバーが底に達した場合= 0 、それが満たす関係:ドキュメントの高さ = ドロップダウンバー上記量は、オフセットされた+ つまり、ブラウザ表示可能エリアの高さ: $(ドキュメント).height()= $(ウィンドウ).scrollTop()+ $(ウィンドウ).height()、関係はわずか3に決定されます。
可視領域内の要素かどうかを決定します。
示すように、素子に A と素子B の要素間の可視領域でなければなりません。
2つの条件が必要とされています。.. 1 $(OBJ).offsetTop()+ $(OBJ).height()> $(ウィンドウ).scrollTop() 、要素に近いAの条件
2. $(OBJの).offsetTop <$(ウィンドウ).scrollTop()+ $(ウィンドウ).height() 、素子に近いBの条件
さらなる実施形態にテストコードを取り付けます。
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <meta charset="utf-8" /> <script src="Scripts/jquery-3.1.0.js"></script> <script src="Scripts/jquery.wresize.js"></script> <style> label{ display:block; } </style> </head> <body style="height:100%;"> <div id="lbl"> <label>这是一个label</label> <label>这是一个label</label> <label>这是一个label</label> <label>这是一个label</label> <label>这是一个label</label> <label>这是一个label</label> <label>这是一个label</label> <label>这是一个label</label> <label>这是一个label</label> <label>这是一个label</label> <label>这是一个label</label> <label>这是一个label</label> <label>这是一个label</label> <label>这是一个label</label> <label>这是一个label</label> <label>这是一个label</label> <label>这是一个label</label> <label>这是一个label</label> </div> <script> var isFocusStrong = true; var maxSize = 30; var minSize = 12; SetStrong(); $(window).resize(function () { if (isFocusStrong) { SetStrong(); } }); $(window).scroll(function () { var windowScrollTop = $(window).scrollTop(); var scrollHeight = $(document).height(); var windowHeight = $(window).height(); if (windowScrollTop + windowHeight == scrollHeight) { } else if (isFocusStrong) { SetStrong(); } }); function SetStrong() { $.each($("label"), function (i, obj) { var isVisible = false; //元素距离文档顶部的距离 var objOffsetTop = obj.offsetTop; //浏览器窗体下拉条偏移量 var windowScrollTop = $(window).scrollTop(); //可视区域的高度 var windowHeight = $(window).height(); if ((objOffsetTop + $(obj).height() > windowScrollTop) && (objOffsetTop < windowScrollTop + windowHeight)) { isVisible = true; } if (isVisible) { //元素距离可视区域顶端的距离 var top = objOffsetTop - windowScrollTop; var p = top / windowHeight; var size = (1 - Math.abs(p - 0.45)) * maxSize; if (size < minSize) size = minSize; $(obj).css("font-size", size + "px"); $(obj).text("($(window).scrollTop():" + windowScrollTop + ",top:" + top + ",obj.offsetTop:" + objOffsetTop + ",$(obj).height():" + $(obj).height() + ",isVisible:" + isVisible + ")"); } else { $(obj).text("(isVisible:" + isVisible + ")"); } }); }; // 获取页面可视区域高度、宽度 function getPageSize() { var xScroll, yScroll; if (window.innerHeight && window.scrollMaxY) { xScroll = window.innerWidth + window.scrollMaxX; yScroll = window.innerHeight + window.scrollMaxY; } else { if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac xScroll = document.body.scrollWidth; yScroll = document.body.scrollHeight; } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari xScroll = document.body.offsetWidth; yScroll = document.body.offsetHeight; } } var windowWidth, windowHeight; if (self.innerHeight) { // all except Explorer if (document.documentElement.clientWidth) { windowWidth = document.documentElement.clientWidth; } else { windowWidth = self.innerWidth; } windowHeight = self.innerHeight; } else { if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode windowWidth = document.documentElement.clientWidth; windowHeight = document.documentElement.clientHeight; } else { if (document.body) { // other Explorers windowWidth = document.body.clientWidth; windowHeight = document.body.clientHeight } } } // for small pages with total height less then height of the viewport if (yScroll < windowHeight) { pageHeight = windowHeight; } else { pageHeight = yScroll; } // for small pages with total width less then width of the viewport if (xScroll < windowWidth) { pageWidth = xScroll; } else { pageWidth = windowWidth; } arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight); return arrayPageSize; } function GetSize() { $("#lbl").html( "屏幕分辨率为:" + screen.width + "*" + screen.height + "<br />" + "屏幕可用大小:" + screen.availWidth + "*" + screen.availHeight + "<br />" + "网页可见区域宽:" + document.body.clientWidth + "<br />" + "网页可见区域高:" + document.body.clientHeight + "<br />" + "网页可见区域宽(包括边线的宽):" + document.body.offsetWidth + "<br />" + "网页可见区域高(包括边线的宽):" + document.body.offsetHeight + "<br />" + "网页正文全文宽:" + document.body.scrollWidth + "<br />" + "网页正文全文高:" + document.body.scrollHeight + "<br />" + "网页被卷去的高:" + document.body.scrollTop + "<br />" + "网页被卷去的左:" + document.body.scrollLeft + "<br />" + "网页正文部分上:" + window.screenTop + "<br />" + "网页正文部分左:" + window.screenLeft + "<br />" + "屏幕分辨率的高:" + window.screen.height + "<br />" + "屏幕分辨率的宽:" + window.screen.width + "<br />" + "屏幕可用工作区高度:" + window.screen.availHeight + "<br />" + "屏幕可用工作区宽度:" + window.screen.availWidth ); } </script> </body> </html>