javascript - How to calculate the actual screen distance jQuery? -
i want place ad in page column. when scroll down, want ad follow way, position in vertical middle of page. using script right can set distance cannot set exact 50% distance.
i have tired $window.height()/2
or $document.height()/2
, calculate table height instead of actual screen height. how solve this? thanks!
<html> <head> <script type='text/javascript' src='http://code.jquery.com/jquery-1.9.1.js'></script> <script type="text/javascript"> $(function () { var $sidebar = $("#sidebar"), $window = $(window); $window.scroll(function () { if ($window.scrolltop() > $window.height() / 2) { $sidebar.css('position', 'absolute'); $sidebar.css('top', $window.scrolltop() + $window.height() / 2); $sidebar.slidedown(500); } else { $sidebar.css('position', 'auto'); $sidebar.css('top', 'auto'); } }); }); </script> </head> <body> <br><br> <table bgcolor="#cccccc" width="800" height="3000" align="center" border="1"> <tr> <td width="150" valign="top"> <div style="width:100px;height:100;background:white;"></div> <br> <div style="width:100px;height:100;background:blue;"></div> <br> <div style="width:100px;height:100;background:yellow;"></div> <br> <div style="width:100px;height:100;background:pink;"></div> <br> <div style="width:100px;height:100;background:maroon;"></div> <br> <!-- ad --> <div style="width:100px;height:100;background:red;" id="sidebar">test</div> <br> </td> <td width="500"></td> <td width="150"></td> </tr> </table> </body> </html>
from jquery document, $(window).height();
should return height of browser viewport.
i represent demo based on html , js. looks site :)
see it: http://jsfiddle.net/frfsp/1/
Comments
Post a Comment