페이지 열에 광고를 삽입하고 싶습니다. 아래로 스크롤 할 때 광고를 끝까지 따라 가고 페이지의 수직 가운데에 배치합니다. 스크립트을 사용하면 특정 거리 만 설정할 수 있지만 정확히 50 % 거리를 설정할 수는 없습니다.실제 화면 거리 jQuery를 계산하는 방법은 무엇입니까?
나는 $window.height()/2
또는 $document.height()/2
피곤하지만 실제 화면 높이 대신 테이블 높이를 계산합니다. 어떻게 해결할 수 있을까요? 감사!
<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>
좋은 생각 이니 그가 성취하기를 원하는 것 같아요. @IamRan –
음 ... 가깝지만 정확하게는 아니지만 ...이 웹 사이트를 확인해 주시겠습니까? http://live1.7m.cn/ 중간 열에있는 광고가 처음부터 제자리에 쌓이며, 스크롤 할 때만 페이지 중간에 머물러 있습니다. Btw, 나는 당신의 스크립트를 복사하고 테스트하여 IE에서만 작동하지만 크롬에서는 작동하지 않는다는 것을 알았습니다. –
크롬이 사용 중입니다. 크롬을 사용하여 데모를 작성했습니다. 사이트를 방문하여 시도해 보겠습니다. – Bigxiang