效果:http://hovertree.com/texiao/jquery/28/
效果2:http://hovertree.com/texiao/jquery/28/1.htm
可以使用鼠標也可以使用鍵盤方向鍵.
參考:http://hovertree.com/h/bjaf/jqguding.htm
代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jQuery指定DIV滾動到出現時執行代碼 - 何問起</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<base target="_blank" />
<script type="text/javascript" src="http://hovertree.com/ziyuan/jquery/jquery-1.11.3.min.js"></script>
<style>
body {
margin: 0px;
font-family: Arial;
}
a {
color: blue;
}
</style>
</head>
<body>
<div style="text-align:center;width:100%;margin:0px auto;">
<h1>何問起</h1>
指定DIV滾動到出現時,執行代碼. <br />本頁為把紅色顏色改為藍色.<br />請滾動頁面...<br />
<a href="http://hovertree.com/texiao/yestop/">Yestop</a> <a href="http://hovertree.com/h/bjaf/hoverclock.htm">HoverClock</a> <a href="http://hovertree.com">首頁</a>
<a href="http://hovertree.com/texiao/">特效</a>
<a href="http://hovertree.com/h/bjaf/vc8993j8.htm">原文</a>
</div>
<div style="height: 360px;background-color: #66FF66;text-align:center;">
</div><div id="bg1" style="width:100%;text-align:center;height:200px;background-color:red">
</div>
<div style="width:100%;text-align:center;height:300px;background-color:silver">
</div>
<div style="width:100%;text-align:center;height:200px;background-color:skyblue" id="divhovertree">
何問起 © hovertree.com<br />
本DIV出現時執行代碼,把紅色div改為藍色。
---------------------------內容-----------------
</div>
<div style="height: 200px; visibility: visible; background-color: Olive">
</div><div style="height:200px;background-color:burlywood"></div><div style="height:200px;background-color:darkorchid"></div>
<div style="height: 200px; visibility: visible; background-color:gray">
</div>
<div style="height:200px;background-color:blue"></div>
<div style="height:200px;background-color:deeppink"></div>
<div style="height:200px;background-color:yellow"></div>
<div style="height:200px;background-color:yellowgreen"></div>
<div style="height:800px;background-color:white"></div>
<script type="text/javascript">
/*
*滾動條滑動,位置不變的DIV層
*div_id:DIV的ID屬性值,必填參數
*offsetTop:滾動條滑動時DIV層距頂部的高度,可選參數
*/
function HoverTreeScroll(div_id) {
var Obj = $('#' + div_id);
//判斷元素是否存在 何問起
if (Obj.length != 1) { return false; }
var offsetTop = arguments[1] ? arguments[1] : 0;
var ObjTop = Obj.offset().top - $(window).height();
var h_one = true;
$(window).scroll(function () {
if ($(window).scrollTop() > ObjTop) {
if (h_one) {
//在這裡些滾動指定DIV時執行的代碼
$("#bg1").css("background-color","blue")
h_one = false;
}
}
});
}
//已經滾動到這個ID為divhovertree的DIV.
HoverTreeScroll('divhovertree');
</script>
</body>
</html>