Viewport 是一個簡單的jQuery插件,爲元素添加自定義僞選擇器和處理程序,以便在可視窗口內外進行簡單的元素檢測。javascript
<script src="jquery.js" type="text/javascript"></script> <script src="jquery.viewport.js" type="text/javascript"></script>
$( ":in-viewport" ); $( ":above-the-viewport" ); $( ":below-the-viewport" ); $( ":left-of-viewport" ); $( ":right-of-viewport" ); $( ":partly-above-the-viewport" ); $( ":partly-below-the-viewport" ); $( ":partly-left-of-viewport" ); $( ":partly-right-of-viewport" ); $( ":have-scroll" );
黃色部分離開屏幕後顯示返回按鈕
java
var wodBackButton = function () { //元素在屏幕左側顯示返回按鈕 $("#wodsHome:left-of-screen").each(function () { $('#wodBackButton').removeClass('hide'); return; }); //元素在屏幕顯示區域隱藏返回按鈕 $("#wodsHome:in-viewport").each(function () { $('#wodBackButton').addClass('hide'); return; }); } $('#mediaContainer').bind("scroll", function (event) { wodBackButton(); }); wodBackButton();
經過使用這個插件能很簡單方便的獲取屏幕的區域,屏幕之外的區域,進行開發。jquery
github地址git